Unveiling Design Patterns: A Visual Guide with UML Diagrams
Into the Land of lambda, One Programmer's Journey Into Functional Programming
1. Into the Land of Lambda
(into functional-programming (one
(programmers-journey)))
2. Who am I?
Currently, a programmer @livingsocial
Previously a software consultant at various Fortune 1000 companies, including
banking, pharma, and transportation sectors
Lots of work with start-ups since 2005
Speaker at first Rails Conf (‘06), Ruby Conf
(‘08)
Sarasota resident since ‘96
mike.pence@livingsocial.com
@mikepence
3. The context of my experience…
Tools I have known and loved (starting out)
4. The context of my experience…
Tools I have known and loved (oy! the 90’s!)
You had to re-invent yourself every 2 years
5. The context of my experience…
True love since ’05 (well, love/hate with Rails)
6. ~25 years of OOP (for me)
How come it isn’t getting any easier?
7. Our relationship with objects
It’s complicated
• Graphs (trees) of objects maintain
internal state at every level in the
call stack
• Bugs are the product of
programmatic logic + the transient
state of the world at the time of the
error
• Example-based unit or integration
tests are of limited usefulness (they
prove their example case, but
nothing more)
• No guarantee that calling the same
method with the same arguments
will return the same result (aka
referential transparency) because
variables
9. Attempts to make OOP better
Each with their own trade-offs…
• Dependency injection / AOP
• Following SOLID design principles
• Single Responsibility Principle
• Liskov Substitution Principle
• Etc.
• Hexagonal Architecture
• Ports and Adapters
• Pipes and Filters
Still the fundamental problem remains that
state is casually managed, if managed at all.
Nevermind concurrency!
10. This Talk Gets Its Own Slide
“Are we there yet?”
http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey
The conflation of behavior,
state, identity and time is a big
source of implicit complexity in
current object systems.
We should primarily be
programming with pure
functions and immutable
values.
- Rich Hickey
11. Is There a Different Way?
(Spoiler alert: There is!)
13. Statefulness, YAGNI
Avoid it most of the time. Manage it when you need it.
• 99% of the time, you don’t need internal state in functions / methods
• As an exercise, try passing in variables and having mutated values returned in
<language you use daily>
• But Clojure is not about avoiding state, it is about managing it with well-defined
semantics
• To re-iterate, identity is a sequence of states
• America is an identity
• The PIC (prez, vice prez) are transient
14. A couple more analogies
State is like garbage, or Buddhism (which is not)
• Clojure provides "managed time" in much the same way that GC's have come to
provide managed memory
• * My team at LS offered a lot of this content
• They say Buddhists with advanced training don't see a "video" through their eyes,
but something like a series of pictures, like a flip book
• This "epochal state model" concept is similar, differs in that there are multiple
perceivers observing/looking at many flowing identities
16. Some Code Samples
Here they are!
• http://nakkaya.com/2009/10/04/fractals-in-clojure-buddhabrot-fractal/
• http://wicketconsole.appspot.com/clojureWithTemplates
• http://www.slideshare.net/alexmiller/clojure-the-art-of-abstraction-7161663
18. Clojure / FP Wins
Reliability
• Code is “provable” because it lacks transient variables
– Tests pass in all required state
– Example-based (unit) tests are great
• Generative tests are better
• Stepping debuggers and similar approaches to diagnosing functions with variables
are simply not needed (but are available).
• My experience so far has been that a whole class of bugs simply don’t occur
19. Clojure / FP Wins
Focus
• You focus on a small set of hierarchical data structures and the functions that
process them
– Visualize any JSON nested structure you have seen
• Biggest take-away for me
– You simply don’t need all of the ceremony of OOP
"It is better to have 100 functions operate on one data structure than 10 functions on
10 data structures." — Alan Perlis
20. Clojure / FP Wins
Expressivity and elegance
• Your functions and the built-in functions, or library functions, are syntactical peers
• So no awkwardly implemented DSL’s
• No odd, arbitrary-seeming mix of infix, postfix and dot notation
• It is functions, all the way down
• So you can freely build your own vocabulary of functions to model your problem
domain and solution
• Which I am really excited to see in practice
• Also, macros!
21. Clojure / FP Wins
Access to awesome libraries and community
https://github.com/trending?l=clojure&since=weekly
22. Clojure / FP Wins
More
• Concurrent and performant
• On the JVM
• With all the goodness that brings
• Used by LivingSocial for their massive email processing needs
• Plays well with Java
• But not just for Java!
• CLR version
• Enables Unity3D and Unreal Engine programming!
• JavaScript version
• ClojureScript
• A whole awesome thing in its own right
• Port of Clojure’s immutable data structure libraries included
• Om and React.js, OMG!
• You could program for tablets, consoles, phones, etc.
• LightTable!