25
Into the Land of Lambda (into functional-programming (one (programmers-journey)))

Into the Land of lambda, One Programmer's Journey Into Functional Programming

Embed Size (px)

DESCRIPTION

A travelogue into functional programming for the Sarasota Java User's Group

Citation preview

Page 1: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Into the Land of Lambda

(into functional-programming (one (programmers-journey)))

Page 2: Into the Land of lambda, One Programmer's Journey Into Functional Programming

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

[email protected]@mikepence

Page 3: Into the Land of lambda, One Programmer's Journey Into Functional Programming

The context of my experience…

Tools I have known and loved (starting out)

Page 4: Into the Land of lambda, One Programmer's Journey Into Functional Programming

The context of my experience…

You had to re-invent yourself every 2 years

Tools I have known and loved (oy! the 90’s!)

Page 5: Into the Land of lambda, One Programmer's Journey Into Functional Programming

The context of my experience…

True love since ’05 (well, love/hate with Rails)

Page 6: Into the Land of lambda, One Programmer's Journey Into Functional Programming

How come it isn’t getting any easier?

~25 years of OOP (for me)

Page 7: Into the Land of lambda, One Programmer's Journey Into Functional Programming

• 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

Our relationship with objects

It’s complicated

Page 8: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Reasoning about objects

Feels Like A Shell Game

Page 9: Into the Land of lambda, One Programmer's Journey Into Functional Programming

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!

Page 10: Into the Land of lambda, One Programmer's Journey Into Functional Programming

This Talk Gets Its Own Slide

“Are we there yet?”

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

http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

Page 11: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Is There a Different Way?

(Spoiler alert: There is!)

Page 12: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Another way

Disambiguating identity, value, time and mutation

Page 13: Into the Land of lambda, One Programmer's Journey Into Functional Programming

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

Page 14: Into the Land of lambda, One Programmer's Journey Into Functional Programming

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

Page 15: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Let’s See Some!

Code Examples

Page 16: Into the Land of lambda, One Programmer's Journey Into Functional Programming

• 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

Some Code Samples

Here they are!

Page 17: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Full of win!

A Whole New World

Page 18: Into the Land of lambda, One Programmer's Journey Into Functional Programming

• 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

Clojure / FP Wins

Reliability

Page 19: Into the Land of lambda, One Programmer's Journey Into Functional Programming

• 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

Clojure / FP Wins

Focus

"It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." — Alan Perlis

Page 20: Into the Land of lambda, One Programmer's Journey Into Functional Programming

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!

Page 21: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Clojure / FP Wins

Access to awesome libraries and community

https://github.com/trending?l=clojure&since=weekly

Page 22: Into the Land of lambda, One Programmer's Journey Into Functional Programming

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!

Page 23: Into the Land of lambda, One Programmer's Journey Into Functional Programming

Adorable!

Page 24: Into the Land of lambda, One Programmer's Journey Into Functional Programming

For Your Participation

Thank you!

Page 25: Into the Land of lambda, One Programmer's Journey Into Functional Programming

This subheading intentionally left blank

Questions and Discussion