Access the next item in the list for comparison in Scala

I'm new to Scala, and I was wondering how you can invoke the next list item, because I'm trying to compare the current item with a neighboring item. Given that x is the current element, I tried similarly to java, x + 1, but that didn't work. Any help?

for (x <- list; if (x == (next adj. element))) println("same") 
+8
list scala
source share
8 answers

How about slipping?

 val list = List(1,2,3,4) list.sliding(2).foreach(println) //List(1, 2) //List(2, 3) //List(3, 4) 
+26
source share

Canonical ways to do this in a for loop:

 scala> val xs = List(1,2,3,4,3,2) xs: List[Int] = List(1, 2, 3, 4, 3, 2) scala> for (List(left,right) <- xs.sliding(2) if (left < right)) println(left + " < " + right) 1 < 2 2 < 3 3 < 4 scala> for ((left,right) <- (xs zip xs.tail) if (left < right)) println(left + " < " + right) 1 < 2 2 < 3 3 < 4 

(By the way, you are probably better off putting the if statement outside, rather than inside, for understanding in this example.)

If you have indexes instead of values, you simply search for them using the same template. Personally, I do not think this template is very clear or useful. It is slow, has weird angular scales with lists that aren't filled out, and it's hard to keep track of what's happening. Instead, I define

 class PairedIterable[A](it: Iterable[A]) { def foreachpair(f: (A,A) => Unit) = { val i = it.iterator if (i.hasNext) { var prev = i.next while (!ans && i.hasNext) { val x = i.next f(prev,x) prev = x } } } } implicit def iterable_has_pairs[A](it: Iterable[A]) = new PairedIterable(it) 

which can then be used as follows:

 scala> xs.foreachpair((left, right) => if (left < right) println(left + " < " + right)) 1 < 2 2 < 3 3 < 4 

The options forallpair, existpair, and findpair are especially useful.

+7
source share

This would be better handled by recursing over the list rather than iterating through the elements, since the elements know nothing about the list.

For example:

 def recurse[T](list: List[T]): Unit = list match { case List(x, y, _*) if x == y => println("same") recurse(list.tail) case Nil => case _ => recurse(list.tail) } 
+3
source share

As an option, you can use match and recursion instead of for :

 object Test { def main(args: Array[String]) { val list = List(1, 5, 3) loop(list) } def loop(list: List[Int]) { list match { case Nil => println("Empty list") case x :: Nil => println("last " + x) case x :: tail => { println(x + " - " + tail.head) loop(tail) } } } } 
+2
source share
 scala> val xs = 1::3::5::4::Nil xs: List[Int] = List(1, 3, 5, 4) scala> (xs, xs.tail).zip.foreach(println) (1,3) (3,5) (5,4) scala> 
+2
source share
 scala> val li = List (3, 4, 5) li: List[Int] = List(3, 4, 5) scala> li.tail.head res74: Int = 4 
0
source share

list.tail.head

gives the next element if you want to go through all the elements from the list. This is because the head is the front-most element and the tail is the rest of the list.

0
source share

As in Scala 2.11.7, the following are valid:

 scala> val xs = List(1,2,3,4) xs: List[Int] = List(1, 2, 3, 4) 

1) Fasten the tail

 scala> xs.zip(xs.tail) res0: List[(Int, Int)] = List((1,2), (2,3), (3,4)) 

2) Insert a window

 scala> xs.sliding(2) res1: Iterator[List[Int]] = non-empty iterator 
0
source share

All Articles