What is the difference between sections and groupBy?
I read through the Twitter Scala School right now and look at the methods groupBy
and partition
for the collection. And I'm not really sure what the difference is between the two methods.
I did some tests myself:
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))
Does this mean that it partition
returns a list of two lists, but 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'm not sure why I would use one over the other. So when will I use partition
over groupBy
and vice versa?
source to share
groupBy
better for lists of more complex objects.
Let's say you have a class:
case class Beer(name: String, cityOfBrewery: String)
and beer list:
val beers = List(Beer("Bitburger", "Bitburg"), Beer("Frueh", "Cologne") ...)
you can group beer cityOfBrewery
:
val beersByCity = beers.groupBy(_.cityOfBrewery)
Now you can get a list of all beers in any city you have in your data:
val beersByCity("Cologne") = List(Beer("Frueh", "Cologne"), ...)
Gently, right?
source to share
And I'm not really sure what the difference is between the two methods.
The difference lies in their signature. partition
expects a function A => Boolean
and groupBy
expects a function A => K
.
It looks like in your case the function you are applying along with groupBy
does too A => Boolean
, but you don't always want to do that, sometimes you want to group a function that doesn't always return a boolean based on its input.
For example, if you want to group a list of strings by their length, you need to do so with groupBy
.
So when will I use the section above groupBy and vice versa?
Use groupBy
if the image of the function being applied is not in a boolean set (ie f(x)
for inputting x gives a different result than boolean). If it is not, you can use both, regardless of whether you prefer the result Map
or (List, List)
.
source to share
Splitting is when you need to split some collection in two based on yes / no logic (even / odd numbers, uppercase letters / lowecase, you name it). GroupBy has a more general use: creating many groups based on some function. Suppose you want to split a chunk of words into boxes depending on their first letter (the result is 26 groups), this is simply not possible with .partition
.
source to share