Last active
December 12, 2015 09:59
-
-
Save AutoSponge/4756086 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
(function (global) { | |
var cache = {}; | |
function c(n) { | |
if (n < 1) { | |
return new Function; | |
} | |
var i; | |
var params = ""; | |
var vars = " return function () {" + "\n"; | |
var body = " return _0;" + "\n" + " };"; | |
for (i = 0; i < n; i += 1) { | |
params += i ? (", fn" + i) : "fn" + i; | |
} | |
vars += " var _" + --i + " = fn" + i + ".apply(this, arguments);" + "\n"; | |
while (i--) { | |
vars += " var _" + i + " = fn" + i + ".call(this, _" + (i + 1) + ");" + "\n" | |
} | |
return Function(params, vars + body); | |
}; | |
global.compose = function (/*fn1[, fn2[, fn3...]]*/) { | |
var n = arguments.length; | |
return (cache[n] = cache[n] || c(n)).apply(this, arguments); | |
}; | |
}(this)); |
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 slice(obj) { | |
return Array.prototype.slice.call(obj, 0); | |
} | |
function makeArray(obj) { | |
if (obj === null || obj === undefined) { | |
return []; | |
} | |
if (obj instanceof Array) { | |
return obj; | |
} | |
if (obj instanceof Object && "length" in obj && "0" in obj) { | |
return slice(obj); | |
} | |
return [obj]; | |
} | |
var fixedArr = [1,2,3]; | |
function fixed() { | |
return fixedArr; | |
} | |
function makeUnary(arg) { | |
if (arguments.length > 1) { | |
return arguments; | |
} | |
return arg; | |
} | |
//compose slice and makeArray passing fixed as a parameter | |
console.assert(JSON.stringify(compose(slice, makeArray)(fixed())) === "[1,2,3]"); | |
console.assert(compose(slice, makeArray)(fixed()) !== fixed()); | |
//compose slice, makeArray, and fixed | |
console.assert(JSON.stringify(compose(slice, makeArray, fixed)()) === "[1,2,3]"); | |
console.assert(compose(slice, makeArray, fixed)() !== fixed()); | |
//compose slice, makeArray, and makeUnary so any signature will produce a list | |
var list = compose(slice, makeArray, makeUnary); | |
var testlist = compose(JSON.stringify, slice, makeArray, makeUnary); | |
console.assert(JSON.stringify(list(1)) === "[1]"); | |
console.assert(testlist(1) === "[1]"); | |
console.assert(JSON.stringify(list(1,2)) === "[1,2]"); | |
console.assert(testlist(1,2) === "[1,2]"); | |
console.assert(JSON.stringify(list([1,2])) === "[1,2]"); | |
console.assert(testlist([1,2]) === "[1,2]"); | |
console.assert(JSON.stringify(list({0:1,1:2,length:2})) === "[1,2]"); | |
console.assert(testlist({0:1,1:2,length:2}) === "[1,2]"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment