I'm trying to use an array of booleans to choose particular elements in another array. For example:
我正在尝试使用布尔数组来选择另一个数组中的特定元素。例如:
val arr = Seq("A", "B", "C")
val mask = Seq(true,false,true)
and I'd like the output to be a new array:
我希望输出成为一个新数组:
val arr_new = Seq("A","C")
Is there a way to achieve this in Scala?
有没有办法在Scala中实现这一目标?
1 个解决方案
#1
7
scala> arr.zip(mask).collect { case (v, true) => v }
res0: Seq[String] = List(A, C)
#1
7
scala> arr.zip(mask).collect { case (v, true) => v }
res0: Seq[String] = List(A, C)