Last active
January 14, 2016 20:42
-
-
Save rizo/dbc777e6883f0e1db309 to your computer and use it in GitHub Desktop.
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
| // List Processor | |
| function map(f, seq) { | |
| return function(step) { | |
| seq (function(x) { | |
| return step (f(x)); | |
| }); | |
| }; | |
| } | |
| function filter(p, seq) { | |
| return function(step) { | |
| seq (function(x) { | |
| if (p(x)) { | |
| return step(x); | |
| } | |
| }); | |
| }; | |
| } | |
| function of_list(list) { | |
| return function(step) { | |
| for (var i = 0; i < list.length; i++) { | |
| if (step(list[i])) { | |
| console.log("consumer is done, closing..."); | |
| break; | |
| } | |
| } | |
| console.log("producer is empty, closing..."); | |
| }; | |
| } | |
| function take(n, seq) { | |
| var count = 0; | |
| return function(step) { | |
| seq(function(x) { | |
| if (count === n) { | |
| return true; | |
| } else { | |
| count++; | |
| return step(x); | |
| } | |
| }); | |
| }; | |
| } | |
| // Consumers | |
| function fold(f, init, seq) { | |
| var r = init; | |
| seq (function(x) { | |
| r = f(r, x); | |
| }); | |
| return r; | |
| } | |
| function to_list(seq) { | |
| return fold(function(list, x) { | |
| list.push(x); | |
| return list; | |
| }, [], seq); | |
| } | |
| var seq1 = of_list([1, 2, 3, 4, 5, 6, 7]); | |
| console.log(to_list(take(3, map(function(x) { return x + 100; }, seq1)))); |
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
| "use strict"; | |
| function count(_n, k) { | |
| while(/* true */1) { | |
| var n = _n; | |
| if (k(n)) { | |
| _n = n + 1; | |
| } | |
| else { | |
| return 0; | |
| } | |
| }; | |
| } | |
| function of_list(_l, k) { | |
| while(/* true */1) { | |
| var l = _l; | |
| if (l) { | |
| if (k(l[1])) { | |
| _l = l[2]; | |
| } | |
| else { | |
| return /* () */0; | |
| } | |
| } | |
| else { | |
| return /* () */0; | |
| } | |
| }; | |
| } | |
| function to_list(seq) { | |
| var r = [ | |
| 0, | |
| /* [] */0 | |
| ]; | |
| seq(function (x) { | |
| r[1] = [ | |
| /* :: */0, | |
| x, | |
| r[1] | |
| ]; | |
| return /* true */1; | |
| }); | |
| return r[1]; | |
| } | |
| function take(n, seq, k) { | |
| var count = [ | |
| 0, | |
| 0 | |
| ]; | |
| return seq(function (x) { | |
| return count[1] === n ? /* false */0 : (++ count[1], k(x)); | |
| }); | |
| } | |
| function fold(f, acc, seq) { | |
| var r = [ | |
| 0, | |
| acc | |
| ]; | |
| seq(function (elt) { | |
| r[1] = f(r[1], elt); | |
| return /* true */1; | |
| }); | |
| return r[1]; | |
| } | |
| function map(f, seq, k) { | |
| return seq(function (x) { | |
| return k(f(x)); | |
| }); | |
| } | |
| function filter(p, seq, k) { | |
| return seq(function (x) { | |
| return p(x) ? k(x) : /* true */1; | |
| }); | |
| } | |
| exports.count = count; | |
| exports.of_list = of_list; | |
| exports.to_list = to_list; | |
| exports.take = take; | |
| exports.fold = fold; | |
| exports.map = map; | |
| exports.filter = filter; | |
| /* No side effect */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment