Skip to content

Instantly share code, notes, and snippets.

@retrospectacus
Last active May 2, 2018 22:08
Show Gist options
  • Save retrospectacus/a38cf10b14a4589cb4ae90a96cd9ca6b to your computer and use it in GitHub Desktop.
Save retrospectacus/a38cf10b14a4589cb4ae90a96cd9ca6b to your computer and use it in GitHub Desktop.
case class Book(title: String, category: String, rating: Int)
val t = List(
Book("The dark", "Mystery", 90),
Book("What is", "Romance", 55),
Book("This is what", "Mystery", 80),
Book("FPIS", "Reference", 99),
Book("Darker", "Mystery", 80),
Book("It isn't", "Romance", 82)
)
def zer(x: List[Book]) = {
x.groupBy(_.category).head._2
}
zer(t)
def retro(x: List[Book]) = {
// x.groupBy(_.category).values.map(_.sortBy(_.rating).head)
x.groupBy(_.category).values.map(_.maxBy(_.rating))
}
retro(t)
.foreach {
case Book(t, c, _) =>
println(s"Top book for $c is $t")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment