by brundolf · 4y ago · 0 comments
IMO it's the worst of both worlds and there's virtually never a good reason to use it. If you're going to write a side-effect-y loop, a classic for-loop (or more often, a for..of loop) helps to emphasize that the code is side-effect-y.
forEach emphasizes you are operating on each element of the loop in a semi-independent fashion and you explicitly don't have access to any kind of intermediary index/iterator variable.
When you "drop down" to a classic for loop you're telling the reader that you need access to the index/iterator for some reason (and there are plenty of valid reasons for that).
If you only use classic for loops then the reader has to be constantly vigilant you're not doing any index/iterator shenanigans. It's just higher "cognitive load"
I disagree with this point, specifically for JavaScript the for...of syntax is more pleasant to read and also does not infer that any index access is gonna be done. And as a plus, it is easier to be used with async/await.
await Promise.all(collection.map(thing => some_async_function(thing)));
instead of: for thing of collection {
await some_async_function(thing);
}Yeah, the “map and friends pass extra parameters that are usually ignored” plus the “lots of JS functions optionally take extra parameters that are usually unused” makes it so easy to think of a function as a one-arg function when its really one plus multiple optionals, use it bare in a map, and get stung by it.
Is it bad to use map?
You actually don't need `map`, you can easily implement that using `reduce` too ;)
There is utility in communicating whether you're working with a catamorphism vs a homomorphism. Code is written for humans, after all.
Also I think `forEach` is great for cases such as `arr.forEach(f)` where `arr: Array<T>, f: T => void`.
ForEach does get you both.
I don't remember using for loop for at least 3 years previously as a frontend engineer :)
It is longer than:
array.forEach((value, index) => {})
… but forEach has too many limitations (no exits, no await, …)
I'll give you await (gotta use awkward reduce there) but if you want exit, you're better off with something like `find`
Another advantage is that array functions better convey what are you trying to do with this particular operation.
I strongly disagree.
> ['1', '2', '3'].map(parseInt)
[1, NaN, NaN]
> ['3', '2', '1'].map(parseInt)
[3, NaN, 1]// Consider: ["1", "2", "3"].map(parseInt); // While one could expect [1, 2, 3] // The actual result is [1, NaN, NaN]
// parseInt is often used with one argument, but takes two. The second being the radix // To the callback function, Array.prototype.map passes 3 arguments: // the element, the index, the array // The third argument is ignored by parseInt, but not the second one, // hence the possible confusion. See the blog post for more details
function returnInt(element){ return parseInt(element,10); }
["1", "2", "3"].map(returnInt); // Actual result is an array of numbers (as expected)
The normal solution is to write an inline function; “shortcutting” it by just passing a named function directly can be nice when it’s appropriate, but it often isn’t what you want to do
Yes, it is.
Its about .map(), and the broader design of the API around it. Yes, map passes extra args and parseInt takes optional args, and it wouldn’t be a problem if both of those weren’t true. It would be better if .map() was one-arg and had friends .map_with_index(), .map_with_collection(), map_with_index_and_collection().
(It would be better if .map() wasn’t an Array-specific method but part of a general collection protocol that returned [and was applicable to] generators instead of returning Arrays, and then you also had companion methods to like .enumerate(), but that’s…well, farther afield.)
> The normal solution is to write an inline function
Yes, that’s the solution, but that doesn’t say that the design of .map() isn’t a critical component of there being something to solve in the first place.
Also, the silently ignored argument count mismatch errors heavily contribute to the problem. You may have used other functions with map() and never knew it's always passing an extra argument in, because JS silently ignores this error.
As far as ignoring errors silently, sure, but I’m not even sure what the error would be in this case. Index is a number and radix is a number; even typescript won’t complain about this because the types line up.