[groovy-dev] Method Naming in Collection

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[groovy-dev] Method Naming in Collection

Russel Winder
Am I right in believing that  Collection.collect  is what everyone else
would call map and that Colelction.inject  is what everyone else would
call reduce?  Was there a reason for choosing names that are different
from the common ones?

Interestingly (or not) Python is aiming to remove map, filter, reduce
and lambda.  Map and filter are no problem as Python has list
comprehensions.  Reduce is a bit of a problem but...  lambda is
effectively an unnamed closure and I bet that will be hard to live
without.

Is there a Groovy way of doing list comprehensions?

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

tugwilson

On 3 Nov 2005, at 09:38, Russel Winder wrote:

> Am I right in believing that  Collection.collect  is what everyone  
> else
> would call map and that Colelction.inject  is what everyone else would
> call reduce?  Was there a reason for choosing names that are different
> from the common ones?

I'm not sure everybody else would call them that. The Groovy names  
come from Ruby I think.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] Method Naming in Collection

Dierk König
In reply to this post by Russel Winder
> Am I right in believing that  Collection.collect  is what everyone else
> would call map and that Colelction.inject  is what everyone else would
> call reduce?  

There was a long thread about 'collect' vs 'map'.

BTW: Ruby has both :-)

'inject' is as in Ruby and Smalltalk.

> Was there a reason for choosing names that are different
> from the common ones?

What is 'common' refers to your background. I for myself
have never seen 'reduce' before...

YMMV
Mittie
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

Russel Winder
In reply to this post by tugwilson
On Thu, 2005-11-03 at 09:48 +0000, John Wilson wrote:

> I'm not sure everybody else would call them that. The Groovy names  
> come from Ruby I think.

Perhaps I overstated the case, but coming from a FP (Scheme, Miranda) /
Python perspective map, filter, reduce and lambda are the labels.  I
think Ruby uses map, select, reduce and lambda.

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] Method Naming in Collection

Russel Winder
In reply to this post by Dierk König
On Thu, 2005-11-03 at 10:57 +0100, Dierk Koenig wrote:

> There was a long thread about 'collect' vs 'map'.

I guess I should check this out.

> BTW: Ruby has both :-)
>
> 'inject' is as in Ruby and Smalltalk.

Perhaps Groovy could have aliases for these methods as well?  I know it
is better to have a single name but there are clearly two distinct
naming camps.  Is it worth supporting both?

Python is running away from the issue by removing the functionality in
favour of list comprehensions.  It is one solution.  Groovy doesn't seem
to have list comprehensions as far as I can see.  Is it worth
investigating.

> What is 'common' refers to your background. I for myself
> have never seen 'reduce' before...

Indeed.  I had never seen inject :-)

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

Martin C. Martin
In reply to this post by Russel Winder
The word "map" is already taken in Java.  There are far more people who
know Java's definition of "map" than know functional languages, so a
good case could be made that calling it "map" would be confusing to most
users.

Russel Winder wrote:

>Am I right in believing that  Collection.collect  is what everyone else
>would call map and that Colelction.inject  is what everyone else would
>call reduce?  Was there a reason for choosing names that are different
>from the common ones?
>
>Interestingly (or not) Python is aiming to remove map, filter, reduce
>and lambda.  Map and filter are no problem as Python has list
>comprehensions.  Reduce is a bit of a problem but...  lambda is
>effectively an unnamed closure and I bet that will be hard to live
>without.
>
>Is there a Groovy way of doing list comprehensions?
>
>  
>
I'm rusty on my Python, but IIRC each() and/or collect() let you do
everything you could with list comprehensions, and more, more
elegantly/concisely.

- Martin

Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

Russel Winder
On Thu, 2005-11-03 at 08:09 -0500, Martin C. Martin wrote:
> The word "map" is already taken in Java.  There are far more people who
> know Java's definition of "map" than know functional languages, so a
> good case could be made that calling it "map" would be confusing to most
> users.

map is not used as a method name at all in the JDK so it is available
for use.  I don't think there is any problem of conflict between the
type Map and a method map.

> >Is there a Groovy way of doing list comprehensions?
> >
> I'm rusty on my Python, but IIRC each() and/or collect() let you do
> everything you could with list comprehensions, and more, more
> elegantly/concisely.

Guido clearly disagrees, he is adamant to remove
map/filter/reduce/lambda.  I can see his point.  They are reflections of
a different style of programming, one that can be slotted into Python
but is not in the spirit of core Python.  In Python I think list
comprehensions do sit better.

The question is whether they are worth entertaining in Groovy.  Its all
a question of style and presentation rather than one of expressibility.
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

Guillaume Laforge
Administrator
On 03/11/05, Russel Winder <[hidden email]> wrote:
> map is not used as a method name at all in the JDK so it is available
> for use.  I don't think there is any problem of conflict between the
> type Map and a method map.

"map" was especially confusing for standard Java programmers who think
such a method will generate... a Map! People don't always come from
Python or from functional languages you know ;-)

--
Guillaume Laforge
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

Russel Winder
On Thu, 2005-11-03 at 16:17 +0100, Guillaume Laforge wrote:

> "map" was especially confusing for standard Java programmers who think
> such a method will generate... a Map! People don't always come from
> Python or from functional languages you know ;-)

But isn't it obvious to everyone that the method you describe would be
called "toMap" ;-)

Actually I am a Fortran IV programmer who has dabbled in Pascal, C, C++,
Scheme, Miranda, Java, Python, Ruby, Groovy in that order.  So why are
there no common blocks in Groovy? :-)
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] Method Naming in Collection

Guillaume Laforge
Administrator
On 03/11/05, Russel Winder <[hidden email]> wrote:
> But isn't it obvious to everyone that the method you describe would be
> called "toMap" ;-)

It isn't since I remember one user one day asking what that map()
method was and why it didn't return a map...

--
Guillaume Laforge
http://glaforge.free.fr/blog/groovy