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

Sorting

Example

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