Skip to content

Instantly share code, notes, and snippets.

@alaz
Created July 6, 2009 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaz/141463 to your computer and use it in GitHub Desktop.
Save alaz/141463 to your computer and use it in GitHub Desktop.
/**
* 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