score:4
Accepted answer
as long as your arrays have the same length, this can be accomplished using zip
and map
define arrays
scala> array("1", "2", "3")
res0: array[string] = array(1, 2, 3)
scala> array("orange", "apple", "grape")
res1: array[string] = array(orange, apple, grape)
scala> array("milk", "juice", "cream")
res2: array[string] = array(milk, juice, cream)
zip
them together in two steps. zip
creates arrays of tuples
scala> res0 zip res1
res3: array[(string, string)] = array((1,orange), (2,apple), (3,grape))
scala> res3 zip res2
res4: array[((string, string), string)] = array(((1,orange),milk), ((2,apple),juice), ((3,grape),cream))
map
over the zipped result to transform the nested tuples to arrays
scala> res4 map {case ((a,b),c) => array(a,b,c) }
res5: array[array[string]] = array(array(1, orange, milk), array(2, apple, juice), array(3, grape, cream))
score:1
also for
val a = array("1", "2", "3")
val b = array("orange", "apple", "grape")
val c = array("milk", "juice", "cream")
then
(a,b,c).zipped.map{ case(x,y,z) => array(x,y,z) }
delivers
array(array(1, orange, milk), array(2, apple, juice), array(3, grape, cream))
update
yet a different approach to those using zippers, simpler
array(a,b,c).transpose
this allows for any number of input arrays, in contrast to zipper approachers which requires a priori knowledges of the number of input arrays for pattern-matching.
Source: stackoverflow.com
Related Query
- Merging Arrays in scala
- Merging two arrays in Scala
- Scala - printing arrays
- Why no immutable arrays in scala standard library?
- Scala - merging multiple iterators
- Combining Arrays in Scala
- Decoding structured JSON arrays with circe in Scala
- Merging two sequences in scala in an orderly fashion
- Interesting findings when combining Arrays in Scala
- scala dynamic multi dimensional mutable arrays like datastructures
- Element-wise sum of arrays in Scala
- Scala Arrays vs Vectors
- Scala operations with arrays performance (scalacl plugin)
- What is the fastest way to subtract two arrays in scala
- Merging rows into a single struct column in spark scala has efficiency problems, how do we do it better?
- Merging two Scala Future[List]
- Scala - Iterate Over Two Arrays
- Filtering empty arrays from array of arrays in Scala
- What is the idiomatic approach to perform elementwise addition to an Array of Arrays in Scala
- How can I get a sum of arrays of tuples in scala
- How to read csv file into an Array of arrays in scala
- Problems with running Android APK file when merging dex files using Scala
- Array.isDefinedAt for n-dimensional arrays in scala
- Merging elements in a scala list
- Adding value to arrays in scala
- Scala nested arrays flattening
- To which category Scala Arrays belong?
- Scala arrays and parameterized types
- JSON arrays and Scala Seq
- Scala - Pattern matching on arrays doesn't warn for missing cases
More Query from same tag
- Are these values empty or null and how do I drop these columns?
- Use global variable in Scala Spark
- How to execute only the outer for loop in scala?
- Scala - Remove all elements in a list/map of strings from a single String
- Scala: default return type of Option.getOrElse(...)
- Spark: Splitting JSON strings into separate dataframe columns
- Find a instance by field value comparison in Seq
- What the Scala submission file would look like when submitting a job to Condor?
- IntelliJ style problems after crashing
- Lift: create AJAX hyperlink for each item with CSS transform
- How to serialize sealed abstract class with Json4s in Scala?
- compilation error when a SIRD is added in the Play application
- Structured Streaming - Could not use FileContext API for managing metadata log files on AWS S3
- Scala val has to be guarded with synchronized for concurrent access?
- State in bolt/function in storm or trident
- Scala finding the last element in a Map list
- Matching type constraints in Scala fails
- How can I find where does a method definition rather then declaration in idea IDE?
- How do exception messages in Thrift exceptions work?
- Generic data structure with a union type in Scala
- Why can't I have new as a method for my Users controller and routes defined in one line in play framework?
- Why `majority-min-cap` set to 5 by default for Akka Sharding?
- How to use switch/case (simple pattern matching) in Scala?
- How to filter a column in Spark dataframe using a Array of strings?
- Scala dependency on Spark installation
- Parsing and manipulating json in Scala
- Spline Interpolation Performance in Scala vs Java
- Scala : URL rewriting (e.g. ../../someurl)
- Publish Site using SBT
- Respond to message received by TestProbe