This is some example code that demonstrates extending the sequence
method that is common to functional programming to absorb some monadic abstractions.
Please use Github Issues or comment on Commits to provide feedback. All thoughts are welcome.
The canonical source code is coffee-src/supervis.es.coffee
. The code is tested using npm test
, and a pretest
script compiles the CoffeeScript to JavaScript in lib/supervis.es.js
. jasmine-node
then interprets the specs in the spec
folder (they're also written in CoffeeScript).
To try it, you'll need node, jasmine-node, CoffeeScript, and their respective dependencies.
The key function is sequence
. In naïve form, sequence
appears to create a pipeline through a series of unary functions, much like functional composition in reverse:
plusOne = (n) -> n + 1
double = (n) -> n * 2
sequence(plusOne, double, double)(1)
#=> 8
If the first argument to sequence
is an instance of Supervisor
, then sequence
will use the Supervisor to wrap and unwrap values along the way. Here is the above example using the Writer
monad:
plusOne = (n) -> [n + 1, "(#{n}) -> #{n + 1}; "]
double = (n) -> [n * 2, "(#{n}) -> #{n * 2}; "]
sequence(Supervisor.Writer, plusOne, double, double)(1)
#=> [ 8, '(1) -> 2; (2) -> 4; (4) -> 8; ' ]
If the first argument isn't a Supervisor instance, sequence
actually uses the Identity
monad behind the scenes.
At this time, I've coded up:
- Identity
- Maybe
- Writer
- List
- Promise
- Callback
The Promise
monad wraps a ridiculously simple Promise
implementation. Errors fall through much like the Maybe monad. Callback
is an implementation of Haskell's infamous "Continuation" monad: You can sequence asynchronous callback functions without ugly nesting.
Yes please!
- File a Github Issue
- Comment on a Commit
- Fork this and issue a Pull Request
Thanks!