Scala notes

wait and see

Fold methods

foldRight

List(a, b, c).foldRight(e)(f) = f(a, f(b, f(c, e)))

foldLeft

List(a, b, c).foldLeft(e)(f) = f(f(f(e, a), b), c)

Example

val ns = (1 to noTimes).toList
val attempts = ns.map(_ => () => block) // this delays execution
val failed = Future.failed(new Exception("boom"))val result = attempts.foldLeft (failed) ( (a, block) => a recoverWith { block() })result

Collections hierarchy

  • Traversable[T] – collections of elements with type T, with operations implemented using foreach
  • Iterable[T] – collections of elements with type T, with operations implemented using iterator
  • Seq[T] – an ordered sequence of elements of type T
  • Set[T] – a set of elements of type T that doesn’t allow duplicates
  • Map[K, V] – key value pairs, no duplicate keys allowed

Sorting

Example

list.sortWith( (prev, next) => prev._2 > next._2)