In Scala, how to use Ordering[T] with List.min or List.max and keep code readable

前端 未结 6 583
轮回少年
轮回少年 2020-12-23 20:58

In Scala 2.8, I had a need to call List.min and provide my own compare function to get the value based on the second element of a Tuple2. I had to write this kind of code:

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-23 21:50

    list.min(Ordering.fromLessThan[(String, Int)](_._2 < _._2))
    

    Which is still too verbose, of course. I'd probably declare it as a val or object.

提交回复
热议问题