In the following code, I need to stop processing the map if the element = 2:
val seq = Seq(1,2,3)
seq.map { x => if (x==2) /* stop processing the map */ }
<
You can't do it with map()
directly as map()
is designed to work over the entire collection, but you can split the collection into 2 parts and apply the map()
to one and not the other.
val seq = Seq(0,1,2,3,3,2,1,0)
val (before, after) = seq.span(_ != 2)
before.map(_ + 70) ++ after //res0: Seq[Int] = List(70, 71, 2, 3, 3, 2, 1, 0)