Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

p

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p - npm Package Compare versions

Comparing version
0.0.1
to
0.0.2
+121
README.md
<a name="pattern"/>
# pattern
`pattern` is a way to do pattern matching in javascript that helps you do asynchronous iterations
``` js
// 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]);
```
the first pattern in `pattern` sets the arity of the function to execute
``` js
// first call sets arity #1
// when this condition is met it logs the message done
insert_all([], function () { console.log('done'); });
```
then we normally register the iteration pattern:
``` js
// var _; was set in the top, value is undefined
insert_all(_, function (l) {
```
if you then call `insert_all` where the argument count matches arity, `pattern` knows its time to execute
``` js
// one argument, arity #1. run forest, run.
insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
```
[this] is the code you would normally write to do the same thing in javascript
<a name="installation"/>
# installation
<a name="node"/>
## node.js
1. install [npm]
2. `npm install p`
3. `var p = require('p');`
<a name="samples"/>
## samples
there are samples in the `samples` directory. check them out
<a name="disclaimer"/>
## disclaimer
if you are not familiar with `haskell` and you love your javascript object oriented code you better look away right now. this pretty much breaks everything you love and care about in javascript
`pattern` was made so i could learn some more javascript. it's slow, and certainly not web scale
this software does not obey laws, common best practices, or even common sense
it does everything that is wrong in javascript; or at least attempts to (suggestions are welcome)
i'll probably still use it anyway
<a name="roadmap"/>
# roadmap
[pointfree] style (**note** i'm just kidding)
<a name="contribute"/>
# contribute
everyone is welcome to contribute. patches, bug-fixes, new features
1. create an [issue][issues] so the community can comment on your idea
2. fork `pattern`
3. create a new branch `git checkout -b feature_name`
4. create tests for the changes you made
5. make sure you pass both existing and newly inserted tests
6. commit your changes
7. push to your branch `git push origin feature_name`
8. create an pull request
<a name="meta"/>
# meta
* code: `git clone git://github.com/dscape/p.git`
* home: <http://github.com/dscape/p>
* bugs: <http://github.com/dscape/p/issues>
* build: [![build status](https://secure.travis-ci.org/dscape/p.png)](http://travis-ci.org/dscape/pattern)
`(oO)--',-` in [caos]
<a name="license"/>
# license
copyright 2012 nuno job <nunojob.com> (oO)--',--
licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at
http://www.apache.org/licenses/LICENSE-2.0
unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.
[npm]: http://npmjs.org
[issues]: http://github.com/dscape/p/issues
[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
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], []);
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], []);
map(function plusone(x) { return x+1; }, [1,2,3], []);
var insert_all = require('../pattern'), _;
// pretending we are doing an async call
function insert_element(data, callback) {
setTimeout(function() { callback(data); },
Math.ceil(Math.random() * 1000));
}
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);
});
});
insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
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]);
+6
-18
{ "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"]
, "devDependencies" : { "mocha": "*", "should": "*" }
, "scripts" :
{ "test" :
"./node_modules/mocha/bin/mocha -r should -t 10000 -s 2000 test/index.js"
}
, "engines" :
{ "node" : ">=0.4.0"
, "firefox" : ">=0.8.0"
, "chrome" : ">=16.0.912"
}
, "keywords" : ["pattern matching", "pattern", "flow", "async"]
, "engines" : { "node" : ">=0.4.0" }
}

@@ -1,5 +0,40 @@

;(function (pattern) {
pattern = function pattern() {
console.log('a');
};
})(typeof exports === "undefined" ? pattern = {} : exports);
(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(typeof pattern === 'object')
return JSON.stringify(pattern) === JSON.stringify(value);
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 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(; 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
log(' ✔ ', s[i], '===', arguments[i]);
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) { 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.
if(require.cache[module.id]) // make isaac nervous
delete require.cache[module.id]; // make everyone hate me
module.exports = exports = p; // export our ""constructor""
})();