Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

do-notation using sweet.js

View do.sjs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
macro $do {
case { $y:expr } => {
$y
}
case { $x:ident <- $y:expr $rest ... } => {
λ['>=']($y, function($x) {
return $do { $rest ... }
});
}
}
 
// Using the bilby.js functional programming library:
// https://github.com/pufuwozu/bilby.js
var λ = require('./bilby');
 
// Haskell:
//
// do
// x <- [1, 2]
// y <- [x, x]
// [y * 2, y * 3]
//
// [2,3,2,3,4,6,4,6]
 
// sweet.js:
 
// Can't use arrays directly because of https://github.com/mozilla/sweet.js/issues/28
// Need to manually replace a, b and c in output with array expressions.
 
var list = $do {
x <- a // [1, 2]
y <- b // [x, x]
c // [y * 2, y * 3]
};
console.log(list);
 
// [ 2, 3, 2, 3, 4, 6, 4, 6 ]

This is awesome! Nice job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.