Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Using generators here so your mileage may vary
- */
- var makeIterable = function (iterator) {
- var iterable = {};
- iterable[Symbol.iterator] = iterator;
- return iterable;
- };
- /*
- Function.method('curry', function ( ) {
- var slice = Array.prototype.slice,
- args = slice.apply(arguments),
- that = this;
- return function ( ) {
- return that.apply(null, args.concat(slice.apply(arguments)));
- };
- });
- */
- var curry = function () {
- // TODO
- };
- var compose = function () {
- // TODO
- };
- // transformers
- var map = function (mapping, iterable) {
- return makeIterable ( function* () {
- for(let x of iterable) {
- yield mapping(x);
- }
- });
- };
- var filter = function (predicate, iterable) {
- return makeIterable ( function* () {
- for(let x of iterable) {
- if( predicate(x) ){
- yield x;
- }
- }
- });
- };
- var scan = function (accumulate, initValue, iterable) {
- // TODO
- };
- // terminators
- var collect = function (iterable) {
- var arr = [];
- for(let x of iterable) {
- arr.push(x);
- }
- return arr;
- }
- var fold = function (accumulate, initValue, iterable) {
- // TODO
- };
- var each = function (action, iterable) {
- // TODO
- };
- //
- // tests
- //
- var EXPECT_EQ = function (expected, actual) {
- // hacky way to compare stuff
- var e = JSON.stringify(expected);
- var a = JSON.stringify(actual);
- if (e !== a) {
- console.log('expected ' + e + ', got ' + a);
- }
- };
- EXPECT_EQ( [2, 4, 6], collect(map( (x) => x*2, [1, 2, 3])) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement