fun-js

Another functional programming library for js

fun-js

Functional Programming for Javascript

$ npm install fun-js

Drop fun.js into your project and see usage below.

$ ./test

See project page for test suite output.

Note: fun-js adds a method called 'autoCurry' to Function.prototype

var assert = require("assert");
 
// import *everything* into the global context 
var fun = require("fun-js").import();
 
// Currying 
var findBrians = filter(function(person) {
    return person.name === "Brian";
});
assert.strictEqual(typeof findBrians, "function", "filter can be curried");
 
// Composition 
var hasBrian = compose(compose(not, empty), findBrians);
assert.strictEqual(typeof hasBrian, "function", "compose creates new functions from old ones");
 
var beatles = [
    { name : "John" },
    { name : "Paul" },
    { name : "George" },
    { name : "Ringo" },
    { name : "Brian" }
];
 
assert(hasBrian(beatles), "Brian is a legendary rock star");
 
// Another way of making a hasBrian function... 
var isBrian = compose(identical("Brian"), pluck("name"));
var hasBrian2 = compose(isDefined, find(isBrian));
assert(hasBrian2(beatles), "double-checking that Brian is a legendary rock star");
// global reference stored under window.fun... 
// selectively import 'curry' and 'compose' into the global context 
fun.import({
    select: [
        "curry",
        "compose"
    ]
});
 
if (typeof curry !== "function" || typeof compose !== "function") {
    throw new Error("fun-js could not selectively import curry and compose");
}
    

project page