3 ms·
I dunno, I find the second one harder. Because now you have to know about array indices and things like that. The for loop forces you to deal with petty details
by squiggleblaz 9y ago
I dunno, I find the second one harder. Because now you have to know about array indices and things like that. The for loop forces you to deal with petty details. The map reduce lets someone else deal with that.
I tend to think there's some additional overhead from Javascript's particular object oriented implementation of mapping and reducing when compared with haskell's i.e.
f xs = foldr + 0 (map (\x -> x * x) xs)
seems a bit easier to me.
Is it habit? Probably. But I really find it hard to understand why maps and folds are simpler than manual iterative loops.
- pasquinelli 9y agosince a map and a sum are both folds, you can simplify it further: f = foldr (\x total -> total + (x * x)) 0 but this is actually a counter argument. look at all these ways of doing this simple operation with higher order functions. plenty of replies from intellectual napoleons like me, too. whereas with the for loop, there's nothing much to say, which seems like a good thing. i genuinely don't know how to feel about this.