3

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.

1
  • Someone else has asked an almost identical question recently, only I can't find it right now. Could you expand on the use-case you have that needs this code? Commented Oct 1, 2014 at 17:16

2 Answers 2

4

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)
}
Sign up to request clarification or add additional context in comments.

5 Comments

and what about when the inside array's size is >100. How can you flatten that?
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.
@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.
@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.
I'm not really sure of anything right now :) I think the Array instances would work fine.
3

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))

2 Comments

This is getting me where I want to go. The output data types isn't exactly what I am looking for (the first element should be a string, not Double, but you couldn't tell that form the example I gave) but that's easy enough to change. Can you provide any explanation around "Array(xs @ _)", particularly what the "@ _" is doing? Thanks very much!
Look here about "@ _*" docs.scala-lang.org/tutorials/FAQ/finding-symbols.html. Roughly, it's a way to unnaply Array as vararg expansion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.