42
Go functional!

Specs2, go functional

  • Upload
    specs2

  • View
    28

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Specs2,  go functional

Go functional!

Page 2: Specs2,  go functional
Page 3: Specs2,  go functional

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,  go functional

tour

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

Page 5: Specs2,  go functional

tour

Page 6: Specs2,  go functional

tour

Page 7: Specs2,  go functional

Shame

Page 8: Specs2,  go functional

Mutability

Page 9: Specs2,  go functional

Concurrency

Page 10: Specs2,  go functional

Acceptance

Page 11: Specs2,  go functional

Why

The idea

User

DesignProgram

Page 12: Specs2,  go functional

Go Functional!

But what

is it?!

Page 13: Specs2,  go functional

Referential transparency

Immutabili

ty

Page 14: Specs2,  go functional

behavior

pipelined

FPemerg

entbehavior

Processing data structures

OO

Page 15: Specs2,  go functional

Why

The idea

User

DesignProgram

Page 16: Specs2,  go functional

Starting point

instructions != expressions

Page 17: Specs2,  go functional

Nesting

Precedence:+!^

Page 18: Specs2,  go functional

FormattingText

Example

Text

Example

Example

Page 19: Specs2,  go functional

Formatting

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

acceptance

Page 20: Specs2,  go functional

Context

Page 21: Specs2,  go functional

doBefore

withDb.apply(e2)

Page 22: Specs2,  go functional

Why

The idea

User

DesignProgram

Page 23: Specs2,  go functional

Run

execute

levels

select

sequence

arguments

statistics

to text

to html

Page 24: Specs2,  go functional

Laziness vs evaluation

Page 25: Specs2,  go functional

Iterating

Page 26: Specs2,  go functional

Traverse

A datastructure to

traversethe

accumulation

the mapping

Page 27: Specs2,  go functional

Applicative magic?

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

simple

composable Type inference is hard,Let’s go

shopping!

Page 28: Specs2,  go functional

Reduceraggregation

“parallel” composition

mapping

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

Page 29: Specs2,  go functional

Why

The idea

User

DesignProgram

Page 30: Specs2,  go functional

Trees

Text

Example 1

Example 2

SuiteDescript

ion

Test Descriptio

n 1

Test Descriptio

n 2

SuiteDescription

fChildren = TD1, TD2

Page 31: Specs2,  go functional

TreeLoc

Page 32: Specs2,  go functional

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,  go functional

Concurrency

a concurrent task

=> Promise[Seq[ExecutedFragment]]

2.9.0

inside!

Page 34: Specs2,  go functional

Exceptions

Page 35: Specs2,  go functional

Exceptions

Page 36: Specs2,  go functional

Exceptions

Page 37: Specs2,  go functional

Mocks

<h1/>

Page 38: Specs2,  go functional

MutabilityBack fo

r

good

Nothing here!

Throws exception

Page 39: Specs2,  go functional

ConfigurationPart of the flow

named parameters + defaults

nested ‘record’

Page 40: Specs2,  go functional

ConfigurationImplicits

implicit arguments

default value

Page 41: Specs2,  go functional

Watch out!

recursivity

SOE

val (a,b) = c

Match

Error

val in traits

NPE

lost expressions

Bug

Page 42: Specs2,  go functional

Conclusion

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

• DSL• Type inference• Learning curve• Gotchas