在集合中查找元素索引的优雅方法是什么?现在我实现了这样的方法:
def getIndexForValue[T] (value: T, collection: Iterable[T]): Option[Int] = {
val pair = collection.zipWithIndex.find(_._1 == value)
if (pair.isDefined) Some(pair.get._2) else None
}
可以用更优雅的方式重写吗?谢谢
在集合中查找元素索引的优雅方法是什么?现在我实现了这样的方法:
def getIndexForValue[T] (value: T, collection: Iterable[T]): Option[Int] = {
val pair = collection.zipWithIndex.find(_._1 == value)
if (pair.isDefined) Some(pair.get._2) else None
}
可以用更优雅的方式重写吗?谢谢