-
-
Save softprops/1047155 to your computer and use it in GitHub Desktop.
foldl
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(f) { // a function that takes 2 args | |
// 1) an accumulated value | |
// 2) a single value from a list | |
// and returns a value which represents | |
// the accumulated computation | |
return function( // returns a function `curry` | |
i, // an initial value for the accumulator | |
l // a list of values | |
) { | |
return l.length ? arguments.callee( // recurse if length > 0 | |
f(i, l[0]), l.slice(1) | |
) : i; // else return the aggregation | |
} | |
} |
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(f){return function(i,l){return l.length?arguments.callee(f(i,l[0]),l.slice(1)):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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "foldl", | |
"description": "JavaScript fold left function application", | |
"keywords": ["fp","functional","array"] | |
} |
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
<html><head><script type="text/javascript"> | |
var foldl = function(f){return function(i,l){return l.length?arguments.callee(f(i,l[0]),l.slice(1)):i;}} | |
var sum = foldl(function(a,e){ | |
return a + e; | |
}) | |
console.log(sum(0, [1,2,3,4])) // 10 | |
var product = foldl(function(a, e) { | |
return a * e; | |
}) | |
console.log(product(1, [1, 2, 3, 4])) | |
var names = foldl(function(a, e) { | |
a.push(e.name) | |
return a | |
}) | |
console.log(names([],[{name:"a"},{name:"b"},{name:"c"}])) | |
</script></head><body></body></html> |
Good call. I updated the example
Just found that you can replace .slice and [0] with "shift()" to make this even shorter:
function(f){return function(i,l){return l.length?arguments.callee(f(i,l.shift())):i}}
The test.html consists only of js. Please either rename it to test.js or provide a html file.
You could save some bytes if you used a named function instead of arguments.callee:
function(f){return function(i,l){return l.length?arguments.callee(f(i,l[0]),l.slice(1)):i;}}
vs
function(f){return function g(i,l){return l.length?g(f(i,l[0]),l.slice(1)):i}}
vs (including atk's comment)
function(f){return function g(i,l){return l.length?g(f(i,l.shift()),l):i}}
And in Firefox one could even use
function(f)function g(i,l)l.length?g(f(i,l.shift()),l):i
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could be much smaller without the switch statement, for example with a shortened if (expr ? true : false):
function(f){return function(i,l){return l.length?arguments.callee(f(i,l[0]),l.slice(1)):i}