Upload
pawel-szulc
View
101
Download
1
Embed Size (px)
Citation preview
The Cats toolbox: a quick tour of some basic typeclasses
Pawel Szulc
@rabbitonweb
What’s a type class?
What’s a type class?trait Semigroup[A] { def combine(x: A, y: A): A}
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)
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)
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
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
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
So academic
So academic
So pointlessly detached from real-world problems
So academic
So pointlessly detached from real-world problemsDo they have
real-world applications
Looking for Jobin Londonwith Cats
Apples & Oranges
Apples & OrangesStep 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
Lets implement it!https://github.com/rabbitonweb/cats_toolbox
Apples & OrangesStep 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
Apples & OrangesStep 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
Thank you :)@rabbitonweb