Output of Iterable.sliding as Tuple

Posted by ziggystar on Stack Overflow See other posts from Stack Overflow or by ziggystar
Published on 2011-01-17T20:48:20Z Indexed on 2011/01/17 20:53 UTC
Read the original article Hit count: 136

Filed under:
|

The method sliding on collections returns a sliding window of given size in the form of X[Iterable[A]] with X being the type of the collection and A the element type. Often I need two or three elements and I prefer to have them named. One ugly workaround for sliding(2) is the following:

points.sliding(2).foreach{ twoPoints =>
      val (p1,p2) = (twoPoints.head,twoPoints.last)
      //do something
}

This sucks and only works for two elements. Also note that

(a,b) = (twoPoints(0),twoPoints(1))

doesn't work. Help me!

© Stack Overflow or respective owner

Related posts about scala

Related posts about collections