Skip to content

Instantly share code, notes, and snippets.

@strandel
Last active December 14, 2015 12:18
Show Gist options
  • Save strandel/5084887 to your computer and use it in GitHub Desktop.
Save strandel/5084887 to your computer and use it in GitHub Desktop.
Synchronous chain
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