interlude

Functional JavaScript library

Interlude

Interlude is ES5 JavaScript with Haskell inspired enhancements. It's aims to simplify and abstract common patterns by joining common higher order functions with the ES5 arsenal to allow a more declarative style with negligible efficiency changes.

It does not simply alias ES5 methods, and it does not touch prototypes. It curries.

Attach it to the short variable of choice:

var $ = require('interlude');

Then spice up your JavaScript with some functional-style programming;

[1,3,2,6,5,4].filter($.gt(4));
// [ 6, 5 ] 
 
$.range(5).map($.pow(2));
// [ 1, 4, 9, 16, 25 ] 
 
var nested = [ [1,3,2], [2,2], [1,4,2,3] ];
nested.filter($.all($.eq(2)));
// [ [2, 2] ] 
 
// outer sort by property 
nested.sort($.comparing('length'));
// [ [ 2, 2 ], [ 1, 3, 2 ], [ 1, 4, 2, 3 ] ] 
 
// inner sort by value 
nested.map($.invoke('sort', $.compare(-1)));
// [ [ 2, 2 ], [ 3, 2, 1 ], [ 4, 3, 2, 1 ] ] 
 
// used with arrays of pairs 
var pairs = [['woo', 3], ['wee', 1], ['boo', 2]];
pairs.sort($.comparing(1)).map($.first); // [ 'wee', 'boo', 'woo' ] 
 
$.zipWith($.plus3, [1,1,1,1,1], $.range(5), [1,0,0]);
// [ 3, 3, 4 ] 
 
// Powers of two 
$.iterate(8, 2, $.times(2));
// [ 2, 4, 8, 16, 32, 64, 128, 256 ] 
 
 
// Pascal's Triangle 
var pascalNext = function (row) {
  return $.zipWith($.plus2, row.concat(0), [0].concat(row));
};
$.iterate(6, [1], pascalNext);
// [ [ 1 ], 
//   [ 1, 1 ], 
//   [ 1, 2, 1 ], 
//   [ 1, 3, 3, 1 ], 
//   [ 1, 4, 6, 4, 1 ], 
//   [ 1, 5, 10, 10, 5, 1 ] ] 
 
// Prime numbers 
var notCoprime = $($.gcd, $.gt(1));
$.nubBy(notCoprime, $.range(2, 20));
// [ 2, 3, 5, 7, 11, 13, 17, 19 ] 

Interlude is merely a stable front for three re-exported modules:

interlude
├─── operators
├─── autonomy
└─── subset

These modules are of course requirable by themselves, and we encourage you to require them directly. The submodules are small (<300 lines each) so by requiring one of them, you are less likely to have to search around to wonder why you included 1000-some lines when you just wanted 2-3 functions.

Minimizing dependencies make it easier for people to reason about your code. That said, this module is here when/if you want it all under the same namespace.

Additionally, interlude provides a stable front for the three submodules which are still pre 1.0.

Regardless, you should read their short and independent APIs:

Additionally, two extra modules (which were not included in interlude due to their smaller likelihood of use) are perfect fits, does not overlap in API, and are highly recommended when needed. Read their short APIs too:

$ npm install interlude

Install development dependencies

$ npm install

Run the tests

$ npm test

MIT-Licensed. See LICENSE file for details.