Appendix A. Common Functions for Functional Programming in JavaScript
This Appendix covers common functions for functional programming in JavaScript:
- Array Functions:
var flatten = function(arrays) { return arrays.reduce( function(p,n){ return p.concat(n); }); }; var invert = function(arr) { return arr.map(function(x, i, a) { return a[a.length - (i+1)]; }); };
- Binding Functions:
var bind = Function.prototype.call.bind(Function.prototype.bind); var call = bind(Function.prototype.call, Function.prototype.call); var apply = bind(Function.prototype.call, Function.prototype.apply);
- Category Theory:
var checkTypes = function( typeSafeties ) { arrayOf(func)(arr(typeSafeties)); var argLength = typeSafeties.length; return function(args) { arr(args); if (args.length != argLength) { throw new TypeError('Expected '+ argLength + ' arguments'); } var results = []; for (var i=0; i<argLength; i++) { results[i] = typeSafeties...