Reputation: 1605
I am reading through Twitter's Scala School right now and was looking at the groupBy
and partition
methods for collections. And I am not exactly sure what the difference between the two methods is.
I did some testing on my own:
scala> List(1, 2, 3, 4, 5, 6).partition(_ % 2 == 0)
res8: (List[Int], List[Int]) = (List(2, 4, 6),List(1, 3, 5))
scala> List(1, 2, 3, 4, 5, 6).groupBy(_ % 2 == 0)
res9: scala.collection.immutable.Map[Boolean,List[Int]] = Map(false -> List(1, 3, 5), true -> List(2, 4, 6))
So does this mean that partition
returns a list of two lists and groupBy
returns a Map with boolean keys and list values? Both have the same "effect" of splitting a list into two different parts based on a condition. I am not sure why I would use one over the other. So, when would I use partition
over groupBy
and vice-versa?
Upvotes: 6
Views: 1640
Reputation: 7152
groupBy
is better suited for lists of more complex objects.
Say, you have a class:
case class Beer(name: String, cityOfBrewery: String)
and a List of beers:
val beers = List(Beer("Bitburger", "Bitburg"), Beer("Frueh", "Cologne") ...)
you can then group beers by cityOfBrewery
:
val beersByCity = beers.groupBy(_.cityOfBrewery)
Now you can get yourself a list of all beers brewed in any city you have in your data:
val beersByCity("Cologne") = List(Beer("Frueh", "Cologne"), ...)
Neat, isn't it?
Upvotes: 5
Reputation: 62835
Partition is when you need to split some collection into two basing on yes/no logic (even/odd numbers, uppercase/lowecase letters, you name it). GroupBy has more general usage: producing many groups, basing on some function. Let's say you want to split corpus of words into bins depending on their first letter (resulting into 26 groups), it simply not possible with .partition
.
Upvotes: 2
Reputation: 93842
And I am not exactly sure what the difference between the two methods is.
The difference is in their signature. partition
expects a function A => Boolean
while groupBy
expects a function A => K
.
It appears that in your case the function you apply with groupBy
is A => Boolean
too, but you don't want always to do this, sometimes you want to group by a function that don't always returns a boolean based on its input.
For example if you want to group a List of strings by their length, you need to do it with groupBy
.
So, when would I use partition over groupBy and vice-versa?
Use groupBy
if the image of the function you apply is not in the boolean set (i.e f(x)
for an input x yield another result than a boolean). If it's not the case then you can use both, it's up to you whether you prefer a Map
or a (List, List)
as output.
Upvotes: 4