Created
May 26, 2013 23:56
-
-
Save agrif/5654491 to your computer and use it in GitHub Desktop.
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
| do x <- foo | |
| y <- bar | |
| return x * y | |
| (which is (almost*) equivalent to (in python syntax)) | |
| foo.bind(lambda x: | |
| bar.bind(lambda y: | |
| x * y)) | |
| So, "x <- foo" tells the interpreter/compiler "wrap up everything after this in a function accepting x as an argument, then pass it to foo.bind". For things like futures, where fut.bind() calls the given function when the future gets a result, this is equivalent to saying "wait for foo to finish, then set x to the result and continue". | |
| This is also why this is done with generators in python. When you yield in a generator, the controlling code has access to next(gen), which is precisely a function that wraps up all the rest of the code in the generator, and a way of sending a value back in. The only problem is that running next(gen) modifies gen, while running a normal function doesn't. | |
| (almost*): because the last part should be MonadType.return(x * y), which you can pretty much ignore for now. It has to do with the sort of function bind() expects. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment