Skip to content

Instantly share code, notes, and snippets.

@defims
Forked from sebastien-p/LICENSE.txt
Last active June 14, 2016 01:24
Show Gist options
  • Save defims/0c32e976cf65acb4e411f3e633c429c7 to your computer and use it in GitHub Desktop.
Save defims/0c32e976cf65acb4e411f3e633c429c7 to your computer and use it in GitHub Desktop.
Array.prototype.reduceRight
// 109 bytes, 153 including 'Array.prototype.reduceRight=[].reduceRight||' ...
function (
a, // callback
b // initialValue and result
){
for (
var c = this, // 'this' shortcut
d = c.length + 1, // iterator
e; // 'undefined' and flag for trailing "holes" in 'c'
d--;
)
b = d in c ? a(b, c[d], d, e = c) : e || b !== e ? b : c[d -= d - 1 in c];
return b
}
export function(a,b){for(var c=this,d=c.length+1,e;d--;)b=d in c?a(b,c[d],d,e=c):e||b!==e?b:c[d-=d-1 in c];return b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Sebastien P. https://twitter.com/#!/_sebastienp
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": "Array.prototype.reduceRight",
"description": "ES5 Array.prototype.reduceRight polyfill",
"keywords": [ "ES5", "array", "reduceRight", "polyfill" ],
"version": "0.1.0"
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>16</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
Array.prototype.reduceRight=function(a,b){for(var c=this,d=c.length+1,e;d--;)b=d in c?a(b,c[d],d,e=c):e||b!==e?b:c[d-=d-1 in c];return b}
document.getElementById( "ret" ).innerHTML = [,,,1,2,,3,,,,,,].reduceRight(function(a,b){return a+b},10)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment