Functional languages like Scala can reduce the complexity of writing high-concurrency, high-throughput systems, but growing software with TDD in Scala presents challenges unfamiliar to those of us who spend most of our time in the JavaScript, Java, and .NET worlds.
In this session at Agile2014, Tim Myer explained how to avoid the pitfalls of testing a functional language and offered some new techniques that you can apply to development in other languages, even if you have never written software using Scala before.
2. SolutionsIQ
Λ Join a great team of developers, consultants
and Agile experts at a company that is Agile
through and through.
Λ We are hiring XP developers and coaches.
Λ Swing by our booth or talk to me afterwards
for more information.
λ the eχtreme: test-driving a functional language 2
3. “
”
λ the eχtreme: test-driving a functional language 3
Scala: you stare at your foot for 3
days without any sleep, you then
figrue [sic] out how to shoot
yourself in the foot with one line of
code… recursively.
Comment from Roberto Leibman on
How to Shoot Yourself in the Foot in Any Programming Language
by Mike Walker
4. λ the eχtreme: test-driving a functional language 4
Agenda
Λ Tools of the Trade
Λ First test
Λ A synchronicity
Λ Network independence
Λ More of the same
Λ Changing Station
Λ And so on and so forth and so on….
5. λ the eχtreme: test-driving a functional language 5
Prelude
7. “
”
λ the eχtreme: test-driving a functional language 7
if you look at the features Scala provides, it is substantially a
functional language, but on the surface it does not always look
like one, and it does not force you to adopt the functional style.
For many of its users, the functional programming constructs in
Scala are the most important set of tools it has to offer but they
are not the only tools. In fact, great care has been spent in the
Scala design to make functional constructs, imperative
constructs, and objects all play well together. I think
postfunctional is a good term for that blend.
- Martin Odersky
A Postfunctional Language
8. λ the eχtreme: test-driving a functional language 8
Tools
Λ Git
Λ Maven
Λ Scala IDE
Λ A Dropbox account
9. TheBeginning
9
Where do we start?
We can start at
TheBeginning.
λ the eχtreme: test-driving a functional language
How can we do
that?
Check it out!
10. λ the eχtreme: test-driving a functional language 10
$ git clone
https://github.com/timezra/LambdaTheEx
treme.git && cd LambdaTheExtreme
$ git checkout tags/TheBeginning
11. TheBeginning
11
We got git, now do
we get an
elephant?
We get a canary
instead.
λ the eχtreme: test-driving a functional language
12. λ the eχtreme: test-driving a functional language 12
Canary
13. Canary
13
What does a canary
have to do with
this?
It’s an early
indication that
something has
gone very wrong.
λ the eχtreme: test-driving a functional language
14. λ the eχtreme: test-driving a functional language 14
$ git checkout tags/Canary
15. λ the eχtreme: test-driving a functional language 15
Interlude
16. λ the eχtreme: test-driving a functional language 16
Recap
Λ A Case Class
Λ An Object
Λ A FeatureSpec
Λ A FunSpec!
17. Canary
17
Can our canary get
account info?
Only with a
Dropbox account.
λ the eχtreme: test-driving a functional language
18. λ the eχtreme: test-driving a functional language 18
Actors
19. Actors
19
What happens
when our account
info changes?
Our expectation is
wrong.
λ the eχtreme: test-driving a functional language
What happens
when we change
our expectation?
Our test fails.
20. Actors
20
Why does it fail?
Our
implementation is
wrong.
λ the eχtreme: test-driving a functional language
Where do we find
the right
implementation?
Look in the upper
left.
21. λ the eχtreme: test-driving a functional language 21
$ git checkout tags/Actors
22. Actors
22
How can we call
Dropbox?
Using HTTP
λ the eχtreme: test-driving a functional language
How will Dropbox
know us?
We’ll give it a
token.
23. Actors
23
When will we get
our account info?
In the future!
λ the eχtreme: test-driving a functional language
24. λ the eχtreme: test-driving a functional language 24
Interlude
25. λ the eχtreme: test-driving a functional language 25
Recap
Λ Configuration Management
Λ Implicit conversions
Λ An Actor
Λ An Actor Testkit
Λ The Future!
26. λ the eχtreme: test-driving a functional language 26
Care and
Feeding
27. MinedReading
27
What happens if we
fail?
You tell me!
λ the eχtreme: test-driving a functional language
Do we catch an
exception?
We intercept it.
29. λ the eχtreme: test-driving a functional language 29
$ git checkout tags/MinedReading
30. λ the eχtreme: test-driving a functional language 30
Etude
31. λ the eχtreme: test-driving a functional language 31
Recap
Λ Stubbing a probe’s return value
Λ Intercepting an expected exception
Λ Iterating over an optional value
33. Zed
33
May I share
something with
you?
Of course!
λ the eχtreme: test-driving a functional language
I would rather you
put than post.
34. Zed
34
What if I don’t have
a file lying around?
You can share
anything you like
over the web.
λ the eχtreme: test-driving a functional language
But how will you
know what it
should look like on
the other side?
36. λ the eχtreme: test-driving a functional language 36
$ git checkout tags/Zed
37. λ the eχtreme: test-driving a functional language 37
Interlude
38. Zed
38
Of course!
Does Zed use
Monads?
λ the eχtreme: test-driving a functional language
Where’s a good
definition of what
a Monad is?
Anywhere but
urbandictionary.com
39. Zed
39
This one is pretty good:
You Could Have Invented Monads!
λ the eχtreme: test-driving a functional language
The original is still the best:
Monads for functional programming
For today, we can look at them as a way
of chaining operations.
40. λ the eχtreme: test-driving a functional language 40
Recap
Λ Implicit transformation from a File to an
abstract representation of its contents
41. λ the eχtreme: test-driving a functional language 41
Birds of a
feather
42. Bof
42
We just need a little
coordination.
What if I want to
share all my
pictures of
canaries with
friends at once?
λ the eχtreme: test-driving a functional language
43. Bof
43
Recursive
traversal fits a tree
like a glove.
How about a little
recursion?
λ the eχtreme: test-driving a functional language
44. λ the eχtreme: test-driving a functional language 44
$ git checkout tags/Bof
45. λ the eχtreme: test-driving a functional language 45
Interlude
46. CallCC
46
And on and on?
But what happens
if the recursion
goes on and on?
λ the eχtreme: test-driving a functional language
47. CallCC
47
And on and on and on and on and on and
on and on and on and on and on and on
and on and on and on and on and on and
on and on and on and on and on and on
and on and on and on and on and on and on
and on and on and on and on and on and on
and on and on and on and on and on and on and on
and on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on and on and on and on and on
λ the eχtreme: test-driving a functional language
48. λ the eχtreme: test-driving a functional language 48
Recap
Λ Changing recursive traversal to iterative
using continuations