3 ms·
> #collect is an alias for #reduce It’s not, though. (If it was, we could write about its equivalence with Python’s [from functools in py3, core in Py2] reduce
by dragonwriter 5y ago
> #collect is an alias for #reduce
It’s not, though. (If it was, we could write about its equivalence with Python’s [from functools in py3, core in Py2] reduce function, though, saying much the same thing as about the actual equivalence with map upthread, except reduce/inject is less often a substitute for generators than map/collect.)
#map is an alias for #collect. [0]
#reduce has the alias #inject. [1]
[0] https://ruby-doc.org/core-3.0.2/Enumerable.html#method-i-collect https://ruby-doc.org/core-3.0.2/Enumerable.html#method-i-col...
[1] https://ruby-doc.org/core-3.0.2/Enumerable.html#method-i-inject https://ruby-doc.org/core-3.0.2/Enumerable.html#method-i-inj...
- pansa2 5y agoThen should the example in the above comment, `some_enum.collect(&:+)`, be using reduce instead of collect?
- busterarm 5y agoYes. My bad. :) I haven't been a professional Rubyist in some years but my habit here was to use inject.
- halostatue 5y agoYou’re right. My apologies.