From: Nathan Broadbent Date: 2012-11-11T18:42:17+09:00 Subject: [ruby-core:49218] Re: [ruby-trunk - Feature #7241] Enumerable#to_h proposal --e89a8fb206ce0a35a604ce3500e3 Content-Type: text/plain; charset=ISO-8859-1 > > Clojure has a function (({into})) that might fit the bill. >> > > This indeed looks very promising. I like the sound of 'into', but am not sure about appending results with the '<<' operator. If Hash had '<<' and '+' aliases for 'update' and 'merge' (respectively), we might as well give 'map' an optional argument, and call: [1,2,3].map({}) {|i| { i => i ** 2 } } And if Hash#update accepted a two-element array, we could do: [1,2,3].map({}) {|i| [i, i ** 2] } So I like the 'into' name, but I think it would be more useful as an alias for 'each_with_object', instead of just 'map' with an argument for the base object. > I often want a "collect" method where I'm not forced to collect exactly > one item per item of the original collection. If collect weren't an alias > to map, I think it would even make a lot of sense to use the word "collect" > for this (map: one-to-one, collect: one-to-many). > Ruby has a 'flat_map' method (aliased as 'collect_concat') that flattens the first level of a returned array, so you can append multiple results, and don't need to use compact. See http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-flat_map [1,nil,2].flat_map {|i| i ? [i] : [] } #=> [1, 2] Best, Nathan --e89a8fb206ce0a35a604ce3500e3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Clojure has a function (({into})) that might fit the bill.

This indeed looks very promising.

I like th= e sound of 'into', but am not sure about appending results with the= '<<' operator. If Hash had '<<' and '+'= ;=A0aliases for=A0'update' and 'merge' (respectively), we m= ight as well give 'map' an optional argument, and call:

=A0 =A0 [1,2,3].map({}) {|i|=A0{ i =3D> i ** 2 } }

And if Hash#update accepted a two-element array, we= could do:

=A0 =A0 [1,2,3].map({}) {|i| [i, i ** 2= ] }

So I like the 'into' name, but I think it would= be more useful as an alias for 'each_with_object', instead of just= 'map' with an argument for the base object.

=A0
I often want a "collect" method where I'm not forced to colle= ct exactly one item per item of the original collection. If collect weren&#= 39;t an alias to map, I think it would even make a lot of sense to use the = word "collect" for this (map: one-to-one, collect: one-to-many).<= br>

Ruby has a 'flat_map' method (alia= sed as 'collect_concat') that flattens the first level of a returne= d array, so you can append multiple results, and don't need to use comp= act. See=A0http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-flat_m= ap

=A0 =A0 [1,nil,2].flat_map {|i| i ? [i] : [] } =A0 =A0#= =3D> [1, 2]


Best,
Nathan
--e89a8fb206ce0a35a604ce3500e3--