42
Go functional!

Specs2 - Scala-Syd presentation

Embed Size (px)

DESCRIPTION

This presentation was given to the Scala Sydney user group on Sep. 14th, 2011.

Citation preview

Page 1: Specs2 - Scala-Syd presentation

Go functional!

Page 2: Specs2 - Scala-Syd presentation
Page 3: Specs2 - Scala-Syd presentation

tourimport IncredibleStringReverser._

class ReverserSpec extends Specification { "a reversed empty string must be empty" in { reverse("") must_== "" } "a reversed empty string must really *be empty*" in { reverse("") must be empty } "a reversed string must be reversed abc -> cba" in { reverse("abc") must be_==("cba") } "a longer string must also be reversed. Whoops!" in { reverse("abcdef") must be_==("xxxxx") }}

Page 4: Specs2 - Scala-Syd presentation

tour

"a reversed empty string must be empty" in { reverse("") must be empty}

Page 5: Specs2 - Scala-Syd presentation

tour

Page 6: Specs2 - Scala-Syd presentation

tour

Page 7: Specs2 - Scala-Syd presentation

Shame

Page 8: Specs2 - Scala-Syd presentation

Mutability

Page 9: Specs2 - Scala-Syd presentation

Concurrency

Page 10: Specs2 - Scala-Syd presentation

Acceptance

Page 11: Specs2 - Scala-Syd presentation

Why

The idea

User

DesignProgram

Page 12: Specs2 - Scala-Syd presentation

Go Functional!

But what

is it?!

Page 13: Specs2 - Scala-Syd presentation

Referential transparency

Immutabili

ty

Page 14: Specs2 - Scala-Syd presentation

behavior

pipelined

FPemerg

entbehavior

Processing data structures

OO

Page 15: Specs2 - Scala-Syd presentation

Why

The idea

User

DesignProgram

Page 16: Specs2 - Scala-Syd presentation

Starting point

instructions != expressions

Page 17: Specs2 - Scala-Syd presentation

Nesting

Precedence:+!^

Page 18: Specs2 - Scala-Syd presentation

FormattingText

Example

Text

Example

Example

Page 19: Specs2 - Scala-Syd presentation

Formatting

pbrendt, t(i)bt, bt(i)

acceptance

Page 20: Specs2 - Scala-Syd presentation

Context

Page 21: Specs2 - Scala-Syd presentation

doBefore

withDb.apply(e2)

Page 22: Specs2 - Scala-Syd presentation

Why

The idea

User

DesignProgram

Page 23: Specs2 - Scala-Syd presentation

Run

execute

levels

select

sequence

arguments

statistics

to text

to html

Page 24: Specs2 - Scala-Syd presentation

Laziness vs evaluation

Page 25: Specs2 - Scala-Syd presentation

Iterating

Page 26: Specs2 - Scala-Syd presentation

Traverse

A datastructure to

traversethe

accumulation

the mapping

Page 27: Specs2 - Scala-Syd presentation

Applicative magic?

Lots of instances:• State• List• Monad• Monoid

simple

composable Type inference is hard,Let’s go

shopping!

Page 28: Specs2 - Scala-Syd presentation

Reduceraggregation

“parallel” composition

mapping

specs2 reducers:• Levels• Statistics• Arguments• Text• Html

Page 29: Specs2 - Scala-Syd presentation

Why

The idea

User

DesignProgram

Page 30: Specs2 - Scala-Syd presentation

Trees

Text

Example 1

Example 2

SuiteDescript

ion

Test Descriptio

n 1

Test Descriptio

n 2

SuiteDescription

fChildren = TD1, TD2

Page 31: Specs2 - Scala-Syd presentation

TreeLoc

Page 32: Specs2 - Scala-Syd presentation

TreeLoc

<h1/>

<h2/> <h2/>

<body>

<h2>part 1</h2><h2>part 2</h2>

<h1>Title 1</h1>

</body>

Anti-XMLThe non-violent solution

Page 33: Specs2 - Scala-Syd presentation

Concurrency

a concurrent task

=> Promise[Seq[ExecutedFragment]]

2.9.0

inside!

Page 34: Specs2 - Scala-Syd presentation

Exceptions

Page 35: Specs2 - Scala-Syd presentation

Exceptions

Page 36: Specs2 - Scala-Syd presentation

Exceptions

Page 37: Specs2 - Scala-Syd presentation

Mocks

<h1/>

Page 38: Specs2 - Scala-Syd presentation

MutabilityBack fo

r

good

Nothing here!

Throws exception

Page 39: Specs2 - Scala-Syd presentation

ConfigurationPart of the flow

named parameters + defaults

nested ‘record’

Page 40: Specs2 - Scala-Syd presentation

ConfigurationImplicits

implicit arguments

default value

Page 41: Specs2 - Scala-Syd presentation

Watch out!

recursivity

SOE

val (a,b) = c

Match

Error

val in traits

NPE

lost expressions

Bug

Page 42: Specs2 - Scala-Syd presentation

Conclusion

• Reliability• Concurrency• Composition• Less mocks• val/vars

• DSL• Type inference• Learning curve• Gotchas