Skip to content

Commit

Permalink
fixed async bug for reduce - issue 2
Browse files Browse the repository at this point in the history
  • Loading branch information
refractalize committed Mar 21, 2011
1 parent 049dd2e commit e5d1123
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
59 changes: 59 additions & 0 deletions specs.js
@@ -0,0 +1,59 @@
var assert = require('assert');
var zo = require('./zo').zo;
require('../cupoftea/cupoftea');

spec('integers', function () {
var i = 0;

spec('should be zero', function () {
assert.equal(i, 0);
});

spec('should be greater than one', function () {
assert.equal(i, 0);
});
});

spec('reduce left', function () {
var assertCorrectResult = function (result) {
assert.deepEqual(result, [1, 2, 3, 4]);
};

spec('async', function () {
zo([1, 2, 3, 4]).reduce([], function(memo, item, into) {
process.nextTick(function() {
memo.push(item);
into(memo);
});
}).results(shouldCall(assertCorrectResult));
});

spec('sync', function () {
zo([1, 2, 3, 4]).reduce([], function(memo, item, into) {
memo.push(item);
into(memo);
}).results(shouldCall(assertCorrectResult));
});
});

spec('reduce right', function () {
var assertCorrectResult = function (result) {
assert.deepEqual(result, [4, 3, 2, 1]);
};

spec('async', function () {
zo([1, 2, 3, 4]).reduceRight([], function(memo, item, into) {
process.nextTick(function() {
memo.push(item);
into(memo);
});
}).results(shouldCall(assertCorrectResult));
});

spec('sync', function () {
zo([1, 2, 3, 4]).reduceRight([], function(memo, item, into) {
memo.push(item);
into(memo);
}).results(shouldCall(assertCorrectResult));
});
});
48 changes: 24 additions & 24 deletions zo.js
Expand Up @@ -32,38 +32,38 @@ var zo = function (items, pipeline) {
return zo(items, pipeline);
};

var foldElement = function (first, folder, mapItems) {
var foldl = function (first, folder) {
pipeline.push(function (items, next) {
var n = items.length;
var foldedResult = first;

if (n > 0) {
_(mapItems(items)).each(function (item) {
folder(foldedResult, item, function (folded) {
foldedResult = folded;
n--;
if (n == 0) {
next(foldedResult);
}
var xyz = function (foldedResult, index, items, next) {
if (index >= items.length) {
next(foldedResult);
} else {
folder(foldedResult, items[index], function (folded) {
xyz(folded, index + 1, items, next);
});
});
} else {
next(foldedResult);
}
});
return zo(items, pipeline);
};
}
};

var foldl = function (first, folder) {
return foldElement(first, folder, function (items) {
return items;
xyz(first, 0, items, next);
});
return zo(items, pipeline);
};

var foldr = function (first, folder) {
return foldElement(first, folder, function (items) {
return _(items).reverse();
pipeline.push(function (items, next) {
var xyz = function (foldedResult, index, items, next) {
if (index < 0) {
next(foldedResult);
} else {
folder(foldedResult, items[index], function (folded) {
xyz(folded, index - 1, items, next);
});
}
};

xyz(first, items.length - 1, items, next);
});
return zo(items, pipeline);
};

return {
Expand Down

0 comments on commit e5d1123

Please sign in to comment.