Skip to content

Commit

Permalink
[samples] added foldl
Browse files Browse the repository at this point in the history
  • Loading branch information
dscape committed Feb 19, 2012
1 parent c4c99a0 commit 3c893a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
33 changes: 26 additions & 7 deletions samples/hof.js
Expand Up @@ -3,15 +3,18 @@ var map = require('../pattern')
, zip_with = require('../pattern')
, filtera = require('../pattern')
, maybe = require('../pattern')
, _, f, ac, l, l1, l2, cb, errcb
, foldl = require('../pattern')
, _, f, ac, l, l1, l2, cb, errcb, z
;

// error handling for conditional execution of functions
maybe(Error, _, errcb, cb, function (err, data, errcb, cb) { errcb(err); });
maybe(_, _, errcb, cb, function(err, data, errcb, cb) { cb(data); });

// map
map(f, [], ac, cb,
// map _ [] = []
// map f (x:xs) = f x : map f xs
map(_, [], ac, cb,
function map_done(f, l, ac, cb) { return cb(ac); });
map(f, l, ac, cb,
function map_catch_all(f, l, ac, cb) {
Expand All @@ -31,7 +34,11 @@ mapa(f, l, ac, cb,
});

// filter with async f
filtera(f, [], ac, cb, function filter_done(f, l, ac, cb) {
// filter _ [] = []
// filter p (x:xs)
// | p x = x : filter p xs
// | otherwise = filter p xs
filtera(_, [], ac, cb, function filter_done(f, l, ac, cb) {
return cb(null, ac);
});
filtera(f, l, ac, cb, function filter_catch_all(f, l, ac, cb) {
Expand All @@ -44,14 +51,26 @@ filtera(f, l, ac, cb, function filter_catch_all(f, l, ac, cb) {
});
});

// foldl
// foldl with async f, no errors possible
// foldl f z [] = z
// foldl f z (x:xs) = foldl f (f z x) xs
foldl(f, z, [], cb, function foldl_done(f, z, l, cb) { cb(z); });
foldl(f, z, l, cb, function foldl_all(f, z, l, cb) {
f(z, l.shift(), function foldl_f_async(new_z) {
foldl(f, new_z, l, cb);
});
});

zip_with(f, [], l2, ac, cb, function emptyl1(f, l1, l2, ac, cb) { cb(ac); });
zip_with(f, l1, [], ac, cb, function emptyl2(f, l1, l2, ac, cb) { cb(ac); });
// zipWith _ [] _ = []
// zipWith _ _ [] = []
// zipWith f (x:xs) (y:ys) = f x y : zipWith' f xs ys
zip_with(_, [], _, ac, cb, function emptyl1(f, l1, l2, ac, cb) { cb(ac); });
zip_with(_, _, [], ac, cb, function emptyl2(f, l1, l2, ac, cb) { cb(ac); });
zip_with(f, l1, l2, ac, cb, function all(f, l1, l2, ac, cb) {
ac.push(f(l1.shift(), l2.shift()));
zip_with(f, l1, l2, ac, cb);
});

module.exports =
{ map: map, map_async: mapa, zip_with: zip_with, filter_async: filtera };
{ map: map, map_async: mapa, zip_with: zip_with, filter_async: filtera
, foldl: foldl };
10 changes: 10 additions & 0 deletions samples/reverse_list_with_reduce.test.js
@@ -0,0 +1,10 @@
/* [ 1, 2, 3, 4, 5 ] */
var reduce = require('./hof').foldl;

function id(z, x, cb) {
setTimeout(
function() {z.unshift(x); cb(z);}, Math.ceil(Math.random() * 100)); }

function reserve (list, cb) { reduce(id, [], list, function(z){ cb(z); }); }

reserve([5,4,3,2,1], function (list) { console.error(list); });

0 comments on commit 3c893a4

Please sign in to comment.