Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between partition and groupBy?

Tags:

scala

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?

like image 890
nonamorando Avatar asked Jul 24 '15 18:07

nonamorando


2 Answers

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?

like image 186
Sascha Kolberg Avatar answered Sep 29 '22 07:09

Sascha Kolberg


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.

like image 29
Alexis C. Avatar answered Sep 29 '22 09:09

Alexis C.