Last active
July 3, 2017 15:30
-
-
Save mcsf/88d3f417debdaa5a761f5a611254b27d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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