Monadic array comprehension with meta programming in JS
Implementation of Haskell-like array comprehension, probabily in the most eccentric way.
$[x + y | x <- [1, 2, 3], y <- [4, 5, 6], x * y % 2 === 0]
// -> [ 5, 7, 6, 7, 8, 7, 9 ]
- With Sweet.js, define a macro for array
comprehension operator
$
. - The macro builds array comprehension into async/await syntax.
- Replace global
Promise
withCArray
(stands for comprehensible array)- Async/await is like do expression in Haskell
- Now it executes monad comprehension for array
- Build async/await syntax into
Promise
chaining- Do expression in Haskell can also be transformed into bind chaining
- Profit!
npm run build -- examples/simple.js
node dist/simple.js
You can see each build step with each build script.
# build macro
./scripts/build-macro.sh examples/simple.js
cat dist/simple.macro.js
# build async/await into Promise chaining with Babel
./scripts/build-babel.sh dist/simple.macro.js
cat dist/simple.babel.js
# concat monad stuff
./scripts/build-monad.sh dist/simple.babel.js
cat dist/simple.js