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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值