Skip to content

stdlib

Try

  • Try.map[B](f: A => B): transform successful values
  • Try.flatMap[B](f: A => Try[B]): tries to transform successful values
  • Try.recover[B](f: PartialFunction[Throwable, A]): Try[B]: transforms some failures to successes
  • Try.recover[B](f: PartialFunction[Throwable, Try[A]]): Try[B]: tries to transforms some failures to successes

Future

  • map and flatMap evaluate after a future value is resolved
  • zip and traverse join together multiple Futures into a single Future and evaluated in parallel
  • recover and recoverWith handle failures

Tips and Tricks

  • Use foldLeft to do computation based on an optional value
  • Use Either.fold(identity, identity) to return whichever of the Left or Right value is present, provided both are of the same type
  • When to use List, cats.data.Chain or Vector
  • BP Use List as default, IterableOnce as parameter and then call to get an Iterator

foldLeft and foldRight

  • intuition
  • applies to lists or sequences that can be thought as consisting of Cons and Nil
  • foldRight is constructor substitution, replacing Cons and Nil with different functions
    • is lazy, looking at each element as needed
    • works on infinite sequences
    • obeys associativity but the order isn't guaranteed, specifically, does not imply that it works from right to left
  • foldLeft is loop replacement
    • is strict, looping through all elements