Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Created September 18, 2013 19:52
Show Gist options
  • Select an option

  • Save danidiaz/6614635 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/6614635 to your computer and use it in GitHub Desktop.
Composing applicative functors.
import Control.Monad.Trans.List
import Control.Monad.Trans.Writer
import Control.Applicative
import Data.Functor.Compose
import Data.Monoid
tell' :: Monoid a => a -> Compose [] (Writer a) ()
tell' = Compose . pure . tell
list :: Monoid a => [x] -> Compose [] (Writer a) x
list = Compose . map pure
foo :: Compose [] (Writer String) Int
foo = tell' "a" *>
list [3,4] <**>
Compose [pure succ,pure pred] <*
Compose [tell "c", tell "d"]
fim :: [Writer String Int]
fim = getCompose $ foo
fum :: [(Int,String)]
fum = map runWriter fim
main = putStrLn . show $ fum
-- *Main> [2,3] <* [(),()]
-- [2,2,3,3]
-- Cool!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment