92 lines
8.3 KiB
JSON
92 lines
8.3 KiB
JSON
|
|
{
|
||
|
|
"_args": [
|
||
|
|
[
|
||
|
|
{
|
||
|
|
"raw": "lazy@~1.0.11",
|
||
|
|
"scope": null,
|
||
|
|
"escapedName": "lazy",
|
||
|
|
"name": "lazy",
|
||
|
|
"rawSpec": "~1.0.11",
|
||
|
|
"spec": ">=1.0.11 <1.1.0",
|
||
|
|
"type": "range"
|
||
|
|
},
|
||
|
|
"/home/httpd/html/mojo.bateworld.com/public_html/html5-chat/nodeMulti/node_modules/nssocket"
|
||
|
|
]
|
||
|
|
],
|
||
|
|
"_from": "lazy@>=1.0.11 <1.1.0",
|
||
|
|
"_id": "lazy@1.0.11",
|
||
|
|
"_inCache": true,
|
||
|
|
"_location": "/lazy",
|
||
|
|
"_npmUser": {
|
||
|
|
"name": "pkrumins",
|
||
|
|
"email": "peteris.krumins@gmail.com"
|
||
|
|
},
|
||
|
|
"_npmVersion": "1.2.14",
|
||
|
|
"_phantomChildren": {},
|
||
|
|
"_requested": {
|
||
|
|
"raw": "lazy@~1.0.11",
|
||
|
|
"scope": null,
|
||
|
|
"escapedName": "lazy",
|
||
|
|
"name": "lazy",
|
||
|
|
"rawSpec": "~1.0.11",
|
||
|
|
"spec": ">=1.0.11 <1.1.0",
|
||
|
|
"type": "range"
|
||
|
|
},
|
||
|
|
"_requiredBy": [
|
||
|
|
"/nssocket"
|
||
|
|
],
|
||
|
|
"_resolved": "https://registry.npmjs.org/lazy/-/lazy-1.0.11.tgz",
|
||
|
|
"_shasum": "daa068206282542c088288e975c297c1ae77b690",
|
||
|
|
"_shrinkwrap": null,
|
||
|
|
"_spec": "lazy@~1.0.11",
|
||
|
|
"_where": "/home/httpd/html/mojo.bateworld.com/public_html/html5-chat/nodeMulti/node_modules/nssocket",
|
||
|
|
"author": {
|
||
|
|
"name": "Peteris Krumins",
|
||
|
|
"email": "peteris.krumins@gmail.com",
|
||
|
|
"url": "http://www.catonmat.net"
|
||
|
|
},
|
||
|
|
"bugs": {
|
||
|
|
"url": "https://github.com/pkrumins/node-lazy/issues"
|
||
|
|
},
|
||
|
|
"dependencies": {},
|
||
|
|
"description": "Lazy lists for node",
|
||
|
|
"devDependencies": {
|
||
|
|
"expresso": ">=0.7.5"
|
||
|
|
},
|
||
|
|
"directories": {},
|
||
|
|
"dist": {
|
||
|
|
"shasum": "daa068206282542c088288e975c297c1ae77b690",
|
||
|
|
"tarball": "https://registry.npmjs.org/lazy/-/lazy-1.0.11.tgz"
|
||
|
|
},
|
||
|
|
"engines": {
|
||
|
|
"node": ">=0.2.0"
|
||
|
|
},
|
||
|
|
"homepage": "https://github.com/pkrumins/node-lazy#readme",
|
||
|
|
"keywords": [
|
||
|
|
"lazy lists",
|
||
|
|
"functional"
|
||
|
|
],
|
||
|
|
"license": {
|
||
|
|
"type": "MIT"
|
||
|
|
},
|
||
|
|
"main": "./lazy.js",
|
||
|
|
"maintainers": [
|
||
|
|
{
|
||
|
|
"name": "pkrumins",
|
||
|
|
"email": "peteris.krumins@gmail.com"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"name": "lazy",
|
||
|
|
"optionalDependencies": {},
|
||
|
|
"readme": "Lazy lists for node\n===================\n\n\n# Table of contents:\n\n[Introduction](#Introduction)\n \n[Documentation](#Documentation)\n\n<a name=\"Introduction\" />\n# Introduction\nLazy comes really handy when you need to treat a stream of events like a list.\nThe best use case currently is returning a lazy list from an asynchronous\nfunction, and having data pumped into it via events. In asynchronous\nprogramming you can't just return a regular list because you don't yet have\ndata for it. The usual solution so far has been to provide a callback that gets\ncalled when the data is available. But doing it this way you lose the power of\nchaining functions and creating pipes, which leads to not that nice interfaces.\n(See the 2nd example below to see how it improved the interface in one of my\nmodules.)\n\nCheck out this toy example, first you create a Lazy object:\n```javascript\n var Lazy = require('lazy');\n\n var lazy = new Lazy;\n lazy\n .filter(function (item) {\n return item % 2 == 0\n })\n .take(5)\n .map(function (item) {\n return item*2;\n })\n .join(function (xs) {\n console.log(xs);\n });\n```\n\nThis code says that 'lazy' is going to be a lazy list that filters even\nnumbers, takes first five of them, then multiplies all of them by 2, and then\ncalls the join function (think of join as in threads) on the final list.\n\nAnd now you can emit 'data' events with data in them at some point later,\n```javascript\n [0,1,2,3,4,5,6,7,8,9,10].forEach(function (x) {\n lazy.emit('data', x);\n });\n```\n\nThe output will be produced by the 'join' function, which will output the\nexpected [0, 4, 8, 12, 16].\n\nAnd here is a real-world example. Some time ago I wrote a hash database for\nnode.js called node-supermarket (think of key-value store except greater). Now\nit had a similar interface as a list, you could .forEach on the stored\nelements, .filter them, etc. But being asynchronous in nature it lead to the\nfollowing code, littered with callbacks and temporary lists:\n```javascript\n var Store = require('supermarket');\n\n var db = new Store({ filename : 'users.db', json : true });\n\n var users_over_20 = [];\n db.filter(\n function (user, meta) {\n // predicate function\n return meta.age > 20;\n },\n function (err, user, meta) {\n // function that gets executed when predicate is true\n if (users_over_20.length < 5)\n users_over_20.push(meta);\n },\n function () {\n // done function, called when all records have been filtered\n\n // now do something with users_over_20\n }\n )\n```\nThis code selects first five users who are over 20 years old and stores them\nin users_over_20.\n\nBut now we changed the node-supermarket interface to return lazy lists, and\nthe code became:\n```javascript\n var Store = require('supermarket');\n\n var db = new Store({ filename : 'users.db', json : true });\n\n db.filter(function (user, meta) {\n return meta.age > 20;\n })\n .take(5)\n .join(function (xs) {\n // xs contains the first 5 users who are over 20!\n });\n```\nThis is so much nicer!\n\nHere is the latest feature: .lines. Given a stream of data that has \\n's in it,\n.lines converts that into a list of lines.\n\nHere is an example from node-iptables that I wrote the other week,\n```javascript\n var Lazy = require('lazy');\n var spawn = require('child_process').spawn;\n var iptables = spawn('iptables', ['-L', '-n', '-v']);\n\n Lazy(iptables.stdout)\n .lines\n .map(String)\n .skip(2) // skips the two lines that are iptables header\n .map(function (line) {\n // packets, bytes, target, pro, opt, in, out, src, dst, opts\n var fields = line.trim().split(/\\s+/, 9);\n return {\n parsed : {\n packets : fields[0],\n bytes : fields[1],\n target : fields[2],\n
|
||
|
|
"readmeFilename": "README.md",
|
||
|
|
"repository": {
|
||
|
|
"type": "git",
|
||
|
|
"url": "git+ssh://git@github.com/pkrumins/node-lazy.git"
|
||
|
|
},
|
||
|
|
"scripts": {
|
||
|
|
"test": "expresso"
|
||
|
|
},
|
||
|
|
"version": "1.0.11"
|
||
|
|
}
|