- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
partialMap -> collect ?
Mon, 2010-03-22, 14:47
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
Mon, 2010-03-22, 16:57
#2
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/ *----------
>
Mon, 2010-03-22, 17:07
#3
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.
Mon, 2010-03-22, 17:17
#4
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
>
Mon, 2010-03-22, 17:27
#5
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
>>
>
Mon, 2010-03-22, 20:57
#6
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".
Mon, 2010-03-22, 21:37
#7
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.
Tue, 2010-03-23, 12:17
#8
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
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
}