Finding elements in a scala list and also know which predicate has been satisfied

Filippo Tabusso picture Filippo Tabusso · Feb 1, 2010 · Viewed 11.6k times · Source

I have the following problem in scala. I have to find the first element in al list which satisfies a predicate function with two conditions in OR. The problem is that I would like to get the element but also know which of the two conditions has been satisfied. Here is a simple example:

val l1 = List("A", "B", "AA", "BB")
val l2 = List("AA", "BB", "A", "B")

def c1(s: String) = s.startsWith("B")
def c2(s: String) = s.length == 2

println(l1.find(s => c1(s) || c2(s)))
println(l2.find(s => c1(s) || c2(s)))

result is:

Some(B)
Some(AA)

For the l1 case I would like to have some return value (a String for example) indicating that c1 was satisfied (c2 for the l2 case). A possible solution could be to define a var before the test and set it within the c1 and c2 functions, but I would like to find a more "functional style" solution, maybe something that return a Tuple like: (element found, condition satisfied).

Thanks in advance for the help

Answer

Daniel C. Sobral picture Daniel C. Sobral · Feb 1, 2010

I'd do this:

Scala 2.8:

def find2p[T](l: List[T], p1: T => Boolean, p2: T => Boolean) = 
  l.view.map(el => (el, p1(el), p2(el))).find(t => t._2 || t._3)

Scala 2.7:

def find2p[T](l: List[T], p1: T => Boolean, p2: T => Boolean) = 
  l.projection.map(el => (el, p1(el), p2(el))).find(t => t._2 || t._3)

The view/projection ensures that the mapping will be done on-demand, instead of being applied to the whole list.