| /* yeah! */ | ||
| var filter = require('./hof').filter_async; | ||
| // simulating an async function that could throw | ||
| function gotBang(x, cb) { | ||
| setTimeout(function() { | ||
| try { | ||
| if (x.msg.indexOf('!') !== 1) { | ||
| process.stderr.write(x.msg); | ||
| cb(null, x); } | ||
| } catch (e) { cb(e); } | ||
| }, Math.ceil(Math.random() * 300)); | ||
| } | ||
| filter(gotBang, | ||
| [ {msg: "y"} | ||
| , {msg: "e"} | ||
| , {msg: "a"} | ||
| , {msg: "h"} | ||
| , {msg: "!"} | ||
| , null | ||
| , {msg: "?"} | ||
| ], [], function(err,data) { | ||
| if(!err) { console.error('I will never get executed!'); } }); |
+1
-1
| { "name" : "p" | ||
| , "description" : "pattern matching in javascript for asyncronous iteration" | ||
| , "author" : "nuno job <nunojobpinto@gmail.com> (http://nunojob.com/)" | ||
| , "version" : "0.0.4" | ||
| , "version" : "0.1.0" | ||
| , "main" : "./pattern.js" | ||
@@ -6,0 +6,0 @@ , "homepage" : "https://github.com/dscape/p" |
+2
-0
@@ -5,2 +5,4 @@ (function () { // stack refers to registered patterns | ||
| function match(pattern, value) { | ||
| if(pattern === Error) { | ||
| return value && value.name.indexOf('Error') !== -1;} | ||
| if(pattern === undefined) return true; | ||
@@ -7,0 +9,0 @@ if(typeof pattern === 'object') |
+25
-4
| var map = require('../pattern') | ||
| , mapa = require('../pattern') | ||
| , zip_with = require('../pattern') | ||
| , _, f, ac, l, l1, l2, cb | ||
| , filtera = require('../pattern') | ||
| , maybe = require('../pattern') | ||
| , _, f, ac, l, l1, l2, cb, errcb | ||
| ; | ||
| // high order functions | ||
| // error handling for conditional execution of functions | ||
| maybe(Error, _, errcb, cb, function (err, data, errcb, cb) { errcb(err); }); | ||
| maybe(_, _, errcb, cb, function(err, data, errcb, cb) { cb(data); }); | ||
| // map | ||
| map(f, [], ac, cb, | ||
@@ -16,2 +22,3 @@ function map_done(f, l, ac, cb) { return cb(ac); }); | ||
| // map with async f, no error possible | ||
| mapa(f, [], ac, cb, | ||
@@ -27,3 +34,16 @@ function map_done(f, l, ac, cb) { return cb(ac); }); | ||
| // filter | ||
| // filter with async f | ||
| filtera(f, [], ac, cb, function filter_done(f, l, ac, cb) { | ||
| return cb(null, ac); | ||
| }); | ||
| filtera(f, l, ac, cb, function filter_catch_all(f, l, ac, cb) { | ||
| var head = l.shift(); | ||
| f(head, function(err, ok) { | ||
| maybe(err, ok, cb, function (ok) { | ||
| if (ok) ac.push(head); | ||
| filtera(f, l, ac, cb); // l is now tail | ||
| }); | ||
| }); | ||
| }); | ||
| // foldl | ||
@@ -38,2 +58,3 @@ | ||
| module.exports = { map: map, map_async: mapa, zip_with: zip_with }; | ||
| module.exports = | ||
| { map: map, map_async: mapa, zip_with: zip_with, filter_async: filtera }; |
12367
11.93%16
6.67%221
24.16%