Created
September 18, 2013 19:52
-
-
Save danidiaz/6614635 to your computer and use it in GitHub Desktop.
Composing applicative functors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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