Skip to content

Instantly share code, notes, and snippets.

@softprops
Forked from 140bytes/LICENSE.txt
Created June 26, 2011 02:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save softprops/1047155 to your computer and use it in GitHub Desktop.
Save softprops/1047155 to your computer and use it in GitHub Desktop.
foldl
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
}
}
function(f){return function(i,l){return l.length?arguments.callee(f(i,l[0]),l.slice(1)):i;}}
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.
{
"name": "foldl",
"description": "JavaScript fold left function application",
"keywords": ["fp","functional","array"]
}
<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>
@atk
Copy link

atk commented Jun 27, 2011

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}

@softprops
Copy link
Author

Good call. I updated the example

@atk
Copy link

atk commented Jun 28, 2011

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}}

@atk
Copy link

atk commented Jul 30, 2011

The test.html consists only of js. Please either rename it to test.js or provide a html file.

@Kijewski
Copy link

Kijewski commented Jan 3, 2012

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}}

@Kijewski
Copy link

Kijewski commented Jan 3, 2012

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