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
// Birthday problem | |
// https://en.wikipedia.org/wiki/Birthday_problem | |
// given n possible values and a set size of k, | |
// determine the probability of a collision | |
function odds( n, k ) { | |
return 1 - Math.exp( -0.5 * k * ( k - 1 ) / n ); | |
} | |
// given n possible values and a probability of p, |
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
function memoize( fn ) { | |
const map = new Map(); | |
const key = Symbol('key'); | |
return function() { | |
let item = map; | |
let cached = false; | |
for ( let i = 0; i < arguments.length; ++i ) { | |
let cache = item.get( arguments[ i ] ); |
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
function compose() { | |
var fns = Array.prototype.slice.call( arguments ); | |
return function( arg ) { | |
return fns.reduceRight(function( result, fn ) { | |
return fn( result ); | |
}, arg ); | |
}; | |
} | |
function double( n ) { |
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
map([ 'a', 'b', 'c' ], function( value ) { | |
return value.toUpperCase(); | |
}); // [ 'A', 'B', 'C' ] |
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
function map( arr, fn ) { | |
if ( arr.length === 0 ) { | |
return []; | |
} | |
return [ fn( arr[ 0 ] ) ].concat( map( arr.slice( 1 ), fn ) ); | |
} |
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
function reverse( str ) { | |
if ( str.length <= 1 ) { | |
return str; | |
} | |
return reverse( str.substr( 1 ) ) + str[ 0 ]; | |
} |
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
function factorial( n ) { | |
if ( n === 1 ) { | |
return 1; | |
} | |
return n * factorial( n - 1 ); | |
} |
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
function curry( fn ) { | |
var arity = fn.length; | |
return (function resolver() { | |
var mem = Array.prototype.slice.call( arguments ); | |
return function() { | |
var args = mem.slice(); | |
Array.prototype.push.apply( args, arguments ); | |
return ( args.length >= arity ? fn : resolver ).apply( null, args ); | |
}; |
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
var EventEmitter = (function() { | |
var cache = new WeakMap(); | |
function EventEmitter( obj ) { | |
cache.set( this, {} ); | |
} | |
EventEmitter.prototype = { | |
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
var EventEmitter = (function() { | |
var cache = new WeakMap(); | |
function EventEmitter() { | |
cache.set( this, {} ); | |
} | |
EventEmitter.prototype = { | |