Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional...

121
Types Working For You Richard Dallaway, @d6y underscore.io

Transcript of Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional...

Page 1: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Types Working For You

Richard Dallaway, @d6y

underscore.io

Page 2: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Modern type system with lots of power

Page 3: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only
Page 4: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only
Page 5: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Two Themes

Straightforward Scala

Types Working for Us

Page 6: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Progression

Part 1 Straightforward Scala

Part 2 Functional Programming

Part 3 Typelevel Programming

Page 7: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Straightforward Scala

— Part 1 —

Page 8: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The only problem was we had no idea what the code was doing at first.

We came across a strange symbol we hadn’t seen in our projects before

The spaceship operator <|*|>

Someone said out loud “what the hell is that?”

http://jimplush.com/talk/

Page 9: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The only problem was we had no idea what the code was doing at first.

We came across a strange symbol we hadn’t seen in our projects before

The spaceship operator <|*|>

Someone said out loud “what the hell is that?”

http://jimplush.com/talk/

Page 10: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The only problem was we had no idea what the code was doing at first.

We came across a strange symbol we hadn’t seen in our projects before

The spaceship operator <|*|>

Someone said out loud “what the hell is that?”

http://jimplush.com/talk/

Page 11: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The only problem was we had no idea what the code was doing at first.

We came across a strange symbol we hadn’t seen in our projects before

The spaceship operator <|*|>

Someone said out loud “what the hell is that?”

http://jimplush.com/talk/

Page 12: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

“It’s about having a maintainable code base where you can have people cross projects easily and get new hires up to speed rapidly”

Page 13: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Power!

Protect the team from it

and

Get the benefit of it

Page 14: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

What can we do?

Page 15: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

1. Expressions, types, & values

2. Objects and classes

3. Algebraic data types

4. Structural recursion

5. Sequencing computation

6. Type classes

Page 16: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

1. Expressions, types, & values

2. Objects and classes

3. Algebraic data types

4. Structural recursion

5. Sequencing computation

6. Type classes

Page 17: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Algebraic data types

Structural recursion

Page 18: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Algebraic data types data into code

Structural recursion

transformation

Page 19: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Model data with logical ors and logical ands

Page 20: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A website visitor is: • anonymous; or • logged in

Page 21: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A logged in user has: • an ID; and • facts we know about

them

Page 22: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Two Patterns

and (product types) or (sum types)

Sum and product together make algebraic data types

Page 23: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Structure of the code follows the structure of

the data

Page 24: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A website visitor is: • anonymous; or • logged in

Page 25: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Visitor

case class Anonymous() extends Visitor

case class User() extends Visitor

Page 26: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A logged in user has: • an ID; and • facts we know about

them

An anonymous has: • an ID

Page 27: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Visitor

case class Anonymous() extends Visitor

case class User() extends Visitor

Page 28: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Visitor

case class Anonymous(id: Id) extends Visitor

case class User(id: Id, facts: Set[Fact]) extends Visitor

Page 29: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Structural recursion

Page 30: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def serveAd(v: Visitor): Advert = ???

Page 31: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Structure of the code follows the structure of

the data

Page 32: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def serveAd(v: Visitor): Advert = ???

Page 33: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def serveAd(v: Visitor): Advert = v match { case User(_, info) => relevantAd(info) case Anonymous(id) => adRotation(id) }

Page 34: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def serveAd(v: Visitor): Advert = v match { case User(_, info) => relevantAd(info) case Anonymous(id) => adRotation(id) }

Page 35: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def serveAd(v: Visitor): Advert = v match { case User(_, info) => relevantAd(info) case Anonymous(id) => adRotation(id) }

Structure

Page 36: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

ADT & Structural Recursion

Straightforward part of Scala.

Clear, productive, occurs frequently.

Be opinionated in what you use.

Structure helps us.

Page 37: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Help from FP Ideas

— Part 2 —

Page 38: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Combining lists

Concatenating strings

Union of sets

Combining things in a loop

Chaining logical operations

Adding numbers

Building up a JavaScript expression

Showing errors in a UI

...

Page 39: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A combine function and

an empty value

Page 40: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Addition

Empty Combine

0 +

Page 41: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Set

Empty Combine

Set.empty union

Page 42: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

For any T

Empty Combine

A zero for T

A way to combine two Ts

and give me back a T

Page 43: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A combine function and

an empty value

Page 44: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Monoid

A combine function and

an empty value

…and laws

Page 45: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only
Page 46: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The boss asks…

What’s the total visits to the web site?

def report(vs: List[Int]): Int = ???

Page 47: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

For any T

Empty Combine

A zero for T

A way to combine two Ts

and give me back a T

Page 48: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

For any T

trait Monoid[T] {

def empty: T

def combine(x: T, y: T): T

}

Page 49: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

