Skip to content
Browse files

documentation, package for npm

  • Loading branch information...
1 parent b084c2e commit bbdf7e33744afec2e051d419f26d800a2b12c7ee @refractalize committed Feb 28, 2011
Showing with 172 additions and 5 deletions.
  1. +1 −0 .npmignore
  2. +101 −0 README.js
  3. +10 −0 docs/each.js
  4. +25 −0 docs/io.js
  5. +9 −0 docs/map.js
  6. +9 −0 docs/reduce.js
  7. +9 −0 docs/select.js
  8. +8 −5 package.json
View
1 .npmignore
@@ -0,0 +1 @@
+test.js
View
101 README.js
@@ -0,0 +1,101 @@
+# zo
+
+What _is_ zo?
+
+Zo is a asynchronous query language providing the usual functional programming list operators like `map`, `select` and `reduce`, but all in an async-friendly style, so they're ready to use while performing async IO operations in node.js.
+
+# Installation
+
+ npm install zo
+
+# Operation
+
+## require
+
+ var zo = require('zo').zo;
+
+## map
+
+ zo([1, 2, 3])
+ .map(function (item, mapTo) {
+ mapTo(item + 1);
+ })
+ .results(function (mappedItems) {
+ console.log(mappedItems);
+ });
+
+Produces: `[ 2, 3, 4 ]`
+
+## select
+
+ zo([1, 2, 3])
+ .select(function (item, selectIf) {
+ selectIf(item > 1);
+ })
+ .results(function (selectedItems) {
+ console.log(selectedItems);
+ });
+
+Produces: `[ 2, 3 ]`
+
+## reduce (also `foldl`)
+
+ zo([1, 2, 3])
+ .reduce(0, function (sum, item, foldInto) {
+ foldInto(sum + item);
+ })
+ .results(function (sum) {
+ console.log(sum);
+ });
+
+Produces: `6`
+
+See also `reduceRight`, which is a synonym for `foldr`
+
+## each
+
+ zo([1, 2, 3])
+ .each(function (item, done) {
+ console.log('item: ' + item);
+ done();
+ })
+ .results(function (items) {
+ console.log('count: ' + items.length);
+ });
+
+Produces:
+
+ item: 1
+ item: 2
+ item: 3
+ count: 3
+
+## Async IO
+
+But the whole point is when you mix it with async IO:
+
+ var fs = require('fs');
+ var zo = require('zo').zo;
+
+ fs.readdir('.', function (err, filesAndDirectories) {
+ zo(filesAndDirectories)
+ .map(function (file, mapTo) {
+ fs.stat(file, function (err, stat) {
+ mapTo({file: file, stat: stat});
+ });
+ })
+ .select(function (fileAndStat, selectIf) {
+ selectIf(fileAndStat.stat.isFile() && !/^\./.test(fileAndStat.file));
+ })
+ .map(function (fileAndStat, mapTo) {
+ mapTo(fileAndStat.file + ': ' + fileAndStat.stat.size + ' bytes');
+ })
+ .each(function (fileWithSize, done) {
+ console.log(fileWithSize);
+ done();
+ })
+ .results(function (files) {
+ console.log();
+ console.log(files.length + ' files');
+ });
+ });
View
10 docs/each.js
@@ -0,0 +1,10 @@
+var zo = require('zo').zo;
+
+zo([1, 2, 3])
+ .each(function (item, done) {
+ console.log('item: ' + item);
+ done();
+ })
+ .results(function (items) {
+ console.log('count: ' + items.length);
+ });
View
25 docs/io.js
@@ -0,0 +1,25 @@
+var fs = require('fs');
+var zo = require('zo').zo;
+
+fs.readdir('.', function (err, filesAndDirectories) {
+ zo(filesAndDirectories)
+ .map(function (file, mapTo) {
+ fs.stat(file, function (err, stat) {
+ mapTo({file: file, stat: stat});
+ });
+ })
+ .select(function (fileAndStat, selectIf) {
+ selectIf(fileAndStat.stat.isFile() && !/^\./.test(fileAndStat.file));
+ })
+ .map(function (fileAndStat, mapTo) {
+ mapTo(fileAndStat.file + ': ' + fileAndStat.stat.size + ' bytes');
+ })
+ .each(function (fileWithSize, done) {
+ console.log(fileWithSize);
+ done();
+ })
+ .results(function (files) {
+ console.log();
+ console.log(files.length + ' files');
+ });
+});
View
9 docs/map.js
@@ -0,0 +1,9 @@
+var zo = require('zo').zo;
+
+zo([1, 2, 3])
+ .map(function (item, mapTo) {
+ mapTo(item + 1);
+ })
+ .results(function (mappedItems) {
+ console.log(mappedItems);
+ });
View
9 docs/reduce.js
@@ -0,0 +1,9 @@
+var zo = require('zo').zo;
+
+zo([1, 2, 3])
+ .reduce(0, function (sum, item, foldInto) {
+ foldInto(sum + item);
+ })
+ .results(function (sum) {
+ console.log(sum);
+ });
View
9 docs/select.js
@@ -0,0 +1,9 @@
+var zo = require('zo').zo;
+
+zo([1, 2, 3])
+ .select(function (item, selectIf) {
+ selectIf(item > 1);
+ })
+ .results(function (selectedItems) {
+ console.log(selectedItems);
+ });
View
13 package.json
@@ -3,11 +3,14 @@
"version": "0.0.1",
"description": "asynchronous query language, for the usual functional list processing functions: map, select, reduce, but async-friendly",
"maintainers": [
- "name": "Tim Macfarlane",
- "email": "timmacfarlane@gmail.com",
- "web": "http://blog.refractalize.org"
+ {
+ "name": "Tim Macfarlane",
+ "email": "timmacfarlane@gmail.com",
+ "web": "http://blog.refractalize.org"
+ }
],
- "dependencies": [
+ "main": "zo.js",
+ "dependencies": {
"underscore": "1.1.4"
- ]
+ }
}

0 comments on commit bbdf7e3

Please sign in to comment.
Something went wrong with that request. Please try again.