4 ms·
Indeed. The real magic is when you start doing things like: some_enum.collect(&:+) This simple line will do everything from sum numbers to concatenate string
by busterarm 5y ago
Indeed.
The real magic is when you start doing things like:
some_enum.collect(&:+)
This simple line will do everything from sum numbers to concatenate strings, etc.
- mikepurvis 5y agoNot a rubyist, but I love that collect() made it into Rust, too. I feel like it takes the role that generators have in Python, but is much easier to reason about in terms of the control flow and what data is being passed where.
- dragonwriter 5y agoEnumerable#collect is just an eager version Python map() in method rather than function form (#map is a avaulable as an alias for #collect); it is a little more concise for when the function you are mapping over is a method on the object being processed (even more than Ruby’s normal advantage with lambdas), but not fundamentally more powerful. (Enumerable#lazy gives you an object where collect/map is, and other merhods are, lazy rather than eager.) > I feel like it takes the role that generators have in Python Ruby has generators, though in either Ruby or Python map/collect (the lazy versions, in Ruby) can be used for some of their uses, since it basically produces a generator from an input collection or generator and a mapping function.
- halostatue 5y ago#collect is an alias for #reduce, not #map, making it like Python reduce().
- 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.
- rubyist5eva 5y agoWhile this is cool, you can also just do `some_enum.sum` (also works with string concacts) One thing that is cool about the .to_proc shorthand though is, for example, you have a method def square_it(x); x * 2; end; people tend to write an enumeration that uses it like items.map {|i| square_it i } but you can actually write it like this items.map(&method(:square_it)) So not only can you write shorthands to methods on the object itself, you can write shorthands to other methods and "automagically" pass the item as its argument. Nifty stuff.