Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active March 11, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbx6spp/c67f459bddff36686aa5a7d60bee1622 to your computer and use it in GitHub Desktop.
Save mbbx6spp/c67f459bddff36686aa5a7d60bee1622 to your computer and use it in GitHub Desktop.
Intuitions on functors.
fmap :: Functor f => (a -> b) -> f a -> f b
contramap :: Contravariant f => (a <- b) -> f a -> f b
invmap :: Invariant f => (a <-> b) -> f a <-> f b -- NOT VALID syntax but to give an intuition.
bimap :: Bifunctor p => (a -> b)
-> (c -> d)
-> p a c
-> p b d
dimap :: Profunctor p => (a -> b)
-> (c -> d)
-> p b c
-> p a d
-- putting bimap and dimap together in a slightly different way:
bimap :: Bifunctor p => (a -> b) -> (c -> d) -> p a c -> p b d
dimap :: Profunctor p => (a <- b) -> (c -> d) -> p b c -> p a d
-- another way showing the shapes of the types
bimap :: Bifunctor (p :: * -> *) => (a -> b) -> (c -> d) -> p a c -> p b d
dimap :: Profunctor (p :: * -> *) => (a <- b) -> (c -> d) -> p b c -> p a d
-- Note Bifunctor is covariant in both type arguments
-- Note Profunctor is contravariant in the first type argument and covariant in the second
-- Profunctors are often used for defining decoding and encoding pairs (e.g. JSON (de-)serialization, reading and writing to database).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment