slick 3.0 functional programming and db side effects

47
Slick 3.0: Functional programming with database side effects (On short notice…) Joost de Vries twitter: @jouke email: [email protected] codestar.nl github: joost-de-vries

Upload: joost-de-vries

Post on 14-Apr-2017

124 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Slick 3.0   functional programming and db side effects

Slick 3.0: Functional programming with

database side effects(On short notice…)

Joost de Vries twitter: @jouke

email: [email protected] codestar.nl

github: joost-de-vries

Page 2: Slick 3.0   functional programming and db side effects

๏ Database actions in Slick 3.0

• Actions in FP: side effects

• A lightweight database action implementation

• Database actions in Slick 3.0 revisited

Page 3: Slick 3.0   functional programming and db side effects

What is Slick

• Slick is originally a Scala framework that offers flatmappable relational queries

• Comparable to Microsofts Linq to Sql

• Blocking, thread based

Page 4: Slick 3.0   functional programming and db side effects

var xs = array.Where(x => x >= 1); array filter { _ >= 1 }

var xs = array.Select(x => x + 1); array map { _ + 1 }

var yx = array.SelectMany(x => new [] { x, 3 });

array flatMap { Array(_, 3) }

var results = from x in array where x >= 1 select x;

for (x <- array if x >= 1) yield x

LINQ to Sql: SQL as map, flatmap & filter

Slick offers the same paradigm in Scala JOOQ is an alternative to Slick for the JVM

This talk is not about SQL querying using map and flatmap

Page 5: Slick 3.0   functional programming and db side effects

What’s new in Slick 3.0

• Non blocking

• (Or is it?)

• Source for reactive streams

Page 6: Slick 3.0   functional programming and db side effects

DBIO[A] (non streaming)

From: Hello Slick 3.0 example project

Page 7: Slick 3.0   functional programming and db side effects

A DBIO[R] (simplified)

Page 8: Slick 3.0   functional programming and db side effects

Type hierarchy

Page 9: Slick 3.0   functional programming and db side effects

Type hierarchy

Page 10: Slick 3.0   functional programming and db side effects

Reactive streamsThe goal

Page 11: Slick 3.0   functional programming and db side effects
Page 12: Slick 3.0   functional programming and db side effects

Reactive streams feature pure functions to process a stream of events

back pressure

“asynchronous computations that allow separating cpu intensive code from IO intensive code”

So we need a way to deal with db side effects in a purely functional

way

Page 13: Slick 3.0   functional programming and db side effects

FP vs side effects

• Db manipulation is all about side effects

• So how do you deal with side effects in a purely functional programming model?

• Using flatmappable ‘actions’

Page 14: Slick 3.0   functional programming and db side effects

• Database actions in Slick 3.0

๏ Actions in FP: side effects

• A lightweight database action implementation

• Database actions in Slick 3.0 revisited

Page 15: Slick 3.0   functional programming and db side effects

functional stream processing

• Remember we wanted to let db manipulation take part in the purely functional world of stream computing

• Hence we need a flatmappable way of dealing with side effects

Page 16: Slick 3.0   functional programming and db side effects

Functional stream processing

• In Scala we’re used to flatmappables for

• Null values

• Exceptions

• Latency

• Collections

• But not for

• Input / Output

• Reading from an environment

• Mutating state

I.e. managing side effects

Page 17: Slick 3.0   functional programming and db side effects

The power of semi colons!

Great for side effects but not streamable

Page 18: Slick 3.0   functional programming and db side effects

What’s so great about supporting flatmap?

• Put building blocks together into bigger building blocks

• Alternate outcomes are transparantly propagated through parts to the whole

• … depending on the instance

Page 19: Slick 3.0   functional programming and db side effects

Chaining outcomes in Swift

Page 20: Slick 3.0   functional programming and db side effects

• Pure functions don’t have side effects

• I.e. a function evaluation has always the same value

• (a.k.a. Substitution principle, referential transparency)

• But a program without f.i. IO is useless

Page 21: Slick 3.0   functional programming and db side effects

How does Haskell handle side effects?

• Haskell is a pure lazy language

• There’s no ; operator to evaluate sequentially

• To do IO so called actions are used: recognisable from the their type

Page 22: Slick 3.0   functional programming and db side effects

So writing hello world is in Haskell not the first thing you do

Page 23: Slick 3.0   functional programming and db side effects

Spot the side effect

def f : Unit => A

def g: A => Unit

f

A

?

g

A

?

Page 24: Slick 3.0   functional programming and db side effects

Actions• A value of type IO t is an action that, when

performed, may do some input/output before delivering a result of type t.

• Actions are also called computations

• Evaluating an action has no effect; performing an action has effect.

• Actions can be put together into bigger actionsSource: Simon Peyton Jones - Lazy functional

programming for real

Page 25: Slick 3.0   functional programming and db side effects

Function declaration: type

Function implementation

Page 26: Slick 3.0   functional programming and db side effects

Haskell to Scala translation

Page 27: Slick 3.0   functional programming and db side effects

Actions…

Our building blocks

Page 28: Slick 3.0   functional programming and db side effects

Chaining actions into a programme

The type of >>= : () means: infix operator (ignore the ‘ accent)

flatmap in Scala meaning is similar to ; in Scala

Page 29: Slick 3.0   functional programming and db side effects

Chaining actions into a programme

In the FP world this is called ‘composition’; ‘composing’ 3 flatmappables into a composite

action

type IO a = World -> (a, World)

Page 30: Slick 3.0   functional programming and db side effects

Ignore the result of a previous action

Pronounced as andThen

Page 31: Slick 3.0   functional programming and db side effects
Page 32: Slick 3.0   functional programming and db side effects

• Database actions in Slick 3.0

• Actions in FP: side effects

๏ A lightweight database action implementation

• Database actions in Slick 3.0 revisited

Page 33: Slick 3.0   functional programming and db side effects

Resource mgmt with functions

In the early days of the Spring framework it provided this as

JdbcTemplate

Page 34: Slick 3.0   functional programming and db side effects

From: 2012 talk by Paul Chuisano

Page 35: Slick 3.0   functional programming and db side effects

Let’s flatmap that

Page 36: Slick 3.0   functional programming and db side effects

[coding]

Page 37: Slick 3.0   functional programming and db side effects

Putting actions together into bigger actions

Page 38: Slick 3.0   functional programming and db side effects
Page 39: Slick 3.0   functional programming and db side effects

The Reader flatmappable

Anything that takes a context like object to create results is a candidate for a Reader type

of flat mappable

Page 40: Slick 3.0   functional programming and db side effects
Page 41: Slick 3.0   functional programming and db side effects

• Database actions in Slick 3.0

• Actions in FP: side effects

• A lightweight database action implementation

๏ Database actions in Slick 3.0 revisited

Page 42: Slick 3.0   functional programming and db side effects

DBIO[A] (non streaming)

Page 43: Slick 3.0   functional programming and db side effects

StreamingDBIO

Page 44: Slick 3.0   functional programming and db side effects

Type of materialised result

Read and/or Write side effect for master/slave

routing

Type of streamed elements

Page 45: Slick 3.0   functional programming and db side effects

Client code is run in client execution context DB code is run against dedicated thread pool

Separated!

Page 46: Slick 3.0   functional programming and db side effects

Resources• Stefan Zeigers talk at Scala Days San Fransisco

• slides http://slick.typesafe.com/talks/scaladays2015sf/Reactive_Slick_for_Database_Programming.pdf

• video https://www.parleys.com/tutorial/reactive-slick-database-programming

• Paul Chiusano and Rúnar Bjarnason - Functional programming in Scala

• Activator template hello-slick-3.0

Page 47: Slick 3.0   functional programming and db side effects

• Let’s take a short break and start hacking!

• The idea is to enhance a scala application that uses Slick and deploy it to Heroku to demonstrate.

• If you’ve forwarded your github handle I’ll add you to the project repo.

• The repo code is for todays use only. Please be responsible and don’t put it on the public internet somewhere.