Common Seq operations

// scala code
object Test {
// Seq operations
val xs = List(1, 2, 3, 4, 5)
xs.isDefinedAt(2)
xs(2)
xs.lengthCompare(xs.size) // same length, returns 0
xs.lengthCompare(xs.size + 1) // less than. returns -1
xs.lengthCompare(xs.size - 1) // less than. returns 1
xs.indexOf(5) // 4
val ys = List(1, 2, 2, 3, 4)
ys.indexOf(2) // 1
ys.lastIndexOf(2) // 2
ys.indexOfSlice(List(2, 2)) // 1
val zs = List(1, 2, 2, 3, 2, 2, 4)
zs.lastIndexOfSlice(List(2, 2)) // 4
xs.indexWhere(_ == 2)
val xs1 = List(1, 2, 2, 3, 2, 2, 2, 2)
xs1.segmentLength(_ == 2, 1)
xs1.prefixLength(_ > 1)

val xs2 = List(1, 1, 1, 1, 1, 1, 1)
val xs3 = List(2, 2, 2)
xs2.patch(1, xs3, 4) // drop the last 4 elems of xs2, starting from index 1, patch with xs3
xs.updated(1, 11)
// xs(1) = 11 // same as the above line, only for mutable collections
xs.reverse.sorted
xs.sortWith((x, y) => x < y)
xs.sortWith((x, y) => x > y)
xs.sortBy(_ % 3) // transform elems with modulo 3 and then sort
xs.reverseMap(_ + 1)
xs.startsWith(List(1, 2))
xs.endsWith(List(4, 5))
xs.contains(5)
xs.containsSlice(List(1, 2))
(xs.corresponds(xs.map(_ + 1)))((x, y) => y - x == 1) // test whether each pair (x_i, y_i) satisfies a predicate
xs.intersect(xs.tail).sameElements(xs.tail)
}