Permalink
Browse files

move fns after sequence params

  • Loading branch information...
1 parent 8217e02 commit 047ea34301d2a7e387f2012d6a87557787f24f02 @azer committed Jun 9, 2012
Showing with 37 additions and 22 deletions.
  1. +1 −0 .gitignore
  2. +32 −21 lib/functools.js
  3. +4 −1 package.json
View
@@ -1 +1,2 @@
node_modules
+test/logs
View
@@ -1,14 +1,24 @@
var functools = (function(undefined){
- function isArray(el){
- return Object.prototype.toString.call(el) == '[object Array]';
+ var slice = Array.prototype.slice;
+
+ function isArray(obj){
+ return Object.prototype.toString.call(obj) == '[object Array]';
+ }
+
+ function objectKeys(obj){
+ var keys = [], key;
+ for(key in obj){
+ keys.push( key );
+ }
+ return keys;
}
/**
* Function composition implementation
*/
function compose(/* functions */){
- var fns = Array.prototype.slice.call(arguments);
+ var fns = slice.call(arguments);
return function(initialValue){
return reduce(function(f,g){
return function(val){
@@ -19,7 +29,7 @@ var functools = (function(undefined){
}
compose.async = function compose_async(/* functions */){
- var fns = Array.prototype.slice.call(arguments);
+ var fns = slice.call(arguments);
return function(initialValue,callback){
(function(i,error,value){
if(error || fns.length<=i){
@@ -43,26 +53,27 @@ var functools = (function(undefined){
* juxt({ "num": Number, "str": String, "arr": Array })(true) => { "num": 1, "str": "true", "arr": [true] }
*/
function juxt(/* functions */){
- var fns = arguments.length > 1 || typeof arguments[0] != 'object' ? arguments : arguments[0];
+ var fns = arguments.length > 1 || typeof arguments[0] != 'object' ? slice.call(arguments) : arguments[0];
return function(){
var args = arguments;
- return map(function(fn){
+ return map(fns, function(fn){
return fn.apply(undefined, args);
- }, fns);
+ });
};
}
juxt.async = function(/* functions */){
- var fns = arguments.length > 1 || typeof arguments[0] != 'object' ? arguments : arguments[0];
+ var fns = arguments.length > 1 || typeof arguments[0] != 'object' ? slice.call(arguments) : arguments[0];
+
return function(/* args, callback */){
- var args = Array.prototype.slice.call(arguments, 0, arguments.length-1),
+ var args = slice.call(arguments, 0, arguments.length-1),
callback = arguments[arguments.length-1];
- map.async(function(fn, callback){
+ map.async(fns, function(fn, callback){
fn.apply(undefined, args.concat([callback]));
- }, fns, callback);
+ }, callback);
};
};
@@ -71,7 +82,7 @@ var functools = (function(undefined){
* are gathered.
*/
function curry(fn){
- var args = Array.prototype.slice.call(arguments,1),
+ var args = slice.call(arguments,1),
len = fn.length;
return (function(){
Array.prototype.push.apply(args,arguments);
@@ -82,7 +93,7 @@ var functools = (function(undefined){
/**
* Execute 'fn' once per 'iterable' element.
*/
- function each(fn,iterable){
+ function each(iterable, fn){
for(var i = -1, len=iterable.length; ++i < len; ){
fn(iterable[i],i,iterable);
};
@@ -92,15 +103,15 @@ var functools = (function(undefined){
/**
* Apply 'fn' to every element of 'iterable', returning those elements for which fn returned a true value.
*/
- function filter(fn,iterable){
+ function filter(iterable, fn){
var accumulation = [];
for(var i = -1, len=iterable.length; ++i < len; ){
fn(iterable[i],i,iterable) && accumulation.push(iterable[i]);
};
return accumulation;
}
- filter.async = function filter_async(fn, iterable, callback){
+ filter.async = function filter_async(iterable, fn, callback){
var accumulation = [];
(function(i,ptest){
@@ -114,17 +125,17 @@ var functools = (function(undefined){
fn(iterable[i], partial(arguments.callee, [i+1]));
})(0);
- }
+ };
/**
* Apply fn to every element of iterable.
*/
- function map(fn, iterable){
+ function map(iterable, fn){
var clone, i, len, key;
if(isArray(iterable)){
- clone = Array.prototype.slice.call(iterable, 0);
+ clone = slice.call(iterable, 0);
i = -1;
len = clone.length;
@@ -145,11 +156,11 @@ var functools = (function(undefined){
return clone;
}
- map.async = function map_async(fn, iterable, callback){
+ map.async = function map_async(iterable, fn, callback){
var clone, iter, i, len, key, value,
list = isArray(iterable);
- iter = list ? Array.prototype.slice.call(iterable) : Object.keys(iterable);
+ iter = list ? slice.call(iterable) : objectKeys(iterable);
clone = list ? iter : {};
(function next(i, error, rpl){
@@ -176,7 +187,7 @@ var functools = (function(undefined){
function partial(fn,initialArgs,ctx){
!initialArgs && ( initialArgs = [] );
return function(){
- var args = Array.prototype.slice.call(initialArgs,0);
+ var args = slice.call(initialArgs,0);
Array.prototype.push.apply(args,arguments);
return fn.apply(ctx,args);
};
View
@@ -1,6 +1,6 @@
{
"name":"functools",
- "version":"1.2.0",
+ "version":"1.3.0",
"description":"A minimal library of functional operations",
"author":"Azer Koculu <azer@kodfabrik.com>",
"keywords":["functional","fp"],
@@ -10,5 +10,8 @@
"devDependencies":{
"lowkick":"0.x",
"highkick":"1.x"
+ },
+ "scripts":{
+ "test":"./node_modules/.bin/highkick test/main.js"
}
}

0 comments on commit 047ea34

Please sign in to comment.