val addition = new Monoid[Int] {

def empty = 0

def combine(x: Int, y: Int) = x+y

}

Page 50: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold

Page 51: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold(vs: List[Int]): Int =

vs match {

case Nil => 0

case v :: rest => v + fold(rest)

}

fold(List(1,2,3)) // 6

Page 52: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold(1,2,3)

1 + fold(2,3)

2 + fold(3)

3 + fold()

0

0 + 3 + 2 + 1 = 6

Page 53: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold(1,2,3)

1 + fold(2,3)

2 + fold(3)

3 + fold()

0

0 + 3 + 2 + 1 = 6

Page 54: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold(1,2,3)

1 + fold(2,3)

2 + fold(3)

3 + fold()

0

0 + 3 + 2 + 1 = 6

Page 55: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold(1,2,3)

1 + fold(2,3)

2 + fold(3)

3 + fold()

0

0 + 3 + 2 + 1 = 6

Page 56: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold(1,2,3)

1 + fold(2,3)

2 + fold(3)

3 + fold()

0

0 + 3 + 2 + 1 = 6

Page 57: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

fold(1,2,3)

1 + fold(2,3)

2 + fold(3)

3 + fold()

0

0 + 3 + 2 + 1 = 6

Page 58: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold(vs: List[Int]): Int =

vs match {

case Nil => 0

case v :: rest => v + fold(rest)

}

fold(List(1,2,3)) // 6

Page 59: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold(vs: List[Int], m: Monoid[Int]): Int =

vs match {

case Nil => 0

case v :: rest => v + fold(rest)

}

fold(List(1,2,3), addition) // 6

Page 60: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold(vs: List[Int], m: Monoid[Int]): Int =

vs match {

case Nil => m.empty

case v :: rest => m.combine(v, fold(rest,m))

}

fold(List(1,2,3), addition) // 6

Page 61: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold[T](vs: List[T], m: Monoid[T]): T =

vs match {

case Nil => m.empty

case v :: rest => m.combine(v, fold(rest,m))

}

fold(List(1,2,3), addition) // 6

Page 62: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Split on cases,

inspect values you have

Page 63: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold[T](vs: List[T], m: Monoid[T]): T =

vs match {

case Nil => ???

case v :: rest => ???

}

fold(List(1,2,3), addition) // 6

Page 64: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def fold[T](vs: List[T], m: Monoid[T]): T =

vs match {

case Nil => m.empty

case v :: rest => ???

}

fold(List(1,2,3), addition) // 6

Page 65: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

But back to Monoids…

Page 66: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The boss asks…

What’s the total visits to the web site?

def report(vs: List[Int]): Int =

fold(vs, addition)

Page 67: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Benefits

Composition

Flexibility

Problem Solving

Page 68: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The boss asks…

How many distinct visitors?

def report(vs: List[Visitor]): Int = ???

Page 69: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Set

Empty Combine

Set.empty union

Page 70: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The boss says…

Argh! The servers are OutOfMemory

Page 71: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

HyperLogLog

Empty Combine

new HLL() HLL.plus

Armon Dadgar (Papers We Love, 2015) “Bloom Filters and HyperLogLog”

Page 72: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

The boss asks…

Who are the really keen visitors to the site?

Page 73: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Count-Min Sketch

Empty Combine

new CMS() CMS.plus

Laura Bledaite (Scala eXchange 2015) “Count-Min Sketch in Real Data Applications”

Page 74: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

We can safely run a parallel version

of fold

Page 75: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Laws

Page 76: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

a + 0 = a

(a + b) + c = a + (b + c)

Page 77: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Identity & Associativity

a combine empty = a

(a combine b) combine c = a combine (b combine c)

Page 78: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

a combine b

combine combine

c d e f

Page 79: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only
Page 80: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Errors: 10 Warnings: 0

Page 81: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Its a monoid

I know this

Page 82: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

…so we fold

Page 83: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Summary

Types and laws give us flexibility & help lead us to solutions.

They help us every day.

Page 84: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

A Taste of Typelevel

— Part 3 —

Page 85: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Date Metric

Mon Low

Tue High

csv( List(“Date”, “Metric”), List( List(“Mon”, “Low”), List(“Tue”, “High”) ) )

Page 86: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Date

Mon Low

Tue High

csv( List(“Date”), List( List(“Mon”, “Low”), List(“Tue”, “High”) ) )

Page 87: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

How can we prevent that error happening again?

Page 88: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def csv( hdrs: List[String], rows: List[List[String]] ): String = ???

Page 89: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def csv[N <: Nat]( hdrs: List[String], rows: List[List[String]] ): String = ???

import shapeless._ import syntax.sized._

Page 90: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

def csv[N <: Nat]( hdrs: Sized[List[String], N], rows: List[Sized[List[String], N]] ): String = ???

