如何在Scala的对列表中惯用搜索?value
(element, value)
是否有比以下解决方案更好(也许更简洁和/或更有效)的方法?
code.find(_._1 == 2).get._2
code
类型在哪里List[(Int, String)]
?
scala> val code: List[(Int, String)] = (0, "zero") :: (1, "one") :: (2, "two") :: Nil
code: List[(Int, String)] = List((0,zero), (1,one), (2,two))
scala> code.find(_._1 == 2).get._2
res0: String = two