如何使用 scalaz IList
how to use scalaz IList
scala.collection.immutable.List 定义了 indexWhere
,其中 returns 满足谓词 p 的此通用序列的第一个元素的索引,或 -1,如果 none存在:
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
val ilist = IList.fromList(List("hello", "world"))
ilist.indexWhere(_.length > 10) // None
ilist.indexWhere(_.length > 2) // Some(0)
scala.collection.immutable.List 定义了 indexWhere
,其中 returns 满足谓词 p 的此通用序列的第一个元素的索引,或 -1,如果 none存在:
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
val ilist = IList.fromList(List("hello", "world"))
ilist.indexWhere(_.length > 10) // None
ilist.indexWhere(_.length > 2) // Some(0)