Last active
July 22, 2016 12:47
-
-
Save vlas-ilya/9a22b97f48a76b728be8304b8d6b4eb1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"use strict"; | |
var currying = function currying(fn) { | |
var fun = function fun() { | |
for (var _len = arguments.length, orig = Array(_len), _key = 0; _key < _len; _key++) { | |
orig[_key] = arguments[_key]; | |
} | |
var inner = function inner() { | |
for (var _len2 = arguments.length, val = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | |
val[_key2] = arguments[_key2]; | |
} | |
return fun.apply(undefined, orig.concat(val)); | |
}; | |
inner.valueOf = function () { | |
return fn.apply(undefined, orig).valueOf(); | |
}; | |
return inner; | |
}; | |
return fun; | |
}; |
This file contains hidden or 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 currying = function (fn) { | |
var fun = function (...orig) { | |
var inner = (...val) => fun(...orig, ...val); | |
inner.valueOf = () => fn(...orig).valueOf(); | |
return inner; | |
}; | |
return fun; | |
}; |
This file contains hidden or 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 a = (a1, a2, a3, a4) => a1 + a2 + a3 + a4; | |
var currying_a = currying(a); | |
console.log(currying_a(1)(2)(3)(4) == a(1, 2, 3, 4)); | |
console.log(currying_a(1)(2, 3, 4) == a(1, 2, 3, 4)); | |
console.log(currying_a(1, 2)(3, 4) == a(1, 2, 3, 4)); | |
console.log(currying_a(1, 2, 3)(4) == a(1, 2, 3, 4)); | |
console.log(currying_a(1, 2, 3, 4) == a(1, 2, 3, 4)); | |
var currying_a_2_3 = currying_a(2, 3); | |
console.log(currying_a_2_3(4)(5) == a(2, 3, 4, 5)); | |
console.log(currying_a_2_3(5, 6) == a(2, 3, 5, 6)); | |
var currying_currying_a = currying(currying_a) | |
console.log(currying_currying_a(1)(2)(3)(4) == a(1, 2, 3, 4)); | |
console.log(currying_currying_a(1)(2, 3, 4) == a(1, 2, 3, 4)); | |
console.log(currying_currying_a(1, 2)(3, 4) == a(1, 2, 3, 4)); | |
console.log(currying_currying_a(1, 2, 3)(4) == a(1, 2, 3, 4)); | |
console.log(currying_currying_a(1, 2, 3, 4) == a(1, 2, 3, 4)); | |
var currying_currying_currying_a_2_3 = currying(currying_currying_a(2, 3)) | |
console.log(currying_currying_currying_a_2_3(4)(5) == a(2, 3, 4, 5)); | |
console.log(currying_currying_currying_a_2_3(5, 6) == a(2, 3, 5, 6)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment