scala notes (4) - collection

- Collection



  • Array is equivalent of Java array, it's mutable in terms of value update. but not size

  • sequence

  1. Vector is immutable equivalent of ArrayBuffer which is indexed sequence with fast random access. Vector is implemented as tree with one node having up to 32 children.
  2. Range is integer sequence. sequence values are not stored however. only start, end and increment are needed.
  3. List, Nil is empty list. List.head, List.tail .  ListBuffer for the mutable equivalent . prepend is more faster than append. use list.reverse after prepend
9 :: List(4, 2)

9 :: 4 :: 2 :: Nil

def sum(lst: List[Int]): Int = lst match {
    case Nil => 0
    case h :: t => h + sum(t) // h is lst.head, t is lst.tail
}
  1. Set, distinct elements not ordered. LinkedHashSet to keep insertion order. SortedSet. BitSet (the ith bit is set to 1 if i is in set)
val digits = Set(1, 7, 2, 9)
digits contains 0 // false
Set(1, 2) subsetOf digits // true

union, intersect and diff
  1. add or remove elements

1. Append (:+) or prepend (+:) to a sequence. //for Vector
2. Add (+) to an unordered collection.
3. Remove with the - operator.
4. Use ++ and -- for bulk add and remove.
5. Mutations are += ++= -= --=.
6. For lists, many Scala programmers prefer the :: and ::: operators.
7. Stay away from ++: +=: ++=:.

  • Stream, is an immutable list in which tail is computed lazily.


def numsFrom(n: BigInt): Stream[BigInt] = n #:: numsFrom(n + 1) // #:: generate stream

val tenOrMore = numsFrom(10) // Stream(10, ?)
val squares = numsFrom(1).map(x => x * x) // stream methods are executed lazily too. Stream(1, ?)

use squares.take(5).force to force evaluation. stream caches its values being evaluated

  • Lazy view, like stream, but first element is not evaluated 
val palindromicSquares = (1 to 1000000).view
.map(x => x * x)
.filter(x => x.toString == x.toString.reverse)

palindromicSquares.take(10).mkString(",") // view doesn't cache values
view(i) causes evaluation of entire view. use view.take(i).last instead.
  • parallel collection

coll.par.sum

coll.par.count(_ % 2 == 0)

for (i <- (0 until 100000).par) print(s" $i")


  • aggregate operations

reduce, reduce*, fold -> result type is same or super type of element type. 

foldLeft,  foldRight ->  result type can be different than element type


reduce and fold -> Elements can be oprated in parallel.

reduce* and fold* -> elements operation need to be in order



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章