4 ms·
A forEach can easily return a transformed list of values. acc = [] for el in values: acc.append(f(el)) return acc This hierarchy doesn't exist; you
by thaumasiotes 6y ago
A forEach can easily return a transformed list of values.
acc = []
for el in values:
acc.append(f(el))
return acc
This hierarchy doesn't exist; you can pretty much implement any of them in terms of the others.
To make that untrue, you need to define very strict limits on what else your language can do.
(Sure, we produce a transformed list here through the use of side effects. But note that the original example sparking the claim that forEach can't do what map can was this:
.forEach(() => button.click())
Side effects are clearly allowed.)
- joe-user 6y agoThe forEach isn't returning it in your example, the line after it is returning. Try again without acc and I believe that's what was meant. You can implement any of them in terms of the others, but only if you break convention and introduce side effects. Follow convention and try to implement (for example) reduce with map and you'll find that it's not possible.
- thaumasiotes 6y ago> You can implement any of them in terms of the others, but only if you break convention and introduce side effects. This can't be right. forEach has no effects other than side effects. A convention that says not to use side effects prevents you from using forEach at all. But that would make claims about the place of forEach in a hierarchy into meaningless nonsense. > The forEach isn't returning it in your example, the line after it is returning. That depends on your point of view. C has numerous functions which accept pointer parameters and return values in those parameters. That's just the normal way to return multiple values in C. And by that standard, the forEach itself is returning the transformed list; that's where the transformation occurs. The following `return` line is only necessary if this is a snippet within a function whose purpose is to execute a for statement; you would actually do this inline, by just writing the for statement without needing a following `return`.
- HeavyStorm 6y agoI really don't think you got the original point...
- thaumasiotes 6y agoWhat was the original point?
- lurn 6y agoI think they’re only talking about JavaScript, in which case the forEach function doesn’t return anything. So you can do x.map(…).forEach(…) because map returns an array. But u can’t do x.forEach(…).map(…) Ur examples seem like they’re for a different language.
- deleted 6y ago[deleted]
- seedboot 6y agoforEach doesn't return a value https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- momentoftop 6y agoHaskell has strict limits on side-effects, but also abstracts these functions over the thing you're "forEaching." There, a reduce can't implement a map, because a map is required to return the same type of collection you put in. A reduce can only return an arbitrarily chosen collection (say, an Array). And without side-effects, a map can't implement a forEach.
- thaumasiotes 6y ago> And without side-effects, a map can't implement a forEach. Yes, it can, because without side effects, forEach is a NOP. That's not difficult to implement. As long as you don't do anything with the return value from map, you're there.