scala> //PS:116 scala> //psn > a40 > a30 scala> scala> //Example : toSeq scala> val myList = Seq('a', 'b', 'c', 'd', 'e', 'f', 'g') myList: Seq[Char] = List(a, b, c, d, e, f, g) scala> val slideIter = myList.sliding(2) slideIter: Iterator[Seq[Char]] = non-empty iterator scala> // Wo do not get the result ; the result of the sequence will scala> // be evaluated lazily(on when needed) scala> val resultSeq1 = slideIter.toSeq resultSeq1: Seq[Seq[Char]] = Stream(List(a, b), ?) scala> val resultSeq2 = slideIter.toSeq resultSeq2: Seq[Seq[Char]] = Stream(List(b, c), ?) scala> resultSeq2.size res51: Int = 5 scala> for (element <- resultSeq2) { | // Results are evaluated here as we looping through the elements | // in the sequence | println(element) | } List(b, c) List(c, d) List(d, e) List(e, f) List(f, g)
scala> //Example: toList scala> val myList = Seq('a', 'b', 'c', 'd', 'e', 'f', 'g') myList: Seq[Char] = List(a, b, c, d, e, f, g) scala> val slideIter = myList.sliding(2) slideIter: Iterator[Seq[Char]] = non-empty iterator scala> val resultSeq1 = slideIter.toSeq resultSeq1: Seq[Seq[Char]] = Stream(List(a, b), ?) scala> //toList() immediately returs the result immediately which is scala> //different from toSeq() ; where the Sequence data is lazily scala> //evaluated scala> val resultList = slideIter.toList resultList: List[Seq[Char]] = List(List(b, c), List(c, d), List(d, e), List(e, f), List(f, g))
No comments:
Post a Comment