Skip to content

Instantly share code, notes, and snippets.

@jonifreeman
Created April 17, 2013 07:37
Show Gist options
  • Save jonifreeman/5402450 to your computer and use it in GitHub Desktop.
Save jonifreeman/5402450 to your computer and use it in GitHub Desktop.
Do-notation for Fantasy Land
/*
$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
}
@bradphelan
Copy link

@jonifreeman I mean a lazy yield ( generator )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment