Slide 1

Slide 1 text

A Day in the Life of a Func onal Programmer Richard Dallaway

Slide 2

Slide 2 text

Modern Development Func onal Programming with Types

Slide 3

Slide 3 text

Why now?

Slide 4

Slide 4 text

Why now? Modern compilers have powerful type system

Slide 5

Slide 5 text

A Day in the Life

Slide 6

Slide 6 text

Func onal Programming Is For Everyone Makes Change Easier

Slide 7

Slide 7 text

The Morning Standup

Slide 8

Slide 8 text

CRM RDBMS REST Server

Slide 9

Slide 9 text

$ http GET :8080/customers HTTP/1.1 200 OK Content-Type: application/json [ { "id": 1, "name": "Alice", "phone": "+1 555 1234" }, { "id": 2, "name": "Bob", "phone": "+1 555 5678" } ]

Slide 10

Slide 10 text

As a customer, I should have a subscrip on, so I can receive the company magazine.

Slide 11

Slide 11 text

No subscrip on Ac ve subscrip on Lapsed subscrip on

Slide 12

Slide 12 text

Algebraic Data Types Structural Recusion Unfamiliar names for simple powerful ideas

Slide 13

Slide 13 text

sealed trait Subscription

Slide 14

Slide 14 text

sealed trait Subscription case object Never extends Subscription case object Active extends Subscription case object Lapsed extends Subscription

Slide 15

Slide 15 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription

Slide 16

Slide 16 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription case class Customer( name : String, phone : String, sub : Subscription )

Slide 17

Slide 17 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription case class Customer( name : String, phone : String, sub : Subscription ) def transition(sub: Subscription, at: Date): Subscription = ???

Slide 18

Slide 18 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription case class Customer( name : String, phone : String, sub : Subscription ) def transition(sub: Subscription, at: Date): Subscription = sub match { case Lapsed | Never => sub case Active(exp) => ??? }

Slide 19

Slide 19 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription case class Customer( name : String, phone : String, sub : Subscription ) def transition(sub: Subscription, at: Date): Subscription = sub match { case Lapsed | Never => sub case Active(exp) => if (at > exp) Lapsed else sub }

Slide 20

Slide 20 text

“Marke ng just reminded me we give free subscrip ons to some customers” — Product Owner

Slide 21

Slide 21 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription

Slide 22

Slide 22 text

sealed trait Subscription case object Never extends Subscription case class Active(expires: Date) extends Subscription case object Lapsed extends Subscription case object Freebie extends Subscription def transition(sub: Subscription, at: Date): Subscription = sub match { case Lapsed | Never => sub case Active(exp) => if (at > exp) Lapsed else sub } ERROR: non‐exhaus ve match

Slide 23

Slide 23 text

Move side effects out

Slide 24

Slide 24 text

def transition(c: Customer, at: Date): Action = ???

Slide 25

Slide 25 text

def transition(c: Customer, at: Date): Action = ??? sealed trait Action case object NoAction extends Action case class EncourageRenewal(c: Customer) extends Action case class Expire(c: Customer) extends Action ...

Slide 26

Slide 26 text

def transition(c: Customer, at: Date): Action = ??? sealed trait Action case object NoAction extends Action case class EncourageRenewal(c: Customer) extends Action case class Expire(c: Customer) extends Action ... def perform(a: Action): Unit = a match { case NoAction => case Expire(c) => sendSMS(c.phone) ...etc }

Slide 27

Slide 27 text

ADTs Safe Versi le Separate data & behaviour

Slide 28

Slide 28 text

Output JSON to the Client { "id": 1, "name": "Alice", "phone": "+1 555 1234", "sub": { "type": "expired" } }

Slide 29

Slide 29 text

Type Class Big things from ny building blocks

Slide 30

Slide 30 text

Turn Data into JSON text 7.toString

Slide 31

Slide 31 text

Turn Data into JSON text 7.toString 7 "\"" + "Hello" + "\"" "Hello"

Slide 32

Slide 32 text

Turn Data into JSON text 7.toString 7 "\"" + "Hello" + "\"" "Hello" { greeting: "Hello", count: 7 }

Slide 33

Slide 33 text

Turn Data into JSON text 7.toString 7 "\"" + "Hello" + "\"" "Hello" { greeting: "Hello", count: 7 } Int => String String => String

Slide 34

Slide 34 text

Turn Data into JSON text 7.toString 7 "\"" + "Hello" + "\"" "Hello" { greeting: "Hello", count: 7 } Int => String String => String T => String

Slide 35

Slide 35 text

trait JsonFormat[T] { def format(value: T): String }

Slide 36

Slide 36 text

trait JsonFormat[T] { def format(value: T): String } val intFormat = new JsonFormat[Int] { def format(value: Int): String = value.toString } val strFormat = new JsonFormat[String] { def format(value: String): String = "\"" + value + "\"" }

Slide 37

Slide 37 text

trait JsonFormat[T] { def format(value: T): String } val intFormat = new JsonFormat[Int] { def format(value: Int): String = value.toString } val strFormat = new JsonFormat[String] { def format(value: String): String = "\"" + value + "\"" } def outputJson[T](value: T)(jf: JsonFormat[T]) = jf.format(value) outputJson(7)(intFormat) // 7

Slide 38

Slide 38 text

trait JsonFormat[T] { def format(value: T): String } implicit val intFormat = new JsonFormat[Int] { def format(value: Int): String = value.toString } implicit val strFormat = new JsonFormat[String] { def format(value: String): String = "\"" + value + "\"" } def outputJson[T](value: T)(implicit jf: JsonFormat[T]) = jf.format(value) outputJson(7) // 7

Slide 39

Slide 39 text

[ "Hello", "there" ] [ 1, 2, 3, 4 ] [ [1,2], [3,4] ]

Slide 40

Slide 40 text

implicit def listFormat[T] = new JsonFormat[List[T]] { def format(values: List[T]): String = { ??? } }

Slide 41

Slide 41 text

implicit def listFormat[T] = new JsonFormat[List[T]] { def format(values: List[T]): String = { val formattedValues = { ??? } "[" + formattedValues.mkString(",") + "]" } }

Slide 42

Slide 42 text

implicit def listFormat[T] = new JsonFormat[List[T]] { def format(values: List[T]): String = { val formattedValues = for { v <- values } yield ??? "[" + formattedValues.mkString(",") + "]" } }

Slide 43

Slide 43 text

implicit def listFormat[T](implicit jf: JsonFormat[T]) = new JsonFormat[List[T]] { def format(values: List[T]): String = { val formattedValues = for { v <- values } yield jf.format(v) "[" + formattedValues.mkString(",") + "]" } }

Slide 44

Slide 44 text

implicit def listFormat[T](implicit jf: JsonFormat[T]) = new JsonFormat[List[T]] { def format(values: List[T]): String = { val formattedValues = for { v <- values } yield jf.format(v) "[" + formattedValues.mkString(",") + "]" } } def outputJson[T](value: T)(implicit jf: JsonFormat[T]) = jf.format(value) outputJson( List(1,2,3,4) ) [1,2,3,4]

Slide 45

Slide 45 text

implicit def listFormat[T](implicit jf: JsonFormat[T]) = new JsonFormat[List[T]] { def format(values: List[T]): String = { val formattedValues = for { v <- values } yield jf.format(v) "[" + formattedValues.mkString(",") + "]" } } def outputJson[T](value: T)(implicit jf: JsonFormat[T]) = jf.format(value) outputJson( List(1,2,3,4) ) [1,2,3,4] outputJson( List( List(1,2), List(3,4) ) ) [ [1,2], [3,4] ]

Slide 46

Slide 46 text

Type Class Small Building Blocks Combined by the Compiler Used Everywhere

Slide 47

Slide 47 text

case class Customer( name : String, phone : String, id : Long ) sql" select name, phone, id from customer ".query[Customer]

Slide 48

Slide 48 text

case class Customer( name : String, phone : String, id : Long ) sql" select name, phone, id from customer ".query[Customer] implicit val SubscriptionMeta: Meta[Subscription] = Meta[String].nxmap(ch => ch match { case "A" => Active case "L" => Lapsed case "N" => Never }, sub => sub match { case Active => "A" case Lapsed => "L" case Never => "N" })

Slide 49

Slide 49 text

Used Everywhere ADTs Type Classes

Slide 50

Slide 50 text

As a customer, I should have an control over my details, so I can update some or all of them.

Slide 51

Slide 51 text

class CustomerAPI(db: Database) { val service = HttpService { case GET -> Root / "customers" => Ok(db.list) case GET -> Root / "customers" / IntVar(id) => db.find(id).flatMap { case Some(customer) => Ok(customer) case None => NotFound() } } }

Slide 52

Slide 52 text

class CustomerAPI(db: Database) { val service = HttpService { case GET -> Root / "customers" => Ok(db.list) case GET -> Root / "customers" / IntVar(id) => db.find(id).flatMap { case Some(customer) => Ok(customer) case None => NotFound() } case PATCH -> Root / "customers" / IntVar(id) => decode the patch lookup the customer in the database found a record? somehow apply the patch to the record update the database otherwise, 404 the request } }

Slide 53

Slide 53 text

Simplified Patch { "name" : "Robert" } or { "name" : "Robert", "phone : "+1 555 555" }

Slide 54

Slide 54 text

Simplified Patch (Customer, Patch) => Customer

Slide 55

Slide 55 text

case class Customer( id : Long, name : String, phone : String ) case class CustomerPatch( name : Option[String], phone : Option[String] )

Slide 56

Slide 56 text

case class Customer( id : Long, name : String, phone : String ) case class CustomerPatch( name : Option[String], phone : Option[String] ) trait Merge[Into, From] { def merge(i: Into, f: From): Into } def merge[Into, From](i: Into, f: From) = ???

Slide 57

Slide 57 text

case class Customer( id : Long, name : String, phone : String ) case class CustomerPatch( name : Option[String], phone : Option[String] ) trait Merge[Into, From] { def merge(i: Into, f: From): Into } def merge[Into, From](i: Into, f: From) (implicit m: Merge[Into, From]) = m.merge(i, f)

Slide 58

Slide 58 text

case class Customer( id : Long, name : String, phone : String ) case class CustomerPatch( name : Option[String], phone : Option[String] ) trait Merge[Into, From] { def merge(i: Into, f: From): Into } def merge[Into, From](i: Into, f: From) (implicit m: Merge[Into, From]) = m.merge(i, f) implicit def customerMerge: Merge[Customer, CustomerPatch] = ???

Slide 59

Slide 59 text

case class Customer( id : Long, name : String, phone : String ) case class CustomerPatch( name : Option[String], phone : Option[String] ) trait Merge[Into, From] { def merge(i: Into, f: From): Into } def merge[Into, From](i: Into, f: From) (implicit m: Merge[Into, From]) = m.merge(i, f) implicit def customerMerge: Merge[Customer, CustomerPatch] = ??? implicit def optMerge = new Merge[String, Option[String]] { def merge(initial: String, update: Option[String]) = update match { case Some(value) => value case None => initial } }

Slide 60

Slide 60 text

case class Customer( id : Long, name : String, phone : String ) case class CustomerPatch( name : Option[String], phone : Option[String] ) trait Merge[Into, From] { def merge(i: Into, f: From): Into } def merge[Into, From](i: Into, f: From) (implicit m: Merge[Into, From]) = m.merge(i, f) implicit def customerMerge: Merge[Customer, CustomerPatch] = ??? merge( Customer(1, "Bob", "555-123"), CustomerPatch(Some("Robert"), None) ) Customer(1, "Robert", "555-123")

Slide 61

Slide 61 text

Pu ng it all together... case req @ PATCH -> Root / "customers" / IntVar(id) => req.decode[CustomerPatch] { patch => db.find(id).flatMap { case None => NotFound() case Some(customer) => val updated = merge(customer, patch) Ok(db.update(updated)) } } val merge = bulletin.AutoMerge[Customer, CustomerPatch] case class CustomerPatch( name : Option[String], phone : Option[String] )

Slide 62

Slide 62 text

ADTs Safe, Versi le Type Classes Combining Small Building Blocks

Slide 63

Slide 63 text

Scala Modern Powerful Compiler Func onal Programming JVM, Scala Na ve, Scala.js

Slide 64

Slide 64 text

Help Make Change Easier

Slide 65

Slide 65 text

Thank You h ps:/ /underscore.io/