I have a Map[String, Any] of donuts
val donuts: Seq[Map[String, Any]] = Seq(Map("type" -> "plain", "price" -> 1.5), Map("type" -> "jelly", "price" -> 2.5))
I want to find the highest priced donut using maxBy.
val d = donuts.maxBy(donut => donut("price").toString.toDouble)
successfully returns Map[String,Any] = Map(type -> "jelly", price -> 2.5)
I can access d("price") to find the price of the highest priced donut. But if I try to do this in one line:
donuts.maxBy(donut => donut("price").toString.toDouble)("price")
it throws:
error: type mismatch;
found : String("price")
required: Ordering[Double]
(donuts.maxBy(donut => donut("price").toString.toDouble))("price")
^
What's the issue here, and how can I roll this into one line?
That's because the
maxByrequires an implicit ordering parameter:Unfortunately you can't use apply's syntactic sugar, because whatever you pass as a second parameter list will be passed as if it were the implicit argument. You can call apply explicitly though:
It's not that pretty as what you're looking for but it's still a one liner.
Hope that helps :)