Permalink
Browse files

Merge remote branch 'origin/master'

  • Loading branch information...
refractalize committed Mar 21, 2011
2 parents a228df9 + 06501ae commit 4b6fc8de469267c2ff2d9642db1352ce5a99dfd4
Showing with 85 additions and 25 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 package.json
  3. +59 −0 specs.js
  4. +24 −24 zo.js
View
@@ -0,0 +1 @@
+.*.swp
View
@@ -1,6 +1,6 @@
{
"name": "zo",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "asynchronous query language, for the usual functional list processing functions: map, select, reduce, but async-friendly",
"maintainers": [
{
View
@@ -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));
+ });
+});
View
48 zo.js
@@ -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 {

0 comments on commit 4b6fc8d

Please sign in to comment.