I've got
val l List[List[Int]] = List(List(0,0,0),List(1,1,1))
and I want to get it zipped. Is there any succinct way to do it instead of accessing each element of main list by index and zip it together with zip
command?
I've got
val l List[List[Int]] = List(List(0,0,0),List(1,1,1))
and I want to get it zipped. Is there any succinct way to do it instead of accessing each element of main list by index and zip it together with zip
command?
This works, under the assumption that the outer list has exacly two inner lists.
List(List(0,0,0), List(1, 1, 1)) match {
case l1 :: l2 :: Nil => l1 zip l2
case x => ??? // up to you
}
In order to take into account the possibility of failure, you can wrap the result into an Option
.
val x = List(List(0,0,0), List(1, 1, 1))
val res: Option[List[Int]] = x match {
case l1 :: l2 :: Nil => Some(l1 zip l2)
case x => None
}