Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scala> val lst = List(1, 2, 3, 4, 5, 6, 7, 8)
- lst: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
- // "sliding" moves the "window" of a given size across the collection:
- scala> lst.sliding(3).toList
- res2: List[List[Int]] = List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6), List(5, 6, 7), List(6, 7, 8))
- // you can also specify the count of steps for sliding:
- scala> lst.sliding(3, 3).toList
- res3: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8))
- // "collect" applies a Partial Function to generate a new collection:
- scala> val ss = lst collect { case 2 => "two"; case 4 => "four"}
- ss: List[String] = List(two, four)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement