【发布时间】:2016-12-15 09:58:08
【问题描述】:
scala.collection.immutable.List 定义了indexWhere,它返回满足谓词 p 的一般序列的第一个元素的索引,如果不存在,则返回 -1:
def indexWhere(p: (A) ⇒ Boolean): Int
所以,我可以使用:
List("hello", "world").indexWhere(_.length > 10) // -1
但是,我更希望获得Option[Int]。我看到这是在scalaz.IList中实现的:
def indexWhere(f: A => Boolean): Option[Int]
如何使用scalaz.IList.indexWhere?
我尝试导入 scalaz,但仍然得到 -1。
import scalaz._
import std.list._
List("hello", "world").indexWhere(_.length > 10) // -1 instead of None
【问题讨论】:
标签: scalaz