import shapeless._ import syntax.sized._

Page 91: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

csv( Sized(“Date”), List(

Sized(“Mon”, “Low”), Sized(“Tue”, “High”) )

)

Page 92: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

csv( Sized(“Date”), List(

Sized(“Mon”, “Low”), Sized(“Tue”, “High”) )

)

Sized[List, 1]

Sized[List, 2]

Page 93: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

How?

Sized(“Date”) constructs Sized[Nat]

Nat implements numbers as types

Page 94: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Nat trait Succ[P <: Nat] extends Nat trait Zero extends Nat

Page 95: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Zero 0 Succ[Zero] 1 Succ[Succ[Zero]] 2 Succ[Succ[Succ[Zero]]] 3

Page 96: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Nat trait Succ[P <: Nat] extends Nat trait Zero extends Nat

Page 97: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Nat trait Succ[P <: Nat] extends Nat trait Zero extends Nat type One = Succ[Zero] type Two = Succ[One]

implicitly[Succ[Zero] =:= One] implicitly[Succ[One] =:= Succ[Succ[Zero]]]

Page 98: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Nat trait Succ[P <: Nat] extends Nat trait Zero extends Nat type One = Succ[Zero] type Two = Succ[One]

implicitly[Succ[Zero] =:= One] implicitly[Succ[One] =:= Succ[Succ[Zero]]]

Page 99: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Nat trait Succ[P <: Nat] extends Nat trait Zero extends Nat type One = Succ[Zero] type Two = Succ[One]

implicitly[Succ[Zero] =:= One] implicitly[Succ[One] =:= Succ[Succ[Zero]]]

Page 100: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

sealed trait Nat trait Succ[P <: Nat] extends Nat trait Zero extends Nat type One = Succ[Zero] type Two = Succ[One]

implicitly[Succ[Zero] =:= Two] error: Cannot prove that Succ[Zero] =:= Two.

Page 101: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Merging Fields

Page 102: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

case class User( id : Long, name : String, email : Option[String])

val user = User( 123L, “Bruce Wayne”, Some(“[email protected]”))

Page 103: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

PATCH /user/123

{ “name” : “Batman”}

Page 104: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

case class User( id : Long, name : String, email : Option[String])

case class Update( name : Option[String], email : Option[Option[String]])

Page 105: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

val user = User( 123L, “Bruce Wayne”, Some(“[email protected]”))

val update = Update( Some(“Batman”), None)

How do we get to…

User( 123L, “Batman”, Some(“[email protected]”))

Page 106: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Bulletin

https://github.com/davegurnell/bulletin

Page 107: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

How?

User String Option[String] …

Option[String]Option[

Option[String]]

…Update

Page 108: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

How?

User String Option[String] …

Option[String]Option[

Option[String]]

…Update

Head

Page 109: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

How?

User String Option[String] …

Option[String]Option[

Option[String]]

…Update

Head The Rest…

Page 110: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

How?

Type constraints

Implicit methods

HLists

Labelled generic

Macros

Page 111: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

val user = User( 123L, "Bruce Wayne”, Some(“[email protected]”))

val update = Update( Some(“Batman”), None)

import bulletin._

val updated = user.merge(update)

// User(// 123L, // “Batman”, // Some(“[email protected]”))

Page 112: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

val user = User( 123L, "Bruce Wayne”, Some(“[email protected]”))

val update = Update( Some(“Batman”), None)

import bulletin._

val updated = user.merge(update)

// User(// 123L, // “Batman”, // Some(“[email protected]”))

Page 113: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Summary

The compiler can help (maybe more than you thought).

Reduce boilerplate code.

Page 114: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Using Power Tools

Can go one of two ways…

Page 115: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Using Power Tools

Can go one of two ways…

What the hell is that?

It’s a monoid!I know this

Page 116: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Simple

Types

Power

Share

Page 117: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

2008

‘The name Scala stands for “scalable language.”

The language is so named because it was designed

to grow with the demands of its users.’

Page 118: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

What have we seen?

Some straightforward parts of Scala—Clear, maintainable, helpful

Encoding ideas in types —flexibility, leads us to solutions

Let the compiler do it

—when it make sense for your demands

Page 119: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Summary

Scala scaling with your needs —be opinionated in what you use, more when needed

Types working for us, not stopping us —functional programming, share what you learn

Page 120: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Thanks!Richard Dallaway, @d6y

underscore.io

Page 121: Types Working For You - qconlondon.com€¦ · Part 1 Straightforward Scala Part 2 Functional Programming Part 3 Typelevel Programming. Straightforward Scala — Part 1 — The only

Thanks!Richard Dallaway, @d6y

underscore.io

Amanda Laucher Wesley Reisz Noel Welsh

Dave Gurnell Miles Sabin

Jono Ferguson Julio Capote

Alessandro Zoffoli