...

View Full Version : JS Reduce



matrixstar
01-03-2011, 06:53 PM
I need to implement a function called map (it needs to be written using reduce) in the below code that takes an array (arr) and a function (iter) as its arguments and returns an array with all its items transformed by iter, e.g. map([1, 2, 3, 4, 5], function(x) {return x * 2;}) should return [2, 4, 6, 8, 10]


(function(){
var reduce = function(arr, iter, acc){
var len = arr.length, i = 0;
for (i = 0; i < len; i += 1) acc = iter(acc, arr[i], i);
return acc;
};
var map = function(arr, iter){
// Implementation code should go here
//
//
};
return map([1, 2, 3, 4, 5], function(x){return x * 2;});
})();

Can anyone please help me?

Thanks

johnmerlino
01-03-2011, 06:59 PM
Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}





alert([1,2,3,4].map(function(x){ return x * 2}));

Nile
01-03-2011, 07:05 PM
Or


<script type="text/javascript">
Array.prototype.map = function(func) {
for(var a = new Array(), i = 0; i < this.length; a[i] = func(this[i++]));
return a;
};
alert([1,2,3,4].map(function(x){ return x * 2}));
</script>

Dormilich
01-04-2011, 04:47 PM
MDN uses this code

if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp */)
{
"use strict";

if (this === void 0 || this === null)
throw new TypeError();

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();

var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}

return res;
};
}

rnd me
01-04-2011, 07:25 PM
Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}





alert([1,2,3,4].map(function(x){ return x * 2}));


don't use this, ever! it breaks all browser besides IE! same applies for matrixstar's code as well.

1. it doesn't skip over undefined slots in the array, which causes out-of-spec execution of the passed method.

2. where's the dynamic this? nomally, map function are supposed to accept this, but this function never applies a context, it inherits from wherever the [].map function is defined! yikes!

in other words, how can one use a good re-usable map function with code like yours:


function gt(n){return n>this;}
[1,2,3,4,5].map(gt, 3)


should return : [false, false, false, true, true]


3. where are the other arguments? normally arguments[1] is the array index, and arguments[2] is the whole, original array.
how can i build an indexed array, not to mention a ton of other applications, if you take away [].map's other arguments?

Array(11).toString().split("").map(function(a,i){return i;})




4. since [].map is a standard, and the code doesn;t object-detect, it clobbers the native [].map which provides the functionality mentioned above. If i tried to use my re-usable this-bound functions on a site that used your code, my code would break. that's not right.

Dormilich's code is overkill, but at least it works correctly, and who cares how long stuff takes for IE users?

Dormilich
01-04-2011, 07:35 PM
Dormilich's code is overkill, but at least it works correctly, and who cares how long stuff takes for IE users?
ask that the Mozilla guys, I just happened to know that they provide alternate code for JS 1.6+ methods.

rnd me
01-04-2011, 07:45 PM
ask that the Mozilla guys, I just happened to know that they provide alternate code for JS 1.6+ methods.

they mention explicitly that they need the full stock of the code posted for their interpreter and that not all runtimes need this much explication. IE doesn't need as much, so it's very possible to optimize the function for performance whilst maintaining full operational compatibility. I sometimes paste the mdc versions (they are in my snippits manager), but they are so bulky that i prefer other versions.


the "F" library in my signature provides all the array methods, along with dozens of re-usable functions and global shortcuts, all in less than 2kb of code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum