2016-12-15 30 views
1

scala.collection.immutable.List限定indexWhere,其返回滿足謂詞p,或-1這一般序列的第一個元素的索引,如果不存在如何使用scalaz IList的

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 

回答

0
val ilist = IList.fromList(List("hello", "world"))  
ilist.indexWhere(_.length > 10) // None 
ilist.indexWhere(_.length > 2) // Some(0) 
+0

thx。任何想法爲什麼scalaz沒有爲IList提供一個隱式類,以便我們可以避免實例化Ilist(因爲它在scalaz的其他地方完成,比如'List(List(1))。join')? –

+0

(他們可能使用了不同的方法名稱,這是唯一的問題) –

+0

我認爲,這只是爲了避免與相同的方法名稱混淆。 –