September 2018
Intermediate to advanced
398 pages
9h 43m
English
Cats provide several derivations of the Semigroup type class. It also declares a |+| operator in SemigroupOps, which is an alias for combine.
Some examples are as follows:
import cats.implicits._ 1 |+| 2 // res0: Int = 3 "Hello " |+| "World !" // res1: String = Hello World ! (1, 2, "Hello ") |+| (2, 4, "World !") // res2: (Int, Int, String) = (3,6,Hello World !)
Thanks to the built-in derivations brought up with import cats.implicits._, we can combine Int, String, Tuple2, and Tuple3.
We can also combine some parameterized types, such as Option and Vector:
Vector(1, 2) |+| Vector(3, 4) // res3: Vector[Int] = Vector(1, 2, 3, 4) Option(1) |+| Option(2) // res4: Option[Int] = Some(3) Option(1) |+| None |+| Option(2) // res5: Option[Int] ...
Read now
Unlock full access