The document provides an overview of type classes in the Cats library for Scala. It defines a Semigroup type class for combining values together, and shows how to implement it for different types. It then gives an example of using Semigroup to build a checkout system for a shop, and updating it to support buy-one-get-one-free and bulk discount offers on products. The document aims to demonstrate how type classes in Cats can be used to model real-world problems like pricing and discounts in a checkout system.
3. What’s a type class?
trait Semigroup[A] {
def combine(x: A, y: A): A
}
4. What’s a type class?
trait Semigroup[A] {
def combine(x: A, y: A): A
}
def merge[A](a1: A, a2: A)(implicit ev: Semigroup[A]) =
ev.combine(a1, a2)
5. What’s a type class?
trait Semigroup[A] {
def combine(x: A, y: A): A
}
final class SemigroupOps[A](a: A)(implicit ev: Semigroup[A]) {
def |+|(other: A): A = ev.combine(a, other)
def combine(rhs: A): A = ev.combine(a, other)
}
def merge[A](a1: A, a2: A)(implicit ev: Semigroup[A]) =
ev.combine(a1, a2)
6. What’s a type class?
trait Semigroup[A] {
def combine(x: A, y: A): A
}
final class SemigroupOps[A](a: A)(implicit ev: Semigroup[A]) {
def |+|(other: A): A = ev.combine(a, other)
def combine(rhs: A): A = ev.combine(a, other)
}
def merge[A](a1: A, a2: A)(implicit ev: Semigroup[A]) =
a1 combine a2
7. What’s a type class?
trait Semigroup[A] {
def combine(x: A, y: A): A
}
final class SemigroupOps[A](a: A)(implicit ev: Semigroup[A]) {
def |+|(other: A): A = ev.combine(a, other)
def combine(rhs: A): A = ev.combine(a, other)
}
def merge[A](a1: A, a2: A)(implicit ev: Semigroup[A]) =
a1 |+| a2
8. What’s a type class?
trait Semigroup[A] {
def combine(x: A, y: A): A
}
final class SemigroupOps[A](a: A)(implicit ev: Semigroup[A]) {
def |+|(other: A): A = ev.combine(a, other)
def combine(rhs: A): A = ev.combine(a, other)
}
def merge[A : Semigroup](a1: A, a2: A) =
a1 |+| a2
16. Apples & Oranges
Step 1
➔ You are building a checkout system for a shop
which only sells apples and oranges.
➔ Apples cost 60p and oranges cost 25p.
➔ Build a checkout system which takes a list of
items scanned at the till and outputs the total
cost
➔ For example: [ Apple, Apple, Orange, Apple]
=> £2.05
18. Apples & Oranges
Step 1
➔ You are building a checkout system for a shop
which only sells apples and oranges.
➔ Apples cost 60p and oranges cost 25p.
➔ Build a checkout system which takes a list of
items scanned at the till and outputs the total
cost
➔ For example: [ Apple, Apple, Orange, Apple]
=> £2.05
19. Apples & Oranges
Step 1
➔ You are building a checkout system for a shop
which only sells apples and oranges.
➔ Apples cost 60p and oranges cost 25p.
➔ Build a checkout system which takes a list of
items scanned at the till and outputs the total
cost
➔ For example: [ Apple, Apple, Orange, Apple]
=> £2.05
Step 2
➔ The shop decides to introduce two new
offers
◆ buy one, get one free on Apples
◆ 3 for the price of 2 on Oranges
➔ Update your checkout functions accordingly