Skip to content

Instantly share code, notes, and snippets.

@mcsf
Last active July 3, 2017 15:30
Show Gist options
  • Save mcsf/88d3f417debdaa5a761f5a611254b27d to your computer and use it in GitHub Desktop.
Save mcsf/88d3f417debdaa5a761f5a611254b27d to your computer and use it in GitHub Desktop.
if (typeof Object.assign != 'function') {
// from MDN
Object.assign = function(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
Function.flow = function() {
var fs = [].slice.call( arguments );
return fs.reduce( function( acc, f ) {
if ( ! acc ) return f;
return function() {
return f( acc.apply( this, arguments ) );
};
} );
};
[ 'map', 'filter', 'reduce', 'some', 'every' ].forEach( function ( key ) {
if ( ! Array.prototype[ key ] ) return;
Function[ key ] = function( a, b ) {
return function( xs ) {
return xs[ key ]( a, b );
};
};
} );
Function.flatten = function( xs ) {
return xs.reduce( function( acc, x ) {
return acc.concat( x );
}, [] );
};
Function.flatMap = function( f ) {
return Function.flow( Function.map( f ), Function.flatten );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment