Skip to content

Instantly share code, notes, and snippets.

@rlmark
Last active August 27, 2019 21:19
Show Gist options
  • Save rlmark/d085896620e0702124f684f61455555b to your computer and use it in GitHub Desktop.
Save rlmark/d085896620e0702124f684f61455555b to your computer and use it in GitHub Desktop.
Functional Programming in Scala Chapter 4 part 2

Chapter 4 part 2: Eithers!

Review

  • Function composition
  • Higher order functions
  • Curry/Uncurry
  • Referential transparency
  • List (map, flatMap, filter, foldL, foldR)
  • Option (map, flatMap, filter)

Scala Either

sealed trait Either[+E, +A]
case class Left[+E](value: E) extends Either[E, Nothing]
case class Right[+A](value: A) extends Either[Nothing, A]
  • Error handling with additional information
  • Monad!
  • Coproduct (sum) data type

More about Coproducts(sums) and Products

Coproducts represent when a data type can be one thing or another Products represent when a data type can be many things at once.

Are the following examples of Coproducts or products?

  • Case classes
  • Sealed trait hierarchies
  • Tuples
  • HLists (Heterogeneous lists val demo = 42 :: "Hello" :: User("Julien") :: HNil)
  • Option

What methods we went over

  • map, flatmap, map2, orElse, traverse, sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment