I am very new to Scala so this question may be very naive.
I have a list like this List[Int] = List(0, 3, 6, 12, 14, 15, 16, 17)
. I am trying to create a list like this [(0,3),(3,6),(6,12)..]
and so on. So far this is what I have tried:
val l1= List(0, 3, 6, 12, 14, 15, 16, 17)
var l2=scala.collection.mutable.ListBuffer[(Int,Int)]()
l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>{val newval=(x._1,l1(x._2+1)); l2+=newval})
Two questions here:
val newval
, i.e. try to do l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>l2+=(x._1,l1(x._2+1)))
, the compiler says:
<console>:10: error: type mismatch;
found : Int
required: (Int, Int)
l1.zipWithIndex.slice(0,l1.length-1).foreach(x=>l2+=(x._1,l1(x._2+1)))
. Why is that?+=
is a method on the ListBuffer
l2
that accepts repeated parameters. That means when you do something like this:
scala> var l2 = scala.collection.mutable.ListBuffer[(Int, Int)]()
l2: scala.collection.mutable.ListBuffer[(Int, Int)] = ListBuffer()
scala> l2 += (1, 2)
<console>:9: error: type mismatch;
found : Int(1)
required: (Int, Int)
l2 += (1, 2)
.. The compiler thinks you are trying to add multiple Int
s to the ListBuffer
, when you are trying to add a tuple. You need an extra set of parentheses.
l1.zipWithIndex.slice(0,l1.length-1).foreach(x=> l2 += ((x._1,l1(x._2+1)) ))
You can use sliding
, which will create a "sliding window" across the collection to return a list of lists of a specific group size, with a step size of one by default:
scala> List(0, 3, 6, 12, 14, 15, 16, 17).sliding(2)
.map { case List(a, b) => (a, b) }.toList
res10: List[(Int, Int)] = List((0,3), (3,6), (6,12), (12,14), (14,15), (15,16), (16,17))
besides the sliding, you could slide like following:
val l1= List(0, 3, 6, 12, 14, 15, 16, 17)
val l2 = l1.take(l1.size - 1).zip(l1.tail)
updated
l1.zip(l1.tail) works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With