A Functional Javascript Library

functional.js (λ)

functional.js is (go on, guess) a functional JavaScript library I’m writing. It facilitates currying and point-free / tacit programming in JavaScript.

The library already has support for some higher-order programming, such as iterating, mapping and reducing (some examples pulled from my jasmine tests are below).

I’m working to add more functions and helpers to support functional-level programming but it’s pretty tough ;). Contributions are more than welcome if you fancy some brain pain.

Basic λ.curry example

1
2
3
4
5
6
7
8
var concatenate = λ.curry(function(word1, word2) {
return word1 + " " + word2;
});
var concatenateHello = concatenate("Hello");
var result = concatenateHello("World");
expect(result).toEqual("Hello World");

Another λ.curry example

1
2
3
4
5
6
7
8
9
10
11
var add = λ.curry(function(arg1, arg2, arg3) {
return arg1 + arg2 + arg3;
});
var add3 = add(3),
add5 = add3(2);
expect(add(3)(2)(1)).toEqual(6);
expect(add3(2, 1)).toEqual(6);
expect(add3(2)(1)).toEqual(6);
expect(add5(1)).toEqual(6);

Curried λ.each example

1
2
3
4
5
6
7
8
9
10
11
12
var result = [],
items = ["f", "u", "n", "c"];
var addTo = function (item) {
return result.push(item);
};
var addToResult = λ.each(addTo);
expect(typeof (addToResult)).toEqual("function");
addToResult(items);
expect(result).toEqual(["f", "u", "n", "c"]);

Curried λ.map example

1
2
3
4
5
6
7
8
9
10
11
var items = [1, 2, 3];
var doubleUp = function (number) {
return number * 2;
};
var doubleMap = λ.map(doubleUp);
expect(typeof (doubleMap)).toEqual("function");
var result = doubleMap(items);
expect(result).toEqual([2, 4, 6]);

Curried λ.reduce example

1
2
3
4
5
6
7
8
9
10
11
var items = [1, 2, 3];
var multiply = function (arg1, arg2) {
return arg1 * arg2;
};
var multiplyReduceFrom1 = λ.reduce(multiply, 1);
expect(typeof (multiplyReduceFrom1)).toEqual("function");
var result = multiplyReduceFrom1(items);
expect(result).toEqual(6);

Why?

I like torture to push myself as a developer.

Great, but, erm, why?

By extending functions into chains of functions with fewer arguments (currying), you can fix earlier known values and return a function that only accepts future values. This avoids repetition, a good programming practice, and will assist my high-level function refinement.

Libraries like underscore can’t be curried in a regular fashion, as the items come before the iterator (e.g. (items, iterator) not (iterator, items)). There’s also no support for functional-level programming. The idea is that functional.js (λ) will be really lightweight and can be used as an underscore replacement once it’s been developed further. It also has no dependencies.

Where can I get it?

  • $ git clone git@github.com:leecrossley/functional-js.git
  • $ npm install functional.js
  • functional.min.js

I have issues

As long as they’re related, you can report them on github.

Where’s the λ key?

You’re probably reading the wrong blog post :)