1.9's New Methods

I

Intransition

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!
 
R

Ryan Davis

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.
 
I

Intransition

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.
 
M

Michael Fellinger

First a big shout-out to Marc-Andre Lafortune and his <a href=3D"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:

=C2=A0def flat_map(&block)
=C2=A0 =C2=A0return to_enum:)flat_map) unless block_given?
=C2=A0 =C2=A0map(&block).flatten(1)
=C2=A0end unless method_defined? :flat_map
=C2=A0Backports.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=3D>2, 2=3D>4, 3=3D>6}
RUBY_DESCRIPTION
# "ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]"

--=20
Michael Fellinger
CTO, The Rubyists, LLC
 
J

James Edward Gray II

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

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

James Edward Gray II=
 
F

Florian Gilcher

First a big shout-out to Marc-Andre Lafortune and his <a href=3D"http://=
github.com/marcandre/backports">backports</a> project. Nice work.
=20
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:
=20
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
=20
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!
=20

One advantage of :flat_map over map(...).flatten(...) is that it can be =
chained in an Enumerator.

Sometimes, this can come in handy.=20

Regards,
Florian Gilcher
 
R

Ryan Davis

On Apr 15, 2010, at 6:30 AM, Ryan Davis wrote:
=20
i use Hash[*collection.map {...}.flatten] all the time.=20
=20
Me too, but it's needed less in 1.9 I think. Hash[=85] now accepts an =
Array of Arrays.

nice. too bad I don't use 1.9 at all.
 
J

Jörg W Mittag

Intransition said:
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!

#flat_map is monadic bind (also known as flatMap in Scala, SelectMany
in .NET and (>>=) in Haskell). The other monadic operator that is
needed to build a monad, is unit (aka return in Haskell), but in an OO
language, that's just a factory method, IOW it's just .new in Ruby.

So, #flat_map can potentially turn any class that mixes in Enumerable
into a monad, provided that it also obeys the monad laws, of course.

Whether or not that warrants its own method ... well, I have no idea.

jwm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top