functools is a JavaScript library for functional programming.
Inspired languages: Common Lisp, Clojure and Python.
Version: 1.1.2
SYNOPSIS
A typical usage:
var map = require("functools").map;
var seq = [3,1,4,1,5,9];
functools.map(function(el,ind,seq){
return el+ind;
},seq);
Function Composition:
var compose = require("functools").compose;
compose(select, update, prettify, display)("body .messages");
Async Function Compositon:
function findFiles(path, callback){ ... }
function readContents(files, callback){ ... }
function upload(files, callback){}
compose.async(findFiles, readContents, upload)('~/messages', function(error, uploadResult){
...
});
Currying:
var fn = require("functools");
var pickEvens = fn.curry(fn.filter)(function(num){ return num%2==0 });
pickEvens([3,1,4])
pickEvens([1,5,9,2,6,5])
DESCRIPTION
functools is a library for functional programming written in JavaScript. It's
based on a single CommonJS module (that can be run on web browsers, as well) that consists of several function manipulation and list
iteration tools.
API
compose(functions ...)(value)
Combine functions in a new one, passing the result of each function to next
one, from left to right.
function cube(x){ return x*x*x };
compose(Math.sqrt,cube)(4);
compose.async(functions ...)(value,callback)
Asynchronous, continuation passing based version of compose function. Requires
specified functions to call a callback function, passing an error object (if
there is one) and the result to be carried.
function receiveMessage(url, callback){ ... callback(); }
function findRelatedUser(user, message, callback){ ... callback(null, user, message); }
function transmitMessage(user, message){ ... callback(); }
var messageTransmission = compose.async(receiveMessage, findRelatedUser, transmitMessage);
messageTransmission({ msg:"Hello !", 'user': 3 }, function(error, result){
...
})
curry(function, args ...)
Transform multiple-argument function into a chain of functions that return each other until all arguments are gathered.
function sum(x,y){ return x+y; }
var add3 = curry(sum, 3);
add3(14);
add3(20);
partial(function,initial arguments,*context *)
Return a new function which will call function with the gathered arguments.
function testPartial(){
var args = reduce(function(x,y){ x+", "+y },arguments);
console.log("this:",this);
console.log("args:",args);
}
partial(testPartial, [3,14], 3.14159)(1,5,9);
The example code above will output:
this: 3.14159
args: 3,14,1,5,9
each(function,iterable)
Call function once for element in iterable.
each(function(el,ind,list){ console.assert( el == list[ind] ); }, [3,1,4]);
map(function,iterable)
Invoke function once for each element of iterable. Creates a new array
containing the values returned by the function.
map(function(el,ind,list){ return el*el },[3,1,4,1,5,9]);
map.async(function,iterable, callback)
Apply async function to every item of iterable, receiving a callback
function which takes error (if there is) and replacement parameters.
function readFile(id, callback){ ... callback(undefined, data); }
map.async(readFile, ['./foo/bar', './foo/qux', './corge'], function(error, files){
if(error) throw error;
console.log(files[0]);
});
filter(function,iterable)
Construct a new array from those elements of iterable for which function returns true.
filter(function(el,ind,list){ return el%2==0 },[3,1,4]); // returns [4]
filter.async(function,iterable, callback)
Call async function once for each element in iterable, receiving a boolean
parameter, and construct a new array of all the values for which function
produces true
var users = [ 3, 5, 8, 13, 21 ];
function hasPermission(userId, callback){ ... callback(
juxt(functions ...)
Take a set of functions, return a function that is the juxtaposition of those
functions. The returned function takes a variable number of arguments and
returns a list containing the result of applying each fn to the arguments.
function inc1(n){ return n+1 };
function inc2(n){ return n+2 };
function inc3(n){ return n+3 };
juxt(inc1, inc2, inc3)([3,1,4]);
juxt.async(functions ...)
Async implementation of juxt.
function md5(path, callback){ fetch(path, callback); }
function sha1(path, callback){ fetch(path, callback); }
function crc32(path, callback){ fetch(path, callback); }
juxt.async(md5, sha1, crc32)("hello world", function(error, result){
result[0] => md5("hello world")
result[1] => sha1("hello world")
result[2] => crc32("hello world")
});
reduce(function,iterable)
Apply function cumulatively to the items of iterable, as to reduce the
iterable to a single value
reduce(function(x,y){ return x*y }, [3,1,4]);
reduce.async(function,iterable, callback)
Async implementation of reduce.
var users = [2, 3, 5, 8, 13];
function usernames(accum, userId){ ... callback(undefined, accum + ', ' + username); }
reduce.async(usernames, users, function(error, result){
if(error) throw error;
console.log(result);
});
SEE ALSO