Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Someone Halp!

Name: Anonymous 2010-11-10 2:20

What in the fuck:

Array.prototype.foldr = function(func, initial) {
    return this.reduce(function(sum, x) {
        return function(n) {
            return sum(func(n, x));
        };
    }, function(x){return x;})(initial);
}

Name: Anonymous 2013-08-10 8:24

>>1

That is retarded, you should write normal code like this:


Array.prototype.foldl = function(f,z){
for(i = 0; i < this.length; i++){
   z = f(z,this[i]);
}
return z;
}


Javascript sucks at tail recursion, so you better translate your snippet to imperative code. Also foldl is more natural in this context and has no disadvantage over foldr. If you want foldr, walk over the list reverse (start by (length - 1)).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List