Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Last active April 23, 2017 10:34
Show Gist options
  • Select an option

  • Save davidallsopp/f00cac7d73f4d3533482 to your computer and use it in GitHub Desktop.

Select an option

Save davidallsopp/f00cac7d73f4d3533482 to your computer and use it in GitHub Desktop.
Composing functors using Data.Functor.Compose - thanks to Tony Morris for his presentation on Monad Transformers at http://vimeo.com/73648150
λ> import Data.Functor.Compose
-- List of lists
λ> getCompose $ fmap (+1) $ Compose [[],[1],[2,3],[4,5,6]]
[[],[2],[3,4],[5,6,7]]
-- List of Maybe
λ> getCompose $ fmap (+1) $ Compose [Just 1, Just 2, Nothing, Just 3]
[Just 2,Just 3,Nothing,Just 4]
-- Maybe of Lists
λ> getCompose $ fmap (+1) $ Compose $ Just [1,2,3]
Just [2,3,4]
-- the getCompose is needed because theres no Show instance for Compose
-- so we can map (fmap) an ordinary function over a nested structure of any two functors
-- rather than having to do:
λ> (fmap . fmap) (+1) [[],[1],[2,3],[4,5,6]]
[[],[2],[3,4],[5,6,7]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment