Created
February 23, 2011 17:46
-
-
Save poetix/840799 to your computer and use it in GitHub Desktop.
Lazy streams in Javascript
This file contains 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
'use strict'; | |
var slothful = (function() { | |
function _(step) { | |
var getResult = function() { return step(); }; | |
return function() { | |
var result = getResult(); | |
getResult = function() { return result; } | |
return result; | |
}; | |
} | |
function $(token) { | |
return token(); | |
} | |
function lazy(val) { | |
return _(function() { return val; }); | |
} | |
function cons(h, t) { | |
return {h: h, t: t}; | |
} | |
function head(cell) { | |
return cell.h; | |
} | |
function tail(cell) { | |
return cell.t; | |
} | |
function _null() { | |
return lazy(null); | |
} | |
function each(_s, f) { | |
var s = $(_s); | |
if (s === null) { | |
return _null(); | |
} | |
f(head(s)); | |
return each(tail(s), f); | |
} | |
function map(_s, f) { | |
var s = $(_s); | |
if (s === null) { | |
return _null(); | |
} | |
return _(function() { return cons(f(head(s)), map(tail(s), f)); }); | |
} | |
function filter(_s, p) { | |
var s = $(_s), | |
x; | |
if (s === null) { | |
return _null(); | |
} | |
x = head(s); | |
if (p(x)) { | |
return _(function() { return cons(x, filter(tail(s), p)); }); | |
} | |
return filter(tail(s), p); | |
} | |
function concat(_s, t) { | |
var s = $(_s); | |
if (s === null) { | |
return t; | |
} | |
return _(function() { return cons(head(s), concat(tail(s), t)); }); | |
} | |
function seq() { | |
var start = arguments.length>0 ? arguments[0] : 0, | |
step = arguments.length>1 ? arguments[1] : 1; | |
return _(function() { return cons(start, seq(start + step, step)); }); | |
} | |
function take(_s, howMany) { | |
var s; | |
if (howMany <= 0) { | |
return _null(); | |
} | |
s = $(_s); | |
if (s === null) { | |
return _null(); | |
} | |
return _(function() { return cons(head(s), take(tail(s), howMany - 1)); }); | |
} | |
function drop$(_s, howMany) { | |
var s = $(_s); | |
if (howMany === 0) { | |
return s; | |
} | |
if (s === null) { | |
return null; | |
} | |
return drop$(tail(s), howMany - 1); | |
} | |
function drop(_s, howMany) { | |
return _(function() { return drop$(_s, howMany); }); | |
} | |
function zip(_s, _t) { | |
var s = $(_s), | |
t; | |
if (s === null) { | |
return _null(); | |
} | |
t = $(_t); | |
if (t === null) { | |
return _null(); | |
} | |
return _(function() { return cons([head(s), head(t)], zip(tail(s), tail(t))); }); | |
} | |
function esrever(_s, r) { | |
var s = $(_s); | |
if (s === null) { | |
return r; | |
} | |
return esrever(tail(s), cons(head(s), _(function() { return r; }))); | |
} | |
function reverse(_s) { | |
return _(function() { return esrever(_s, null); }); | |
} | |
function list() { | |
return list$(arguments, 0); | |
} | |
function list$(args, i) { | |
var arg; | |
if (args.length <= i) { | |
return _null(); | |
} | |
arg = args[i]; | |
return _(function() { return cons(arg, list$(args, i+1)); }); | |
} | |
return { | |
_: _, | |
$: $, | |
lazy: lazy, | |
cons: cons, | |
head: head, | |
tail: tail, | |
each: each, | |
map: map, | |
concat: concat, | |
reverse: reverse, | |
seq: seq, | |
zip: zip, | |
take: take, | |
drop: drop, | |
filter: filter, | |
list: list | |
}; | |
})(); | |
var each = slothful.each, | |
map = slothful.map, | |
concat = slothful.concat, | |
seq = slothful.seq, | |
zip = slothful.zip, | |
take = slothful.take, | |
reverse = slothful.reverse, | |
drop = slothful.drop, | |
filter = slothful.filter, | |
list = slothful.list, | |
$ = slothful.$, | |
_ = slothful._, | |
cons = slothful.cons | |
lazy = slothful.lazy; | |
each(concat(list('foo', 'bar', 'baz'), list('apple', 'banana', 'pear')), function(fruit) { print(fruit); }); | |
each( | |
drop( | |
filter( | |
zip( | |
reverse(take(seq(), 10)), | |
map( | |
take(seq(), 10), | |
function(n) { return n*2; } | |
) | |
), | |
function(pair) { | |
return pair[1] > 5; | |
}), | |
2), | |
function(pair) { print(pair); } | |
); | |
function compound(_base, transform) { | |
var base = $(_base), | |
_transformed = function() { return transform(base); }; | |
return _(function() { return cons(base, compound(_transformed, transform)); }); | |
} | |
function interest(rate) { | |
return function(base) { | |
return base * rate; | |
}; | |
} | |
function repay(amt) { | |
return function(base) { | |
return base - amt; | |
} | |
} | |
function ccDebt(repaying, interestOn) { | |
return function(base) { | |
return repaying(interestOn(base)); | |
} | |
} | |
each(take(compound(lazy(4000), ccDebt(repay(100), interest(1.01))), 40), function(val) { print(val); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment