Created
April 20, 2013 11:30
-
-
Save hiroshi-maybe/5425688 to your computer and use it in GitHub Desktop.
Note to understand "Monads Are Not Metaphors".
http://www.codecommit.com/blog/ruby/monads-are-not-metaphors
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
| andThen f1 f2 = do f1 | |
| f2 | |
| --foo bar = (print "Executing foo") `andThen` (print bar) `andThen` (do return (length bar)) | |
| -- Container Thing | |
| data Thing a = Thing a deriving Show | |
| value :: Thing a -> a | |
| value (Thing x) = x | |
| -- Increment with container | |
| foo :: Int -> Thing Int | |
| foo i = Thing (i+1) | |
| a = Thing 1 | |
| b = foo $ value a | |
| -- Increment without container | |
| foo' :: Int -> Int | |
| foo' i = i+1 | |
| a' = 1 | |
| b' = foo' a' | |
| -- Increment with Monad | |
| -- Start with something which you pull apart and use to compute a new something of that same type | |
| -- pull apart with pattern match, Apply f (compute) and return with container | |
| bind :: Thing a -> (a -> Thing b) -> Thing b | |
| bind (Thing x) f = f x | |
| a'' = Thing 1 | |
| b'' = a'' `bind` foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment