specs2 - lamdbajam 2014

35
Epic Success V Failure ? Refactoring to real FP Eric Torreborre

Upload: specs2

Post on 25-Jul-2015

385 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Specs2 - Lamdbajam 2014

Epic Success V Failure ? Refactoring to real FP

Eric Torreborre

Page 2: Specs2 - Lamdbajam 2014

Why, oh why?

Page 3: Specs2 - Lamdbajam 2014

What now?

Page 4: Specs2 - Lamdbajam 2014

How hard can it be?

HelloWorldSpec.scala

Page 5: Specs2 - Lamdbajam 2014

How hard can it be?

TextText

ExampleExample

TextText

TextText

ExampleExample

StepStep

ExampleExample

Filterin

gFilterin

gby

nameby

nameby

previousby

previous

Executio

nExecutio

nconcurre

ntconcurre

ntcondition

alcondition

al

ReportingReporting

consoleconsole

htmlhtml

Page 6: Specs2 - Lamdbajam 2014

How hard can it be?

Configuration(s)Configuration(s)ResourcesResources

FileSystemFileSystem

“Flow”“Flow”

StateState

CompositionComposition

ErrorsErrors

Page 7: Specs2 - Lamdbajam 2014

Problem?

Page 8: Specs2 - Lamdbajam 2014

Strategy

Page 9: Specs2 - Lamdbajam 2014

Strategy

Page 10: Specs2 - Lamdbajam 2014

Filtering by name

Page 11: Specs2 - Lamdbajam 2014

Filtering by previous

TextText ExampleExample ExampleExample TextText

StatsStats

resultresult

resultresultresultresult

resultresultresultresult

Page 12: Specs2 - Lamdbajam 2014

Monad stacks

Task[A]Task[A]

AA

ErrorError

AA

ErrorError

AA

FutureFuture

Page 13: Specs2 - Lamdbajam 2014

Monad stacks values

AAErrorError

FutureFuture

now(a: A)

delay(a: =>A)AA

ErrorError

FutureFuture

AAErrorError

FutureFuture

fail(t: Throwable)

and

flatM

ap!

and

flatM

ap!

Page 14: Specs2 - Lamdbajam 2014

Action in action

Action[A]Action[A]

AA

ErrorError

AA

ErrorError

AA

IOIO

AA

ErrorError

IOIO

ReaderReader

Conf => IO[A] => IO[Error]

Error = String | Throwable | (String, Throwable)

Page 15: Specs2 - Lamdbajam 2014

Action in action

Page 16: Specs2 - Lamdbajam 2014

Action opportunity

Read

everything

!

Read

everything

!

Page 17: Specs2 - Lamdbajam 2014

Filtering by previous

TextText ExampleExample ExampleExample TextText

StatsStats

resultresult

resultresultresultresult

resultresultresultresult

Page 18: Specs2 - Lamdbajam 2014

Filtering by previous

TextText ExampleExample ExampleExample TextText

StatsStats

resultresult

Action[Fragment] => Task[Fragment]

Fragment => Action[Result]

Action[Result] => Action[Fragment]

mapmap

Task[Fragment] => Process[Task, Fragment]

evaleval

Page 19: Specs2 - Lamdbajam 2014

Filtering by previous

flatM

ap!

flatM

ap!

Page 20: Specs2 - Lamdbajam 2014

Filtering by previous

Page 21: Specs2 - Lamdbajam 2014

On stacks

No big

dealNo big

deal

SpecialiseSpecialise

LibraryLibrary

Composition?Composition?

Integration?Integration?

Page 22: Specs2 - Lamdbajam 2014

Scalaz-stream

Page 23: Specs2 - Lamdbajam 2014

Concurrency with

Easy!Easy!

Page 24: Specs2 - Lamdbajam 2014

Concurrency with

With

state!With

state!

TextText ExampleExample ExampleExample StepStep ExampleExample

stop herestop here

Page 25: Specs2 - Lamdbajam 2014

Flatmap with

Online

testing!Online

testing!

Page 26: Specs2 - Lamdbajam 2014

Flatmap with

Online

testing!Online

testing!

Text1Text1 Example1Example1 Example2Example2 Example3Example3

Text1Text1 Example1Example1 Example2Example2 Example3Example3

Example2.1Example2.1 Example2.2Example2.2

Page 27: Specs2 - Lamdbajam 2014

Reporting with

“duplicate”stream

“duplicate”stream

Write to consoleWrite to console

Accumulatestate

Accumulatestate

Page 28: Specs2 - Lamdbajam 2014

Reporting with

Text1Text1 Example1Example1 Example2Example2 Example3Example3

“t1”“t1” “e1”“e1” “e2”“e2” “e3”“e3”

> Console

statsstats statsstats statsstats

Page 29: Specs2 - Lamdbajam 2014

Reporting with

Not composable

Not composable

Page 30: Specs2 - Lamdbajam 2014

Reporting with and Folds!

Page 31: Specs2 - Lamdbajam 2014

Reporting with and Folds!

Page 32: Specs2 - Lamdbajam 2014

Reporting with and Folds!

Opaque

dependent

type

Opaque

dependent

type

Page 33: Specs2 - Lamdbajam 2014

Reporting with and Folds!

Fold Monoid!Fold Monoid!

Page 34: Specs2 - Lamdbajam 2014

Reporting with and Folds!

Page 35: Specs2 - Lamdbajam 2014

Conclusion