Permalink
Browse files

[dist] version 0.0.2 runs map

  • Loading branch information...
dscape committed Feb 18, 2012
1 parent 26ac1c9 commit 3d7e92c7b6db35d9948064ef7d589f9fc00872d5
Showing with 94 additions and 128 deletions.
  1. +23 −47 README.md
  2. +6 −13 package.json
  3. +12 −18 pattern.js
  4. 0 samples/errors.js
  5. +5 −4 samples/map.js
  6. +11 −0 samples/map_async.js
  7. +8 −14 samples/nodetuts.js
  8. +29 −0 samples/two_patterns.js
  9. +0 −32 samples/twopatterns.js
View
@@ -1,60 +1,43 @@
<a name="pattern"/>
# pattern
-`pattern` is a way to do pattern matching in javascript that helps you do asynchronous iterations.
+`pattern` is a way to do pattern matching in javascript that helps you do asynchronous iterations
``` js
-var insert_all = require('p')
- , _
- ;
-
-// pretending we are doing an async call
-function insert_element(data, callback) {
- setTimeout(function() { callback(null, data); }, 100);
-}
-
-insert_all([], _, function (cb) { cb(); });
-insert_all(function (l,cb) {
- var elem = l.shift(); // head
- insert_element(elem, function(err, elem) {
- if(err) { return cb(err); }
- console.log(elem + ' inserted');
- insert_all(l, cb);
+// check `samples/nodetuts.js` for working code
+insert_all([], function () { console.log('done'); });
+insert_all(_, function (l) {
+ insert_element(l.shift(), function (elem) {
+ console.log('', elem);
+ insert_all(l);
});
-});
-
-insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function () {
- console.log('done');
});
+
+insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
```
-the first pattern in `pattern` sets the arity of the function to execute. if you then give it anything with those numbers of arguments it will assume it must be the function.
+the first pattern in `pattern` sets the arity of the function to execute
``` js
-// first call sets arity #2
-insert_all([], _, function (cb) { cb(); });
-// _ is an undefined variable we declared on the top
-// the same as passing undefined
-// will match anything
+// first call sets arity #1
+// when this condition is met it logs the message done
+insert_all([], function () { console.log('done'); });
```
-the last thing to be registered should be the catch all
-
+then we normally register the iteration pattern:
``` js
-// same as insert_all(_, _, function ...
-insert_all(function (l,cb) {
+// var _; was set in the top, value is undefined
+insert_all(_, function (l) {
```
-when `pattern` finds a invocation where arguments.length matches arity it runs the functions.
+if you then call `insert_all` where the argument count matches arity, `pattern` knows its time to execute
``` js
-// two arguments, arity #2. lets run!
-insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function () {
- console.log('done');
-});
+// one argument, arity #1. run forest, run.
+insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
```
-[this] is what you normally would do.
+[this] is the code you would normally write to do the same thing in javascript
<a name="installation"/>
# installation
@@ -66,16 +49,10 @@ insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function () {
2. `npm install p`
3. `var p = require('p');`
-<a name="browser"/>
-## browser
-
-1. minimize p.js
-2. load it into your webpage
-
<a name="samples"/>
## samples
-there are samples in the `samples` directory. check them out.
+there are samples in the `samples` directory. check them out
<a name="disclaimer"/>
## disclaimer
@@ -93,9 +70,7 @@ i'll probably still use it anyway
<a name="roadmap"/>
# roadmap
-* point free programming **note** i'm just kidding.
-
-check [issues]
+[pointfree] style (**note** i'm just kidding)
<a name="contribute"/>
# contribute
@@ -143,3 +118,4 @@ limitations under the license.
[caos]: http://caos.di.uminho.pt/
[samples]: https://github.com/dscape/p/tree/master/samples
[this]: https://gist.github.com/00663e475092e55ac66c#file_howitis.js
+[pointfree]: http://www.haskell.org/haskellwiki/Pointfree
View
@@ -1,18 +1,11 @@
{ "name" : "p"
-, "description" : "Pattern matching in javascript for asyncronous iteration"
-, "author" : "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com/)"
-, "version" : "0.0.1"
+, "description" : "pattern matching in javascript for asyncronous iteration"
+, "author" : "nuno job <nunojobpinto@gmail.com> (http://nunojob.com/)"
+, "version" : "0.0.2"
, "main" : "./pattern.js"
, "homepage" : "https://github.com/dscape/p"
-, "repository" :
- { "type" : "git"
- , "url" : "http://github.com/dscape/p.git"
- }
+, "repository" : { "type": "git", "url": "http://github.com/dscape/p.git" }
, "bugs" : "http://github.com/dscape/p/issues"
-, "keywords" : ["pattern matching", "pattern", "flow", "async"]
-, "scripts" :
- { "test" :
- "./node_modules/mocha/bin/mocha -r should -t 10000 -s 2000 test/index.js"
- }
-, "engines" : { "node" : ">=0.4.0" }
+, "keywords" : ["pattern matching", "pattern", "flow", "async"]
+, "engines" : { "node" : ">=0.4.0" }
}
View
@@ -1,45 +1,39 @@
-(function () {
+(function () { // stack refers to registered patterns
var stack = [], arity, DEBUG = process.env.DEBUG;
function log() { if(DEBUG) console.log.apply(this,arguments); }
function match(pattern, value) {
- if(pattern === undefined)
- return true;
+ if(pattern === undefined) return true;
if(typeof pattern === 'object')
return JSON.stringify(pattern) === JSON.stringify(value);
- return pattern.toString() === value.toString();
- }
+ return pattern.toString() === value.toString(); }
function p() {
if(!arity) { arity = arguments.length-1; } // set arity in first invok.
if(arity===arguments.length) { // # arguments match arity, execute
- var ok = false;
+ var j = 0; // we need explicit control over j, cant be reset by :il
ol: for(var i=0; i<arguments.length; i++) { // for each argument
- il: for(var j=0; j<stack.length; j++) { // for pattern in the stack
+ il: for(; j<stack.length; j++) { // for pattern in the stack
var s = stack[j]; // get the current pattern
log('α ', i, j);
log('', s, s[i]);
log(' σ ', s.length);
log('', [].slice.call(arguments,0));
if(s.length > i) { // if there's something in this pos for pattern
if(match(s[i], arguments[i])) { // if we have a match
- ok = true; // set our flag to matching
log('', s[i], '===', arguments[i]);
- j=0;
if(arguments.length !== i+1) { log(''); continue ol; } }
else { // if it doesnt match try next pattern in stack
log('', s[i], '===', arguments[i]);
// dont break and set ok to false if this is the last element
- if(stack.length!==j+1) { log(''); ok = false; continue; } } }
- //if(stack.length === j+1) {
- // decide on the function base ok if its a match or not
- var f = ok ? arguments[arguments.length-1] : s[s.length-1];
- log(' ' + (ok ? 'λ' : 'ƒ'), f.name || f);
- // execute whatever is the last argument on last pattern of stack
- return f.apply(this, [].slice.call(arguments,0)); } } //}
+ if(stack.length!==j+1) { j=0; log(''); continue; } } }
+ var f = s[s.length-1];
+ log(' ' + (typeof f === 'function' ? 'ƒ' : 'λ'), f.name || f);
+ // execute whatever is the last argument on last pattern of stack
+ return (typeof f === 'function') ? // is there a callback?
+ f.apply(this, [].slice.call(arguments,0)) : null; } }
} else {
stack.push([].slice.call(arguments,0)); // initializing add pattern
log('', [].slice.call(arguments,0));
- }
- } // dont cache, each require is a new inst.
+ } } // dont cache, each require is a new inst.
if(require.cache[module.id]) // make isaac nervous
delete require.cache[module.id]; // make everyone hate me
module.exports = exports = p; // export our ""constructor""
View
No changes.
View
@@ -1,11 +1,12 @@
var map = require('../pattern')
- , _
+ , _, f, ac
;
-map(_, [], _, function stop(cb,ac) { return cb(ac); });
-map(function all(f, l, ac) {
+map(f, [], ac, function done(_,_,ac) { return console.log(ac); });
+map(f, _, ac, function all(f, l, ac) {
ac.push(f(l.shift())); // head
map(f, l, ac); // l is now tail
});
-map(function duplicate(x) { console.log(x); return x*2; }, [1,2,3], []);
+map(function duplicate(x) { console.log(x*2); return x*2; }, [1,2,3], []);
+map(function plusone(x) { return x+1; }, [1,2,3], []);
View
@@ -0,0 +1,11 @@
+var map = require('../pattern')
+ , _, f, ac
+ ;
+
+map(f, [], ac, function done(_,_,ac) { return console.log(ac); });
+map(f, _, ac, function all(f, l, ac) {
+ ac.push(f(l.shift())); // head
+ map(f, l, ac); // l is now tail
+});
+
+map(function duplicate(x) { console.log(x*2); return x*2; }, [1,2,3], []);
View
@@ -1,23 +1,17 @@
-var insert_all = require('../pattern')
- , _
- ;
+var insert_all = require('../pattern'), _;
// pretending we are doing an async call
function insert_element(data, callback) {
- setTimeout(function() { callback(null, data); },
+ setTimeout(function() { callback(data); },
Math.ceil(Math.random() * 1000));
}
-insert_all([], _, function finish(cb) { cb(); });
-insert_all(function catchall(l,cb) {
- var elem = l.shift(); // head
- insert_element(elem, function elem_cb(err, elem) {
- if(err) { return cb(err); }
+insert_all([], function done() { console.log('done'); });
+insert_all(_, function catchall(l) {
+ insert_element(l.shift(), function elem_cb(elem) {
console.log(elem + ' inserted');
- insert_all(l, cb);
+ insert_all(l);
});
-});
+});
-insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function done() {
- console.log('done');
-});
+insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
View
@@ -0,0 +1,29 @@
+var p1 = require('../pattern')
+ , p2 = require('../pattern')
+ , _
+ ;
+
+// pretending we are doing an async call
+function insert_element(data, callback) {
+ setTimeout(function() { callback(data); },
+ Math.ceil(Math.random() * 1000));
+}
+
+function done() { console.log('done'); }
+
+function any_generator (name,context) {
+ return function (l) {
+ insert_element(l.shift(), function(elem) {
+ console.log(elem + ' inserted in ' + name);
+ context(l);
+ });
+ };
+}
+
+p1([], done);
+p1(_, any_generator('p1', p1));
+p2([], done);
+p2(_, any_generator('p2', p2));
+
+p1([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+p2([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
View
@@ -1,32 +0,0 @@
-var p1 = require('../pattern')
- , p2 = require('../pattern')
- , _
- ;
-
-// pretending we are doing an async call
-function insert_element(data, callback) {
- setTimeout(function() { callback(null, data); },
- Math.ceil(Math.random() * 1000));
-}
-
-function simple_cb(cb) { cb(); }
-function done() { console.log('done'); }
-
-function any_generator (name,context) {
- return function (l,cb) {
- var elem = l.shift(); // head
- insert_element(elem, function(err, elem) {
- if(err) { return cb(err); }
- console.log(elem + ' inserted in ' + name);
- context(l, cb);
- });
- };
-}
-
-p1([], _, simple_cb);
-p1(any_generator('p1', p1));
-p2([], _, simple_cb);
-p2(any_generator('p2', p2));
-
-p1([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], done);
-p2([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], done);

0 comments on commit 3d7e92c

Please sign in to comment.