purescript-exists icon indicating copy to clipboard operation
purescript-exists copied to clipboard

`mapExists` helper

Open prescientmoon opened this issue 3 years ago • 2 comments

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.

prescientmoon avatar Aug 07 '21 23:08 prescientmoon

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.

yukikurage avatar Nov 06 '22 11:11 yukikurage

Works for me.

JordanMartinez avatar Nov 07 '22 17:11 JordanMartinez