際際滷

際際滷Share a Scribd company logo
Go functional!
Specs2,  go functional
tour
import 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")
}
}
tour
"a reversed empty string must be empty" in {
reverse("") must be empty
}
tour
tour
Sham
e
Mutability
Concurrency
Acceptance
Wh
y
The idea
User
Design
Program
Go Functional!
Referential
transparency

Immutability
behavior
pipelined
F
Pemergent
behavior
Processing data
structures
OO
Wh
y
The idea
User
Design
Program
Starting point
instructions != expressions
Nesting
Precedence:
+
!
^
Formatting
Text
Example
Text
Example
Example
Formatting
p
br
end
t, t(i)
bt, bt(i)
acceptance
Context
doBefore
withDb.apply(e2)
Wh
y
The idea
User
Design
Program
Run
execute
levels
select
sequence
arguments
statistics
to text
to html
Laziness vs evaluation
Iterating
Traverse
A data
structure to
traverse
the
accumulation
the
mapping
Applicative magic?
Lots of instances:
 State
 List
 Monad
 Monoid
simple
composable Type inference
is hard,
Lets go shopping!
Reducer
aggregation
parallel composition
mapping
specs2 reducers:
 Levels
 Statistics
 Arguments
 Text
 Html
Wh
y
The idea
User
Design
Program
Trees
Text
Example 1 Example 2
Suite
Description
Test
Description 1
Test
Description 2
Suite
Description
fChildren = TD1, TD2
TreeLoc
TreeLoc
Anti-XML
The non-violent solution
Concurrency
a concurrent
task
=> Promise[Seq[ExecutedFragment]]
Exceptions
Exceptions
Exceptions
Mocks
Mutability
Nothing here!
Throws
exception
Configuration
Part of the flow
named parameters + defaults
nested record
Configuration
Implicits
implicit
arguments
default
value
Watch out!
recursivity
val (a,b) = c
val in traits
lost expressions
Conclusion
 Reliability
 Concurrency
 Composition
 Less mocks
 val/vars
 DSL
 Type inference
 Learning curve
 Gotchas

More Related Content

Specs2, go functional