21
The Cats toolbox: a quick tour of some basic typeclasses Pawel Szulc @rabbitonweb

The cats toolbox a quick tour of some basic typeclasses

Embed Size (px)

Citation preview

Page 1: The cats toolbox  a quick tour of some basic typeclasses

The Cats toolbox: a quick tour of some basic typeclasses

Pawel Szulc

@rabbitonweb

Page 2: The cats toolbox  a quick tour of some basic typeclasses

What’s a type class?

Page 3: The cats toolbox  a quick tour of some basic typeclasses

What’s a type class?trait Semigroup[A] { def combine(x: A, y: A): A}

Page 4: The cats toolbox  a quick tour of some basic typeclasses

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)

Page 5: The cats toolbox  a quick tour of some basic typeclasses

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)

Page 6: The cats toolbox  a quick tour of some basic typeclasses

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

Page 7: The cats toolbox  a quick tour of some basic typeclasses

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

Page 8: The cats toolbox  a quick tour of some basic typeclasses

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

Page 9: The cats toolbox  a quick tour of some basic typeclasses
Page 10: The cats toolbox  a quick tour of some basic typeclasses

So academic

Page 11: The cats toolbox  a quick tour of some basic typeclasses

So academic

So pointlessly detached from real-world problems

Page 12: The cats toolbox  a quick tour of some basic typeclasses

So academic

So pointlessly detached from real-world problemsDo they have

real-world applications

Page 13: The cats toolbox  a quick tour of some basic typeclasses
Page 14: The cats toolbox  a quick tour of some basic typeclasses

Looking for Jobin Londonwith Cats

Page 15: The cats toolbox  a quick tour of some basic typeclasses

Apples & Oranges

Page 16: The cats toolbox  a quick tour of some basic typeclasses

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

Page 17: The cats toolbox  a quick tour of some basic typeclasses

Lets implement it!https://github.com/rabbitonweb/cats_toolbox

Page 18: The cats toolbox  a quick tour of some basic typeclasses

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

Page 19: The cats toolbox  a quick tour of some basic typeclasses

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

Page 20: The cats toolbox  a quick tour of some basic typeclasses
Page 21: The cats toolbox  a quick tour of some basic typeclasses

Thank you :)@rabbitonweb

[email protected]