3 ms·
> for the longest time for loops required a mutating index Well, at least one mutating index is convenient: function forEach (a,f) { for(var i=0;i<a.l
by wwweston 13y ago
> for the longest time for loops required a mutating index
Well, at least one mutating index is convenient:
function forEach (a,f) {
for(var i=0;i<a.length;i++) f(a[i],i,a);
}
After that, you generally wouldn't need more indexes.
But does it really require a mutating index?
function forEach (a,f,i) {
var n = i || 0;
f(a[0],n,a);
if(a.length > 1)
forEach(a.slice(1),f,n+1);
}
Array copies aren't particularly efficient, of course, and there are languages with data structures backing arrays for which some similar approach might be efficient, but there it is.
- tieTYT 13y agoI guess the thing I'm harping on is shawnz saying that javascript is INHERENTLY functional. I think it's possible to write javascript in a functional way: Underscore.js is a perfect example of that. But I wouldn't say it's INHERENT of javascript. I think javascript is INHERENTLY object oriented/imperative but can be functional if you put some effort into it.
- nijk 13y agoJS isn't inherenttly OO. Crockford wrote a whole book chapter on twisting JS into make it OO. JavaScript is inherently a blurry mess, and intentional but hacky hybrid of Scheme and C-style.