Created
July 6, 2009 14:41
-
-
Save alaz/141463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* In reply to http://blog.xebia.com/2009/07/04/real-world-functional-programming-in-scala-comparing-java-clojure-and-scala/ | |
*/ | |
def opt[T](x: Seq[T]): Seq[T] = if (x == null) Nil else x | |
def indexed[T](coll: Seq[T]): Seq[(Int, T)] = { | |
val c = opt(coll) | |
List.range(0,c.length).zip(c.toList) | |
} | |
def indexedFilter[T](pred:Seq[T], coll:Seq[T]): Seq[Int] = | |
for (p <- opt(pred); idxPair <- indexed(coll); if p == idxPair._2) yield idxPair._1 | |
def indexOfAny[T](pred: Seq[T], coll:Seq[T]): Option[Int] = | |
indexedFilter(pred, coll).firstOption |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment