Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that looks like this Array((1,Array(1.0,0.0,3.0)), (2,Array(0.0,2.0,1.0))) that I want to turn into and array that looks like: Array((1,1.0,0.0,3.0),(2,0.0,2.0,1.0)).

Is there an easy way to do that? I'm guessing there is some sort of map I can do but I haven't been able to figure out the syntax.

Thanks.

share|improve this question

2 Answers 2

You could do this:

a.map { case (a, Array(b,c,d)) => (a,b,c,d) }

scala> val a = Array((1,Array(1.0,0.0,3.0)), (2,Array(0.0,2.0,1.0)))
a: Array[(Int, Array[Double])] = Array((1,Array(1.0, 0.0, 3.0)), (2,Array(0.0, 2.0, 1.0)))

scala> a.map({ case (a, Array(b,c,d)) => (a,b,c,d) })
res4: Array[(Int, Double, Double, Double)] = Array((1,1.0,0.0,3.0), (2,0.0,2.0,1.0))

A solution with support for up to 22-tuple. Of course, even this one assumes that all array members have the same lengths.

a.map {
  case (a, Array(b))           => (a,b)
  case (a, Array(b,c))         => (a,b,c)
  case (a, Array(b,c,d))       => (a,b,c,d)
  // pseudo-scala
  case (n1, Array(n2,...,n22)) => (n1,n2,...,n22)
}
share|improve this answer
    
and what about when the inside array's size is >100. How can you flatten that? –  eliasah 1 hour ago
    
That works great, the only problem is that is the number of elements in the array might be different depending on the data I feed to the function that generates the array (but would be consistent in any given instance of the array. so I might have Array((1,Array(1,2,3)),(2,Array(2,3,4)), but might later have Array((1,Array(1,2)),(2,Array(2,3)). Is there a way to genericize the expression of the array in the case statement? I'm new to Scala and so don;t understand the nuances of all the syntax. Thanks. –  J Calbreath 1 hour ago
    
@JCalbreath given that in Scala tuples support up to 22 elements, you can always list all 22 possible cases. For even more extensibility, you'd have to start using an HList implementation, such as the one in shapeless library. I don't have experience with that, though. –  Ionuț G. Stan 1 hour ago
    
@JCalbreath are you sure you don't want to construct Array instances instead of tuple instances? For example: Array(1, Array(2,3,4)) would become Array(Array(1,2,3,4)). An HList would be overfill for a collection with homogenous element types. –  Ionuț G. Stan 54 mins ago

Please specify the types of input and output. As i understand the task is Array[String, Array[Double]] => Array[Array[Double]]

 scala> val r = Array(("1", Array(1.0, 2.0, 1.0, 0.0, 3.0)), ("2", Array(0.0, 2.0, 1.0)))
r: Array[(String, Array[Double])] = Array((1,Array(1.0, 2.0, 1.0, 0.0, 3.0)), (2,Array(0.0, 2.0, 1.0)))

scala> val res = r map { case (s, Array(xs @ _*)) => (s.toDouble +: xs).toArray }
res: Array[Array[Double]] = Array(Array(1.0, 1.0, 2.0, 1.0, 0.0, 3.0), Array(2.0, 0.0, 2.0, 1.0))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.