purescript-exists
purescript-exists copied to clipboard
`mapExists` helper
I found myself implementing a very basic mapExists
function for my latest project, and wondered if it might fit here:
-- | Map the interoior of an existential
mapExists :: forall f. (forall a. f a -> f a) -> Exists f -> Exists f
mapExists f = runExists (f >>> mkExists)
I have not tried, but unsafeCoerce identity
should work as well.
mapExists can also change type.
mapExists :: forall f g. (f ~> g) -> Exists f -> Exists g
mapExists f = runExists (mkExists <<< f)
This may be a little useful when making a Functor instance of the data created by Exists
data T a b = T b (b -> a)
newtype TE a = TE (Exists (T a))
instance Functor TE where
map f (TE e) = TE $ mapExists (\(T a g) -> T a (map f g)) e
-- map f (TE e) = TE $ runExists (\(T a g) -> mkExists $ T a (map f g)) e
It is just a little different, but I think we can write more intuitively.
Works for me.