Reputation: 15
I need to replicate the result from this array definition using an external file.
scala> val data = Seq(Array(Array(1, 2), Array(3)),Array(Array(1), Array(3, 2), Array(1, 2)),Array(Array(1, 2), Array(5)),Array(Array(6)))
data: Seq[Array[Array[Int]]] = List(Array(Array(1, 2), Array(3)), Array(Array(1), Array(3, 2), Array(1, 2)), Array(Array(1, 2), Array(5)), Array(Array(6)))
I tried creating a testdataI.txt file but can't make it to work.
testdataI.txt ->
1,2
3
1
3,2
1,2
1,2
5
6
Here the result when I do the conversion using io.Source
:
import scala.io.Source
scala> val data = Seq(Source.fromFile("/tmp/testdataI.txt").getLines().map(_.split(",").map(_.trim.toInt)).toArray)
data: Seq[Array[Array[Int]]] = List(Array(Array(1, 2), Array(3), Array(1), Array(3, 2), Array(1, 2), Array(1, 2), Array(5), Array(6)))
The outcome should look like this (A series of Multidimensional Arrays)
data: Seq[Array[Array[Int]]] = List(Array(Array(1, 2), Array(3)), Array(Array(1), Array(3, 2), Array(1, 2)), Array(Array(1, 2), Array(5)), Array(Array(6)))
I found a lot of Multidimensional array information but nothing for this specific case.
Really appreciate,
Fredy A Gomez
Upvotes: 0
Views: 835
Reputation: 1511
No idea why you want to structure the values like that, but here's how you can do it:
scala> import scala.io.Source
import scala.io.Source
scala> val take = List(2, 3, 2, 1)
take: List[Int] = List(2, 3, 2, 1)
scala> val data = Source.fromFile("/tmp/testdataI.txt").getLines().map(_.split(",").map(_.trim.toInt).toList).toList
data: List[List[Int]] = List(List(1, 2), List(3), List(1), List(3, 2), List(1, 2), List(1, 2), List(5), List(6))
scala> def awesomeGrouped(ungrouped: List[List[Int]], take: List[Int]): List[List[List[Int]]] = take match {
| case Nil => Nil
| case t :: ts => ungrouped.take(t) :: awesomeGrouped(ungrouped.drop(t), ts)
| }
awesomeGrouped: (ungrouped: List[List[Int]], take: List[Int])List[List[List[Int]]]
scala> def fixTypes(grouped: List[List[List[Int]]]) = grouped.map(_.map(_.toArray).toArray)
fixTypes: (sorted: List[List[List[Int]]])List[Array[Array[Int]]]
scala> fixTypes(awesomeGrouped(data, take))
res0: List[Array[Array[Int]]] = List(Array(Array(1, 2), Array(3)), Array(Array(1), Array(3, 2), Array(1, 2)), Array(Array(1, 2), Array(5)), Array(Array(6)))
The part that makes everyone uneasy is the take
list distribution you've chosen; it seems arbitrary.
Note I added the fixTypes
function specifically to return the exact return types you want. But arrays are not very idiomatic Scala; are you sure you need them? If not, just remove the fixTypes
function and invocation.
Upvotes: 1