Created
April 17, 2013 07:37
-
-
Save jonifreeman/5402450 to your computer and use it in GitHub Desktop.
Do-notation for Fantasy Land
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 | |
z <- baz | |
return x * y * z | |
} | |
Desugars into: | |
foo.chain(function(x) { | |
return bar.chain(function(y) { | |
return baz.map(function(z) { | |
return x * y * z | |
}) | |
}) | |
}) | |
*/ | |
macro $do { | |
case { $a:ident <- $ma:expr return $b:expr } => { | |
$ma.map(function($a) { | |
return $b; | |
}); | |
} | |
case { $a:ident <- $ma:expr $rest ... } => { | |
$ma.chain(function($a) { | |
return $do { $rest ... } | |
}); | |
} | |
} | |
$do { | |
x <- foo | |
y <- bar | |
z <- baz | |
return x*y*z | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couldn't figure out yield but I got you a better do notation that allows standard var bindings between monadic let bindings
generates