From: Intransition on
First a big shout-out to Marc-Andre Lafortune and his <a href="http://
github.com/marcandre/backports">backports</a> project. Nice work.

Now I want to ask if others have noticed all the new methods being add
to 1.9+? I'm quite happy about vast majority of it, but there was at
least one method I thought pretty peculiar. This Enumerable method:

def flat_map(&block)
return to_enum(:flat_map) unless block_given?
map(&block).flatten(1)
end unless method_defined? :flat_map
Backports.alias_method self, :collect_concat, :flat_map

I am very curious to know how it was decided that a normal map
followed by a 1-deep flatten is common enough to warrant its own
method? Two in fact!

From: Ryan Davis on

On Apr 15, 2010, at 4:06 AM, Intransition <transfire(a)gmail.com> wrote:

> First a big shout-out to Marc-Andre Lafortune and his <a href="http://
> github.com/marcandre/backports">backports</a> project. Nice work.
>
> Now I want to ask if others have noticed all the new methods being add
> to 1.9+? I'm quite happy about vast majority of it, but there was at
> least one method I thought pretty peculiar. This Enumerable method:
>
> def flat_map(&block)
> return to_enum(:flat_map) unless block_given?
> map(&block).flatten(1)
> end unless method_defined? :flat_map
> Backports.alias_method self, :collect_concat, :flat_map
>
> I am very curious to know how it was decided that a normal map
> followed by a 1-deep flatten is common enough to warrant its own
> method? Two in fact!

i use Hash[*collection.map {...}.flatten] all the time.

From: Intransition on
On Apr 15, 7:30 am, Ryan Davis <ryand-r...(a)zenspider.com> wrote:

> i use Hash[*collection.map {...}.flatten] all the time.

I see. But really? So now you will use:

Hash[*collection.flat_map {...}]

But that explains why this would never have occurred to me. I use:

collection.map {...}.to_h

Thanks.

From: Michael Fellinger on
On Thu, Apr 15, 2010 at 8:30 PM, Ryan Davis <ryand-ruby(a)zenspider.com> wrote:
>
> On Apr 15, 2010, at 4:06 AM, Intransition <transfire(a)gmail.com> wrote:
>
>> First a big shout-out to Marc-Andre Lafortune and his <a href="http://
>> github.com/marcandre/backports">backports</a> project. Nice work.
>>
>> Now I want to ask if others have noticed all the new methods being add
>> to 1.9+? I'm quite happy about vast majority of it, but there was at
>> least one method I thought pretty peculiar. This Enumerable method:
>>
>>  def flat_map(&block)
>>    return to_enum(:flat_map) unless block_given?
>>    map(&block).flatten(1)
>>  end unless method_defined? :flat_map
>>  Backports.alias_method self, :collect_concat, :flat_map
>>
>> I am very curious to know how it was decided that a normal map
>> followed by a 1-deep flatten is common enough to warrant its own
>> method? Two in fact!
>
> i use Hash[*collection.map {...}.flatten] all the time.
>
>

Hash[[1,2,3].map{|i| [i,i*2]}]
# {1=>2, 2=>4, 3=>6}
RUBY_DESCRIPTION
# "ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]"

--
Michael Fellinger
CTO, The Rubyists, LLC

From: James Edward Gray II on
On Apr 15, 2010, at 6:30 AM, Ryan Davis wrote:

> i use Hash[*collection.map {...}.flatten] all the time.

Me too, but it's needed less in 1.9 I think. Hash[…] now accepts an Array of Arrays.

James Edward Gray II