Hacker News new | ask | show | jobs
by thaumasiotes 1921 days 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`.

1 comments

I really don't think you got the original point...
What was the original point?
I 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.