Advertisement
mitrakov

Operations over the List

Aug 19th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.64 KB | None | 0 0
  1. scala> val lst = List(1, 2, 3, 4, 5, 6, 7, 8)
  2. lst: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
  3.  
  4. // "sliding" moves the "window" of a given size across the collection:
  5. scala> lst.sliding(3).toList
  6. 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))
  7.  
  8. // you can also specify the count of steps for sliding:
  9. scala> lst.sliding(3, 3).toList
  10. res3: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8))
  11.  
  12. // "collect" applies a Partial Function to generate a new collection:
  13. scala> val ss = lst collect { case 2 => "two"; case 4 => "four"}
  14. ss: List[String] = List(two, four)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement