Thursday, January 5, 2012

Learning Scala : Reading the exotic and essential List API scaladoc 14

Authored by Win Myo Htet

Having gone through the needed explanation in the beginning of the series, there is little need to elaborate for most of the stuff except to show the code snippet. Let's just try to cover all the remaining functions in this blog since it has been a long series!

``````def max : A
[use case] Finds the largest element.
def max [B >: A] (implicit cmp: Ordering[B]): A
Finds the largest element.
def maxBy [B] (f: (A) ⇒ B)(implicit cmp: Ordering[B]): A
def min : A
[use case] Finds the smallest element.
def min [B >: A] (implicit cmp: Ordering[B]): A
Finds the smallest element.
def minBy [B] (f: (A) ⇒ B)(implicit cmp: Ordering[B]): A

``````
We can simply call max or min and let the Scala implicit function take care of the comparing or we can convert the element to our liking for unique comparison by using maxBy/minBy.
``````def padTo (len: Int, elem: A): List[A]
[use case] Appends an element value to this list until a given target length is reached.
def padTo [B >: A, That] (len: Int, elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That
Appends an element value to this list until a given target length is reached.
def patch [B >: A, That] (from: Int, patch: Seq[B], replaced: Int)(implicit bf: CanBuildFrom[List[A], B, That]): That
def patch (from: Int, that: GenSeq[A], replaced: Int): List[A]
[use case] Produces a new list where a slice of elements in this list is replaced by another sequence.
def patch [B >: A, That] (from: Int, patch: GenSeq[B], replaced: Int)(implicit bf: CanBuildFrom[List[A], B, That]): That
Produces a new list where a slice of elements in this list is replaced by another sequence.
def permutations : Iterator[List[A]]
Iterates over distinct permutations.
``````

``````def repr : List[A]
The collection of type list underlying this TraversableLike object.
def reverse : List[A]
Returns new list wih elements in reversed order.
def reverseIterator : Iterator[A]
An iterator yielding elements in reversed order.
def reverseMap [B] (f: (A) ⇒ B): List[B]
[use case] Builds a new collection by applying a function to all elements of this list and collecting the results in reversed order.
def reverseMap [B, That] (f: (A) ⇒ B)(implicit bf: CanBuildFrom[List[A], B, That]): That
Builds a new collection by applying a function to all elements of this list and collecting the results in reversed order.
def reverse_::: (prefix: List[A]): List[A]
[use case] Adds the elements of a given list in reverse order in front of this list.
def reverse_::: [B >: A] (prefix: List[B]): List[B]
Adds the elements of a given list in reverse order in front of this list.
``````

``````scala> val list = (for (i <- 1 to 11) yield i) toList
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

scala> list min
res0: Int = 1

scala> list max
res1: Int = 11

scala> list minBy {x => -x}
res2: Int = 11

res3: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11)

res4: List[AnyVal] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, a, a, a, a, a, a, a, a)

scala> list patch (5,List('a','b','c'),3)
res5: List[AnyVal{def getClass(): java.lang.Class[_ >: Int with Char <: AnyVal]}] = List(1, 2, 3, 4, 5, a, b, c, 9, 10, 11)

scala> list patch (5,List('a','b','c'),1)
res6: List[AnyVal{def getClass(): java.lang.Class[_ >: Int with Char <: AnyVal]}] = List(1, 2, 3, 4, 5, a, b, c, 7, 8, 9, 10, 11)

scala> list repr
res7: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

scala>  list reverse
res8: List[Int] = List(11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

scala> for (i <- list reverseIterator) println(i)
11
10
9
8
7
6
5
4
3
2
1

scala> list reverseMap { x => "-"+x+"-"}
res10: List[java.lang.String] = List(-11-, -10-, -9-, -8-, -7-, -6-, -5-, -4-, -3-, -2-, -1-)

scala> list.reverse:::(List('a','b','c'))
res11: List[AnyVal{def getClass(): java.lang.Class[_ >: Char with Int <: AnyVal]}] = List(11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, a, b, c)

scala> val pl=List(1,2,3)
pl: List[Int] = List(1, 2, 3)

scala> (pl permutations) toList
res12: List[List[Int]] = List(List(1, 2, 3), List(1, 3, 2), List(2, 1, 3), List(2, 3, 1), List(3, 1, 2), List(3, 2, 1))

``````

We will start a new repl session for the rest of the functions.
``````def segmentLength (p: (A) ⇒ Boolean, from: Int): Int
Computes length of longest segment whose elements all satisfy some predicate.
def seq : Seq[A]
A version of this collection with all of the operations implemented sequentially (i.
def size : Int
The size of this list, equivalent to length.
def slice (from: Int, until: Int): List[A]
Selects an interval of elements.
def sortBy [B] (f: (A) ⇒ B)(implicit ord: Ordering[B]): List[A]
Sorts this List according to the Ordering which results from transforming an implicitly given Ordering with a transformation function.
def sortWith (lt: (A, A) ⇒ Boolean): List[A]
Sorts this list according to a comparison function.
def sorted [B >: A] (implicit ord: Ordering[B]): List[A]
Sorts this list according to an Ordering.
def span (p: (A) ⇒ Boolean): (List[A], List[A])
Splits this list into a prefix/suffix pair according to a predicate.
def splitAt (n: Int): (List[A], List[A])
Splits this list into two at a given position.
``````

``````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 segmentLength({x => x == 'c'},0)
res0: Int = 0

scala> list segmentLength({x => x == 'c'},2)
res1: Int = 3

scala> list seq
res2: scala.collection.immutable.Seq[Char] = List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> list size
res3: Int = 16

scala> list slice(3,8)
res4: List[Char] = List(c, c, d, e, f)

scala> list sorted
res5: List[Char] = List(a, b, c, c, c, c, c, d, e, f, g, h, j, k, l, m)

scala> val intl= List(3,2,5,4,8,0,4,1,5,76,9,0,4,3,2,24,45,657)
intl: List[Int] = List(3, 2, 5, 4, 8, 0, 4, 1, 5, 76, 9, 0, 4, 3, 2, 24, 45, 657)

scala> intl sorted
res6: List[Int] = List(0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 8, 9, 24, 45, 76, 657)

scala> intl sortBy {x => -x}
res7: List[Int] = List(657, 76, 45, 24, 9, 8, 5, 5, 4, 4, 4, 3, 3, 2, 2, 1, 0, 0)

scala> intl sortWith {(x,y) => x < y}
res8: List[Int] = List(0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 8, 9, 24, 45, 76, 657)

scala> (intl sorted) span {x => x % 2 == 0}
res9: (List[Int], List[Int]) = (List(0, 0),List(1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 8, 9, 24, 45, 76, 657))

scala> (intl sorted) span {x => x < 10}
res10: (List[Int], List[Int]) = (List(0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 8, 9),List(24, 45, 76, 657))

scala> intl splitAt 7
res11: (List[Int], List[Int]) = (List(3, 2, 5, 4, 8, 0, 4),List(1, 5, 76, 9, 0, 4, 3, 2, 24, 45, 657))

scala> val words = "The quick brown fox jumped over the lazy dog".split(' ')
words: Array[String] = Array(The, quick, brown, fox, jumped, over, the, lazy, dog)

res12: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped)

scala> List("Steve", "Tom", "John", "Bob").sortWith(_.compareTo(_) < 0)
res13: List[java.lang.String] = List(Bob, John, Steve, Tom)

``````

For the function toMap, the elements have to be tuple of 2 but I am lazy to create such List and instead, use the function zip, which we will see next.
``````def toArray : Array[A]
[use case] Converts this list to an array.
def toArray [B >: A] (implicit arg0: ClassManifest[B]): Array[B]
Converts this list to an array.
def toBuffer [B >: A] : Buffer[B]
Converts this list to a mutable buffer.
def toIndexedSeq [B >: A] : IndexedSeq[B]
Converts this list to an indexed sequence.
def toIterable : Iterable[A]
Converts this list to an iterable collection.
def toIterator : Iterator[A]
Returns an Iterator over the elements in this list.
def toList : List[A]
Converts this list to a list.
def toMap [T, U] : Map[T, U]
[use case] Converts this list to a map.
def toMap [T, U] (implicit ev: <:<[A, (T, U)]): Map[T, U]
Converts this list to a map.
def toSeq : Seq[A]
Converts this list to a sequence.
def toSet [B >: A] : Set[B]
Converts this list to a set.
def toStream : Stream[A]
Converts this list to a stream.
def toString (): String
Converts this list to a string.
def toTraversable : Traversable[A]
Converts this list to an unspecified Traversable.
def transpose [B] (implicit asTraversable: (A) ⇒ TraversableOnce[B]): Traversable[Traversable[B]]
Transposes this list of traversable collections into a list of lists.
``````

``````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> val intl= List(3,2,5,4,8,0,4,1,5,76,9,0,4,3,2,24,45,657)
intl: List[Int] = List(3, 2, 5, 4, 8, 0, 4, 1, 5, 76, 9, 0, 4, 3, 2, 24, 45, 657)

scala> list toArray
res0: Array[Char] = Array(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> list toBuffer
res1: scala.collection.mutable.Buffer[Char] = ArrayBuffer(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> list toIndexedSeq
res2: scala.collection.immutable.IndexedSeq[Char] = Vector(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> for ( x <- list toIterable) println(x)
a
b
c
c
c
d
e
f
g
h
c
j
k
c
l
m

scala> for ( x <- list toIterator) println(x)
a
b
c
c
c
d
e
f
g
h
c
j
k
c
l
m

scala>  list toList
res5: List[Char] = List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> (list zip intl) toMap
res6: scala.collection.immutable.Map[Char,Int] = Map(e -> 4, j -> 0, f -> 1, a -> 3, m -> 24, b -> 2, g -> 5, l -> 2, c -> 3, h -> 76, k -> 4, d -> 0)

scala> list toSeq
res7: scala.collection.immutable.Seq[Char] = List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> list toSet
res8: scala.collection.immutable.Set[Char] = Set(e, j, f, a, m, b, g, l, c, h, k, d)

scala> list toStream
res9: scala.collection.immutable.Stream[Char] = Stream(a, ?)

scala> list toString
res10: String = List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> for (x <- list toTraversable) println(x)
a
b
c
c
c
d
e
f
g
h
c
j
k
c
l
m

scala> val l = List(List(1, 2, 3), List(4, 5,6), List(7, 8, 9))
l: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))

scala> l.transpose
res12: List[List[Int]] = List(List(1, 4, 7), List(2, 5, 8), List(3, 6, 9))

``````

I didn't know that Scala have zip and Stream as an infinite List when I am writing "Learning Scala: doing Scala exercise from Haskell's example". Should have gone through List API before!
``````def unzip [A1, A2] (implicit asPair: (A) ⇒ (A1, A2)): (List[A1], List[A2])
Converts this list of pairs into two collections of the first and second half of each pair.
def unzip3 [A1, A2, A3] (implicit asTriple: (A) ⇒ (A1, A2, A3)): (List[A1], List[A2], List[A3])
Converts this list of triples into three collections of the first, second, and third element of each triple.
def updated (index: Int, elem: A): List[A]
[use case] A copy of this list with one single replaced element.
def updated [B >: A, That] (index: Int, elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That
A copy of this list with one single replaced element.
def view (from: Int, until: Int): SeqView[A, List[A]]
Creates a non-strict view of a slice of this list.
def view : SeqView[A, List[A]]
Creates a non-strict view of this list.
def zip [A1 >: A, B, That] (that: Iterable[B])(implicit bf: CanBuildFrom[List[A], (A1, B), That]): That
def zip [B] (that: GenIterable[B]): List[(A, B)]
[use case] Returns a list formed from this list and another iterable collection by combining corresponding elements in pairs.
def zip [A1 >: A, B, That] (that: GenIterable[B])(implicit bf: CanBuildFrom[List[A], (A1, B), That]): That
Returns a list formed from this list and another iterable collection by combining corresponding elements in pairs.
def zipAll [B, A1 >: A, That] (that: Iterable[B], thisElem: A1, thatElem: B)(implicit bf: CanBuildFrom[List[A], (A1, B), That]): That
def zipAll [B] (that: Iterable[B], thisElem: A, thatElem: B): List[(A, B)]
[use case] Returns a list formed from this list and another iterable collection by combining corresponding elements in pairs.
def zipAll [B, A1 >: A, That] (that: GenIterable[B], thisElem: A1, thatElem: B)(implicit bf: CanBuildFrom[List[A], (A1, B), That]): That
Returns a list formed from this list and another iterable collection by combining corresponding elements in pairs.
def zipWithIndex : List[(A, Int)]
[use case] Zips this list with its indices.
def zipWithIndex [A1 >: A, That] (implicit bf: CanBuildFrom[List[A], (A1, Int), That]): That
Zips this list with its indices.
``````

``````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> val intl= List(3,2,5,4,8,0,4,1,5,76,9,0,4,3,2,24,45,657)
intl: List[Int] = List(3, 2, 5, 4, 8, 0, 4, 1, 5, 76, 9, 0, 4, 3, 2, 24, 45, 657)

scala> list zip intl
res0: List[(Char, Int)] = List((a,3), (b,2), (c,5), (c,4), (c,8), (d,0), (e,4), (f,1), (g,5), (h,76), (c,9), (j,0), (k,4), (c,3), (l,2), (m,24))

scala> (list zip intl) unzip
res1: (List[Char], List[Int]) = (List(a, b, c, c, c, d, e, f, g, h, c, j, k, c, l, m),List(3, 2, 5, 4, 8, 0, 4, 1, 5, 76, 9, 0, 4, 3, 2, 24))

scala>  list updated(1,'c')
res2: List[Char] = List(a, c, c, c, c, d, e, f, g, h, c, j, k, c, l, m)

scala> list view(3,8)
res3: scala.collection.SeqView[Char,List[Char]] = SeqViewS(...)

scala> list zipAll(intl,"*","-1")
res4: List[(Any, Any)] = List((a,3), (b,2), (c,5), (c,4), (c,8), (d,0), (e,4), (f,1), (g,5), (h,76), (c,9), (j,0), (k,4), (c,3), (l,2), (m,24), (*,45), (*,657))

``````

After such a marathon series, it is finished!

Authored by Win Myo Htet

1 comment:

1. Lucky Tiger Casino | Casino - KT Hub
Lucky 원주 출장안마 Tiger Casino is in the 인천광역 출장안마 entertainment district of Western 경상남도 출장마사지 Cape and provides gaming 바카라 게임 to operators. This casino is owned and operated by Kambi Resorts Limited, 포항 출장안마