Say I have
val l1 = List(1,2,3,4)
val l2 = List(True,False,False,True)
I want to filter elements of l1 that corresponds to True elements in l2
I have done the following:
type Predicate[T] = T => Boolean
def filterXbasedOnY[A, B](X: List[A], Y: List[B], p: Predicate[B]): List[A] = {
{
for {i <- 0 until Y.size if p(Y(i))} yield X(i)
}.toList
}
which is working fine by calling:
val result = filterXbasedOnY(l1, l2, (b:Boolean) => b)
but is this the best way to accomplish this?
l1.zip(l2).filter(_._2).map(_._1)l1.zip(l2).collect{case (x,true) => x}