Last active
December 25, 2015 14:49
-
-
Save AutoSponge/6993297 to your computer and use it in GitHub Desktop.
function-first curried natives and their array-first counterparts
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 (global, a, f) { | |
var list = ["map", "filter", "some", "every", "reduce", "reduceRight", "sort"]; | |
function flip(fn) { | |
return function (that, arg) { | |
//use arguments length because arg could be falsey | |
return arguments.length > 1 ? fn.call( that, this, arg ) : fn.call( that, this ); | |
}; | |
} | |
function createGlobals(fName) { | |
global[fName] = f.bind.bind( flip( a[fName] ) ); | |
global[fName + "To"] = f.bind.bind( a[fName] ); | |
} | |
global.each = f.bind.bind( flip( a.forEach ) ); | |
global.eachTo = f.bind.bind( a.forEach ); | |
eachTo( list )( createGlobals ); | |
}( this, Array.prototype, Function.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 arr = [1, 2, 3]; | |
var fakeArr = {0:1,1:2,2:3,length:3}; | |
function validateEach(e, i) { | |
console.assert(e === i + 1); | |
} | |
each(validateEach)(arr); | |
eachTo(arr)(validateEach); | |
arr.forEach(validateEach); | |
each(validateEach)(fakeArr); | |
eachTo(fakeArr)(validateEach); | |
function validateMap(e, i, _arr) { | |
console.assert(e === i + 1); | |
return e; | |
} | |
console.assert(JSON.stringify(map(validateMap)(arr)) === "[1,2,3]"); | |
console.assert(JSON.stringify(mapTo(arr)(validateMap)) === "[1,2,3]"); | |
console.assert(JSON.stringify(map(validateMap)(fakeArr)) === "[1,2,3]"); | |
console.assert(JSON.stringify(mapTo(fakeArr)(validateMap)) === "[1,2,3]"); | |
console.assert(JSON.stringify(arr.map(validateMap)) === "[1,2,3]"); | |
function validateFilter(e, i, _arr) { | |
console.assert(e === i + 1); | |
return e === 2; | |
} | |
console.assert(JSON.stringify(filter(validateFilter)(arr)) === "[2]"); | |
console.assert(JSON.stringify(filterTo(arr)(validateFilter)) === "[2]"); | |
console.assert(JSON.stringify(filter(validateFilter)(fakeArr)) === "[2]"); | |
console.assert(JSON.stringify(filterTo(fakeArr)(validateFilter)) === "[2]"); | |
console.assert(JSON.stringify(arr.filter(validateFilter)) === "[2]"); | |
function validateSomeOK(e, i, _arr) { | |
console.assert(e === i + 1); | |
return e === 3; | |
} | |
function validateSomeNotOK(e, i, _arr) { | |
console.assert(e === i + 1); | |
return false; | |
} | |
console.assert(some(validateSomeOK)(arr)); | |
console.assert(someTo(arr)(validateSomeOK)); | |
console.assert(some(validateSomeOK)(fakeArr)); | |
console.assert(someTo(fakeArr)(validateSomeOK)); | |
console.assert(arr.some(validateSomeOK)); | |
console.assert(!some(validateSomeNotOK)(arr)); | |
console.assert(!someTo(arr)(validateSomeNotOK)); | |
console.assert(!some(validateSomeNotOK)(fakeArr)); | |
console.assert(!someTo(fakeArr)(validateSomeNotOK)); | |
console.assert(!arr.some(validateSomeNotOK)); | |
function validateEveryOK(e, i, _arr) { | |
console.assert(e === i + 1); | |
return true; | |
} | |
function validateEveryNotOK(e, i, _arr) { | |
console.assert(e === i + 1); | |
return e !== 3; | |
} | |
console.assert(every(validateEveryOK)(arr)); | |
console.assert(everyTo(arr)(validateEveryOK)); | |
console.assert(every(validateEveryOK)(fakeArr)); | |
console.assert(everyTo(fakeArr)(validateEveryOK)); | |
console.assert(arr.every(validateEveryOK)); | |
console.assert(!every(validateEveryNotOK)(arr)); | |
console.assert(!everyTo(arr)(validateEveryNotOK)); | |
console.assert(!every(validateEveryNotOK)(fakeArr)); | |
console.assert(!everyTo(fakeArr)(validateEveryNotOK)); | |
console.assert(!arr.every(validateEveryNotOK)); | |
function validateReduce(a, b, i, _arr) { | |
console.assert(_arr[i - 1] ? _arr[i - 1] === a : a === 0); | |
console.assert(b === _arr[i]); | |
return b; | |
} | |
console.assert(reduce(validateReduce)(arr) === 3); | |
console.assert(reduceTo(arr)(validateReduce) === 3); | |
console.assert(reduce(validateReduce)(fakeArr) === 3); | |
console.assert(reduceTo(fakeArr)(validateReduce) === 3); | |
console.assert(arr.reduce(validateReduce) === 3); | |
function max(a, b, i, _arr) { | |
console.assert(b === _arr[i]); | |
return Math.max(a, b); | |
} | |
console.assert(reduce(max)(arr, 4) === 4); | |
console.assert(reduceTo(arr)(max, 4) === 4); | |
console.assert(reduce(max)(fakeArr, 4) === 4); | |
console.assert(reduceTo(fakeArr)(max, 4) === 4); | |
console.assert(arr.reduce(max, 4) === 4); | |
function validateReduceRight(a, b, i, _arr) { | |
console.assert(_arr[i + 1] ? _arr[i + 1] === a : a === 0); | |
console.assert(b === _arr[i]); | |
return b; | |
} | |
console.assert(reduceRight(validateReduceRight)(arr) === 1); | |
console.assert(reduceRightTo(arr)(validateReduceRight) === 1); | |
console.assert(reduceRight(validateReduceRight)(fakeArr) === 1); | |
console.assert(reduceRightTo(fakeArr)(validateReduceRight) === 1); | |
console.assert(arr.reduceRight(validateReduceRight) === 1); | |
function min(a, b, i, _arr) { | |
console.assert(b === _arr[i]); | |
return Math.min(a, b); | |
} | |
console.assert(reduceRight(min)(arr, 0) === 0); | |
console.assert(reduceRightTo(arr)(min, 0) === 0); | |
console.assert(reduceRight(min)(fakeArr, 0) === 0); | |
console.assert(reduceRightTo(fakeArr)(min, 0) === 0); | |
console.assert(arr.reduceRight(min, 0) === 0) | |
function asc(a, b) { | |
return a - b; | |
} | |
function desc(a, b) { | |
return b - a; | |
} | |
console.assert(JSON.stringify(sort(asc)(arr)) === "[1,2,3]"); | |
console.assert(JSON.stringify(sort(desc)(arr)) === "[3,2,1]"); | |
console.assert(JSON.stringify(sortTo(arr)(asc)) === "[1,2,3]"); | |
console.assert(JSON.stringify(sortTo(arr)(desc)) === "[3,2,1]"); | |
console.assert(JSON.stringify(sort(asc)(fakeArr)) === '{"0":1,"1":2,"2":3,"length":3}'); | |
console.assert(JSON.stringify(sort(desc)(fakeArr)) === '{"0":3,"1":2,"2":1,"length":3}'); | |
console.assert(JSON.stringify(sortTo(fakeArr)(asc)) === '{"0":1,"1":2,"2":3,"length":3}'); | |
console.assert(JSON.stringify(sortTo(fakeArr)(desc)) === '{"0":3,"1":2,"2":1,"length":3}'); | |
console.assert(JSON.stringify(arr.sort(asc)) === "[1,2,3]"); | |
console.assert(JSON.stringify(arr.sort(desc)) === "[3,2,1]"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment