Last active
December 14, 2015 12:18
Synchronous chain
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
function chain() { | |
var queue = [] | |
function chainer(func) { | |
queue.push(function (x) {return func(x)}) | |
return chainer | |
} | |
chainer.run = function () { | |
queue.reduce(function (previous, func) {return func(previous)}, undefined) | |
} | |
return chainer | |
} | |
// Example | |
chain() | |
(just([1,2,3,4,5,6])) | |
(filter(even)) | |
(map(double)) | |
(print) | |
.run() | |
// Details | |
function filter(func) {return function (arr) {return arr.filter(func)}} | |
function map(func) {return function (arr) {return arr.map(func)}} | |
function just(x) {return function () {return x}} | |
function even(x) {return x%2==0} | |
function double(x) {return x*2} | |
function print(x) {console.log(x)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment