This page is no longer maintained — Please continue to the home page at www.scala-lang.org

partialMap -> collect ?

8 replies
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.

I know it's late in the game, but I wanted to bring up one more
possibility for a method renaming. Collections recently acquired
partialMap.

def partialMap[B](pf: PartialFunction[A, B])

Should we rename that to `collect'? I.e.:

acticles collect {
case Diamond(karats: Int, brilliance: Int) => karats * karats
}

I think collect reads better than partialMap here.

For related expressions: collect is map in Smalltalk and is an alias
of map in Ruby. So the name is close enough to be used for partial
maps.

Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: partialMap -> collect ?

On Mon, Mar 22, 2010 at 02:47:35PM +0100, martin odersky wrote:
> Should we rename that to `collect'? I.e.:

Yes, I prefer it, no question. And I like that by removing it from *map
methods, "map" always means that all elements turn up in the result. If
that is sufficiently decisive I can rename it immediately.

And then if you really want to make my day, let me throw in juust one
more collections method, the only still unimplemented useful
intersection of mapping, flattening, and filtering, which actually
allows for all three in one very clear swing:

def flatCollect[B, That](pf: PartialFunction[A, Traversable[B]])(implicit bf: CanBuildFrom[Repr, B, That]): That
val b = bf(repr)
for (x <- coll collect pf)
b ++= x

b.result
}

Patrik Andersson
Joined: 2009-11-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: partialMap -> collect ?

Isn't this a fancy way to say: filter?

On Mon, Mar 22, 2010 at 4:21 PM, Paul Phillips wrote:
> On Mon, Mar 22, 2010 at 02:47:35PM +0100, martin odersky wrote:
>> Should we rename that to `collect'? I.e.:
>
> Yes, I prefer it, no question.  And I like that by removing it from *map
> methods, "map" always means that all elements turn up in the result.  If
> that is sufficiently decisive I can rename it immediately.
>
> And then if you really want to make my day, let me throw in juust one
> more collections method, the only still unimplemented useful
> intersection of mapping, flattening, and filtering, which actually
> allows for all three in one very clear swing:
>
>  def flatCollect[B, That](pf: PartialFunction[A, Traversable[B]])(implicit bf: CanBuildFrom[Repr, B, That]): That
>    val b = bf(repr)
>    for (x <- coll collect pf)
>      b ++= x
>
>    b.result
>  }
>
> --
> Paul Phillips      | A Sunday school is a prison in which children do
> Stickler           | penance for the evil conscience of their parents.
> Empiricist         |     -- H. L. Mencken
> pal, i pill push   |----------* http://www.improving.org/paulp/ *----------
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: partialMap -> collect ?

On Mon, Mar 22, 2010 at 04:54:49PM +0100, Patrik Andersson wrote:
> Isn't this a fancy way to say: filter?

You could compare the implementations and see if any other possibilities
come to mind.

Patrik Andersson
Joined: 2009-11-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: partialMap -> collect ?

What's the difference in effect between a collect in this case and a filter?

On Mon, Mar 22, 2010 at 5:05 PM, Paul Phillips wrote:
> On Mon, Mar 22, 2010 at 04:54:49PM +0100, Patrik Andersson wrote:
>> Isn't this a fancy way to say: filter?
>
> You could compare the implementations and see if any other possibilities
> come to mind.
>
> --
> Paul Phillips      | Before a man speaks it is always safe to assume
> In Theory          | that he is a fool.  After he speaks, it is seldom
> Empiricist         | necessary to assume it.
> slap pi uphill!    |     -- H. L. Mencken
>

Patrik Andersson
Joined: 2009-11-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: partialMap -> collect ?

Nevermind. I'm an idiot!

On Mon, Mar 22, 2010 at 5:08 PM, Patrik Andersson wrote:
> What's the difference in effect between a collect in this case and a filter?
>
> On Mon, Mar 22, 2010 at 5:05 PM, Paul Phillips wrote:
>> On Mon, Mar 22, 2010 at 04:54:49PM +0100, Patrik Andersson wrote:
>>> Isn't this a fancy way to say: filter?
>>
>> You could compare the implementations and see if any other possibilities
>> come to mind.
>>
>> --
>> Paul Phillips      | Before a man speaks it is always safe to assume
>> In Theory          | that he is a fool.  After he speaks, it is seldom
>> Empiricist         | necessary to assume it.
>> slap pi uphill!    |     -- H. L. Mencken
>>
>

Spiros Tzavellas
Joined: 2009-11-23,
User offline. Last seen 42 years 45 weeks ago.
Re: partialMap -> collect ?

Can you also rename the "filterNot" method to "reject"?

As a method name, "filterNot" is good at describing the method's
function, but is it not pleasant to use because it is not an English
phrase and it does not read well. When we speak we say "map",
"filter", "collect" but we do not say "filter not".

IMHO "reject" is a much better name than "filterNot" because:
1) it is as good as "filterNot" at describing the method's function
2) it reads better because it is an English word and
3) it is familiar to many programmers since it is used in the
corresponding "filterNot" method of the Array class in Ruby
(http://ruby-doc.org/core/classes/Array.html).

I understand that it is late for such changes but if you are going to
rename "partialMap" to "collect" you might also want to consider
renaming "filterNot" to "reject".

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: partialMap -> collect ?

On Mon, Mar 22, 2010 at 09:53:36PM +0200, Spiros Tzavellas wrote:
> Can you also rename the "filterNot" method to "reject"?

Personally I'm pretty used to filterNot, and it is the straight negation
of filter so introducing an unrelated word adds to cognitive load.
Conversely, partialMap/collect is a distinct concept.

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: partialMap -> collect ?

On Mon, Mar 22, 2010 at 4:21 PM, Paul Phillips wrote:
> On Mon, Mar 22, 2010 at 02:47:35PM +0100, martin odersky wrote:
>> Should we rename that to `collect'? I.e.:
>
> Yes, I prefer it, no question.  And I like that by removing it from *map
> methods, "map" always means that all elements turn up in the result.  If
> that is sufficiently decisive I can rename it immediately.
>
OK, please do!

> And then if you really want to make my day, let me throw in juust one
> more collections method, the only still unimplemented useful
> intersection of mapping, flattening, and filtering, which actually
> allows for all three in one very clear swing:
>
>  def flatCollect[B, That](pf: PartialFunction[A, Traversable[B]])(implicit bf: CanBuildFrom[Repr, B, That]): That
>    val b = bf(repr)
>    for (x <- coll collect pf)
>      b ++= x
>
>    b.result
>  }
>
I don't think flatCollect is that useful. For instance, instead of

xs flatCollect {
case x => ``some traversable''
}

you can always write

xs flatMap {
case x => ``some traversable''
case _ => Traversable.empty
}

So I'd leave it at collect.

Cheers

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland