Skip to content

Instantly share code, notes, and snippets.

@BenoitZugmeyer
Forked from 140bytes/LICENSE.txt
Created June 23, 2011 22:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenoitZugmeyer/1043790 to your computer and use it in GitHub Desktop.
Save BenoitZugmeyer/1043790 to your computer and use it in GitHub Desktop.
Array.prototype.reduce

A 140 bytes Array.prototype.reduce solution.

It should work as described on MDN.

If the array has no value and no initial value is passed, it raises an error. If the callback is not a function, it will fail when tempting to call it.

There is one major difference between ES5 specifications and this implementation: if the second parametter is specified but undefined, it won't be taken as initial value. There is no place to put a arguments.length here :(

Array.prototype.reduce =
[].reduce || // if not defined yet
function(
c, // callback function
v // initial value (optionnal)
) {
for( // iterate
// make actions in the first for statement, save one ';' :)
var t=this
, u // undefined
, i= // initialize i to:
(u=v===u) // if initial value is not defined
? (v=t[0],1) // set initial value to first value and set i to 1
: 0; // else set i to 0
// for loop condition
i < t.length;
)
// for loop body
v=c(v,t[i],i++,t); // call the callback with the arguments, save the result value
i<2 & u && // if the array was empty and no default value was given
u(); // raise a TypeError (u is not a function)
return v // else return the result
}
Array.prototype.reduce=[].reduce||function(c,v){for(var t=this,u,i=(u=v===u)?(v=t[0],1):0;i<t.length;)v=c(v,t[i],i++,t);i<2&u&&u();return v}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Benoît Zugmeyer <alk at graou dot fr>
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": "ArrayReduce140",
"description": "An Array:reduce method implementation in 140 bytes.",
"keywords": [
"array",
"reduce"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>15</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
Array.prototype.reduce=[].reduce||function(c,v){for(var t=this,u,i=(u=v===u)?(v=t[0],1):0;i<t.length;)v=c(v,t[i],i++,t);i<2&u&&u();return v}
document.getElementById( "ret" ).innerHTML = [1,2,3,4,5].reduce(function(a, b) { return a + b; });
</script>
@jed
Copy link

jed commented Jun 24, 2011

this looks great, i love using æ as a shortcut for throw. can you write a quick html example?

@BenoitZugmeyer
Copy link
Author

I'll write an example later. I changed the æ trick to u(), which is on byte more (æ is two bytes) but raises a TypeError (which is expected).

@jed
Copy link

jed commented Jun 24, 2011

awesome. you're probably the best man to tackle reduceRight too, so please give it a shot!

@jdalton
Copy link

jdalton commented Aug 21, 2011

This fallback is not ES5 compliant.
Also browsers like Chrome have a bug where Array.prototype.reduce = [].reduce will cause Array#reduce to become enumerable.

For more information on what can be changed to make this method ES5 complaint and info on a critical bug, that this gist shares, please see my comments on a similar gist.

@avesus
Copy link

avesus commented Mar 6, 2016

I wonder why initial value in Chrome can't be an empty array!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment