Functional First, Last, Every(thing)

functional.js (λ)

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

I’ve added many new features since my last post on this and I’ve also started on some array functions. These array functions aren’t specifically unique, but they’re handy and we can’t really do without them. As always, everything can be curried. (curryable?).

Examples

You’re the λ.first

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var items = [5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var firstEven = λ.first(even);
var firstOdd = λ.first(odd);
expect(firstEven(items)).toEqual(2);
expect(firstOdd(items)).toEqual(1);

My λ.last

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var items = [5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var lastEven = λ.last(even);
var lastOdd = λ.last(odd);
expect(lastEven(items)).toEqual(2);
expect(lastOdd(items)).toEqual(1);

My λ.every(thing)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var items = [2, 4, 6, 8];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var everyEven = λ.every(even);
var everyOdd = λ.every(odd);
expect(everyEven(items)).toEqual(true);
expect(everyOdd(items)).toEqual(false);

The actual code needs some performance enhancements (such as breaking out of loops, and possibly making asynchronous?).

λ.partition

If you want to look at something a little more unique, take a look at my partition function, which has also been added recently.

1
2
3
4
5
6
7
8
9
10
11
var items = [7, 6, 5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var partitionEven = λ.partition(even);
var result = partitionEven(items);
expect(result).toEqual([[6, 4, 2], [7, 5, 3, 1]]);

Contribute, Contribute, Contribute

functional.js is starting to be used by people :| (based on downloads and npm downloads anyway) and if you want to contribute there’s plenty TODO. All the code is on GitHub.