September 2018
Intermediate to advanced
398 pages
9h 43m
English
We covered some challenging concepts in this chapter. Type classes are also used in other functional programming languages, such as Haskell.
For convenience, the following table summarizes the type classes that we enumerated in this chapter:
| Name | Method | Law(s) | Example(s) |
| Semigroup |
def combine( x: A, y: A) : A |
Associativity |
Option(1) |+| None |+| Option(2) // res5: Option[Int] = Some(3) |
| Monoid | def empty: A | Identity |
Vector(1,2,3).combineAll // res8: Int = 6 Vector("1", "2", "3").foldMap(s => (s,s.toInt))
// res10: (String, Int) = (123,6) |
| Functor | def map[A, B] (fa: F[A]) (f: A => B): F[B] | Identity, Composability |
def square(x: Double): Double = x * xdef squareVector: Vector[Double] => Vector[Double] = Functor[Vector].lift(square) ... |
Read now
Unlock full access