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 ...

Get JavaScript: Functional Programming for JavaScript Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.