Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/refractalize/zo
asynchronous query language, for the usual functional list processing functions: map, select, reduce, but async-friendly
https://github.com/refractalize/zo
Last synced: 3 months ago
JSON representation
asynchronous query language, for the usual functional list processing functions: map, select, reduce, but async-friendly
- Host: GitHub
- URL: https://github.com/refractalize/zo
- Owner: refractalize
- Created: 2011-02-28T22:41:45.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2011-07-15T08:20:37.000Z (over 13 years ago)
- Last Synced: 2024-09-15T17:14:09.540Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 102 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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) {
process.nextTick(function () {
mapTo(item + 1);
});
})
.results(function (mappedItems) {
console.log(mappedItems);
});Produces: `[ 2, 3, 4 ]`
## select
zo([1, 2, 3])
.select(function (item, selectIf) {
process.nextTick(function () {
selectIf(item > 1);
});
})
.results(function (selectedItems) {
console.log(selectedItems);
});Produces: `[ 2, 3 ]`
## reduce (also foldl)
zo([1, 2, 3])
.reduce(0, function (sum, item, reduceInto) {
process.nextTick(function () {
reduceInto(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) {
process.nextTick(function () {
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');
});
});