Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. var k = -1
  2. for (i <- 0 until array.length)
  3. if ((i < array.length - 1) && array(i) < array(i + 1))
  4. k = i
  5.  
  6. scala> val xs = List(5, 4, 2, 3, 1)
  7. xs: List[Int] = List(5, 4, 2, 3, 1)
  8.  
  9. scala> val tail = xs.tail
  10. tail: List[Int] = List(4, 2, 3, 1)
  11.  
  12. scala> xs.zip(tail)
  13. res0: List[(Int, Int)] = List((5,4), (4,2), (2,3), (3,1)
  14.  
  15. scala> res0.indexWhere { case (x, y) => x < y }
  16. res1: Int = 2
  17.  
  18. val k = (array zip array.tail) lastIndexWhere { case (x, y) => x < y }
  19.  
  20. scala> Array(1,2,2,4,5,6, 6).sliding(2).toList
  21. res12: List[Array[Int]] = List(Array(1, 2), Array(2, 2), Array(2, 4), Array(4, 5), Array(5, 6), Array(6, 6))
  22.  
  23. Array(1,2,2,4,5,6, 6).sliding(2).indexWhere { case Array(x1, x2) => x1 == x2 }
  24.  
  25. Array(1,2,2,4,5,6, 6)
  26. .sliding(2) //splits each in to pairs
  27. .zipWithIndex //attaches the current index to each pair
  28. .collect { case (Array(x1, x2), index) if (x1 == x2) => index } //collect filters out non-matching pairs AND transforms them to just the inde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement