Authored by Win Myo Htet
The functions are quite easy and are groups by their directional counterpart. indeOf* return the first index of the element in the List if it is found otherwise -1. Then, the search from the last to head of the element is invoked by lastIndexOf*.
def indexOf (elem: A, from: Int): Int
[use case] Finds index of first occurrence of some value in this list after or at some start index.
def indexOf [B >: A] (elem: B, from: Int): Int
Finds index of first occurrence of some value in this list after or at some start index.
def indexOf (elem: A): Int
[use case] Finds index of first occurrence of some value in this list.
def indexOf [B >: A] (elem: B): Int
Finds index of first occurrence of some value in this list.
def lastIndexOf (elem: A, end: Int): Int
[use case] Finds index of last occurrence of some value in this list before or at a given end index.
def lastIndexOf [B >: A] (elem: B, end: Int): Int
Finds index of last occurrence of some value in this list before or at a given end index.
def lastIndexOf (elem: A): Int
[use case] Finds index of last occurrence of some value in this list.
def lastIndexOf [B >: A] (elem: B): Int
Finds index of last occurrence of some value in this list.
def indexOfSlice [B >: A] (that: Seq[B], from: Int): Int
def indexOfSlice [B >: A] (that: GenSeq[B], from: Int): Int
Finds first index after or at a start index where this list contains a given sequence as a slice.
def indexOfSlice [B >: A] (that: Seq[B]): Int
def indexOfSlice [B >: A] (that: GenSeq[B]): Int
Finds first index where this list contains a given sequence as a slice.
def lastIndexOfSlice [B >: A] (that: Seq[B], end: Int): Int
def lastIndexOfSlice [B >: A] (that: GenSeq[B], end: Int): Int
Finds last index before or at a given end index where this list contains a given sequence as a slice.
def lastIndexOfSlice [B >: A] (that: Seq[B]): Int
def lastIndexOfSlice [B >: A] (that: GenSeq[B]): Int
Finds last index where this list contains a given sequence as a slice.
def indexWhere (p: (A) ⇒ Boolean, from: Int): Int
Finds index of the first element satisfying some predicate after or at some start index.
def indexWhere (p: (A) ⇒ Boolean): Int
Finds index of first element satisfying some predicate.
def indices : Range
Produces the range of all indices of this sequence.
def lastIndexWhere (p: (A) ⇒ Boolean, end: Int): Int
Finds index of last element satisfying some predicate before or at given end index.
def lastIndexWhere (p: (A) ⇒ Boolean): Int
Finds index of last element satisfying some predicate.
scala> val charl=(for ( ch <- 'a' to 'z') yield ch) toList
charl: List[Char] = List(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
scala> val list = List('a','b','c','c','c','d','e','f','g','h','c','j','k','c','l','m')
list: List[Char] = List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)
scala> list indexOf 'c'
res0: Int = 2
scala> list indexOf('c',8)
res1: Int = 10
scala> list lastIndexOf('c')
res2: Int = 13
scala> list lastIndexOf('c',8)
res3: Int = 4
scala> list indexOfSlice(List('c','d','e'))
res4: Int = 4
scala> list indexOfSlice(List('c','d','e'),8)
res5: Int = -1
scala> list indexOfSlice(List('c','d','e'),2)
res6: Int = 4
scala> list lastIndexOfSlice(List('c','d','e'))
res7: Int = 4
scala> list lastIndexOfSlice(List('c','d','e'),2)
res8: Int = -1
scala> list lastIndexOfSlice(List('c','d','e'),6)
res9: Int = 4
scala> list indexWhere{ x => x == 'c'}
res10: Int = 2
scala> list indexWhere({ x => x == 'c'},8)
res11: Int = 10
scala> list lastIndexWhere({ x => x == 'c'},8)
res12: Int = 4
scala> list lastIndexWhere({ x => x == 'c'})
res13: Int = 13
intersect and union come from Math's set theory of intersect and union.
def intersect [B >: A] (that: Seq[B]): List[A]
def intersect (that: Seq[A]): List[A]
[use case] Computes the multiset intersection between this list and another sequence.
def intersect [B >: A] (that: GenSeq[B]): List[A]
Computes the multiset intersection between this list and another sequence.
def union (that: Seq[A]): List[A]
[use case] Produces a new sequence which contains all elements of this list and also all elements of a given sequence.
def union [B >: A, That] (that: GenSeq[B])(implicit bf: CanBuildFrom[List[A], B, That]): That
Produces a new sequence which contains all elements of this list and also all elements of a given sequence.
def union [B >: A, That] (that: Seq[B])(implicit bf: CanBuildFrom[List[A], B, That]): That
scala> list union charl
res26: List[Char] = List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
scala> list intersect charl
res27: List[Char] = List(a, b, c, d, e, f, g, h, j, k, l, m)
Here are the rest of the 'I' functions
def indices : Range
Produces the range of all indices of this sequence.
def isDefinedAt (x: Int): Boolean
Tests whether this list contains given index.
def isTraversableAgain : Boolean
Tests whether this list can be repeatedly traversed.
def iterator : Iterator[A]
Creates a new iterator over all elements contained in this iterable object.
Let us add a few more functions to finish off the 'L' functions.
def length : Int
The length of the list.
def lengthCompare (len: Int): Int
Compares the length of this list to a test value.
scala> list indices
res14: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
scala> list length
res15: Int = 16
scala> list lengthCompare 16
res16: Int = 0
scala> list lengthCompare 13
res17: Int = 1
scala> list lengthCompare 19
res18: Int = -3
scala> list isDefinedAt 19
res19: Boolean = false
scala> list isDefinedAt 14
res20: Boolean = true
scala> for (ch <- list iterator) println(ch)
a
b
c
c
c
d
e
f
g
h
c
j
k
c
l
m
Note lengthCompare function for 13(lesser than 16) does not return -3 but 1 while lengthCompare function for 13(greater than 16) return -3. isTraversableAgain will always return for the List collection since the method inherited from here is final. More of it on SO.
Authored by Win Myo Htet
No comments:
Post a Comment