suppose I have two arrays
val x =Array("one","two","three")
val y =Array("1","2","3")
what's the most elegant way to get a new array like ["one1","two2","three3"]
Using zip
and map
should do it:
(x zip y) map { case (a, b) => a + b }
Similarly to @m-z, with a for comprehension, like this,
for ( (a,b) <- x zip y ) yield a + b
This can be encapsulated into an implicit such as
implicit class StrArrayOps(val x: Array[String]) extends AnyVal {
def join(y: Array[String]) =
for ( (a,b) <- x zip y ) yield a + b
}
and use it like this,
x join y
Array(one1, two2, three3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With