1
votes

I would want to fill a Seq[Seq[Int]] instance with an existing Seq[Int] by using the method fill(n1, n2)(element : A) of the Object Seq, as detailed in the documentation : http://www.scala-lang.org/api/2.12.3/scala/collection/Seq$.html#fillA(elem:=>A):CC[CC[A]]

So I wrote this call :

Seq.fill(width, width)(_random_values.) , with _random_values the existing Seq[Int].

The problem is that the parameter of the second list of fill is an element, not a Seq. So, what could I type to iterate on each _random_values's integers AND to execute the Seq.fill for each current integer ?

1
if you have a Seq(1,2,3) what would be the desired outcome?Sebastian
If I have width = 2 and a Seq(1, 2, 3, 4), my outcome would be Seq(Seq(1, 2) , Seq(3, 4)). (NB : width = "dimension")JarsOfJam-Scheduler

1 Answers

5
votes

Seq.fill is more appropriate to create a Seq base on a static value. For your use case Seq.grouped might be a better fit:

val values: Seq[Int] = List(1, 2, 3, 4)

val result: Seq[Seq[Int]] = values.grouped(2).toSeq

result.foreach(println)

/*
List(1, 2)
List(3, 4)
*/