Thanks to visit codestin.com
Credit goes to www.slideshare.net

Get into Functional
Programming with Clojure
by John Stevenson
@jr0cket
clojure.practical.li
Why Functional Programming
it's not just because it's really fun...
OO
Software
in action
Josh Smith - Tweet
The Complexity Iceberg
- @krisajenkins
● complexity is very
dangerous when hidden
● You can't know what a
function does for certain if it
has side effects
Side Effects
Pure Functions
The results of the function are purely determined by its initial output and its own code
- no external influence, a function only uses local values
- referential transparency (the function can be replaced by its value)
Impure Functions - side causes
The results of the function are purely determined by its initial output and its own code
- behaviour externally influenced and non-deterministic
Eliminating Side Effects
Functional programming is about eliminating side effects where you can,
controlling them where you can't - @krisajenkins
The features in Functional Programming come from a
desire to reduce side effects
Clojure
General purpose language hosted on JVM, JavaScript & CLR
Clojure / ClojureScript
A hosted language with simple interoperability with the host language
- (java.Util.Date.)
- (js/alert “Client side apps are easier in Clojure”)
Persistent Data Structures
(list 1 2 3 4 5) ‘(“fish” “chip” 42)
(vec ‘(1 2 3 4)) [1 2 3 4]
{:key “value} {:name “John” :skill “talking in full rooms”}
(set ‘(1 2 3 4 4)) #{1 2 3 4}
Persistent Data
Structures
Persistent Data
Structures
Higher Order Functions
Functions always return a value & can be used as an argument to another function
- Chain functions over a data structure
Composing functions together
Example: current value of the Clojure project from the configuration file
- `slurp` in the project file, convert into a string and return the value at index 2
Recursion
Process a collection of values by feeding the remaining elements back to the function
- the sum function has different behaviour depending on if passed 1 or 2 arguments
Recursion - tail call optomisation
Protect the heap space from blowing by using the recur function
Lazy Evaluation
Only return a value when necessary
● maintain precision
● optomise evaluation
Sequence / List Comprehension
Sequence / List Comprehension
Iterating through a range of generated values to create a list of 2 value vectors
Concurrency is Easier
Concurrency is much easier to write and reason about because of
- Immutability by default
- Persistent Data Structures
- values are immutable
- functional isolation & pure functions
- state changes managed atomically (software transactional memory)
- core.async library allows you to write asynchronous code as easily as sequential
code
Safe State changes
Changing state safely by not changing it
● Persistent data structures
● Local bindings
Changing state safely by changing it atomically
● Software Transactional Memory (STM)
○ Gives an mechanism like an in-memory atomic database that manages mutable state changes
under the covers
● Atoms
● core.async
Concurrency syntax - atoms
An online card game has players that can join and have their winnings tracked
Concurrency syntax - atoms
The join-game function adds players to the atom by their name, but only up to 2
players
Concurrency syntax - refs for sync updates
The join-game-safely adds players to the ref and alters their account & game account
Putting it all together
Let's find all the most common words used in a popular Science Fiction novel
Clojure
General purpose language hosted on JVM, JavaScript & CLR
Tools to learn Clojure
inspire them & build up their motivation
Clojure support in many different tools
Leiningen - Clojure powered
build automation
LightTable - Instarepl
Emacs & Spacemacs
Figwheel (flappy birds example)
Examples, examples, examples
we learn by example...
Over 20 Books on Clojure...
Where to start with Clojure will be different...
Example:
I typically suggested BraveClojure.com as a starting
point, however many people prefer LivingClojure or
ClojureScript Unraveled...
Help people understand the relevance of a book and if
it's the right thing for them at that time.
Clojure.org & ClojureDocs.org
Github
Clojure-through-code Git repository
http://practical.li/clojure-webapps
Testing your Clojure skills...
Clojurian Community in Person
Probably the most active language-specific
developer communities in London
Learning by teaching others
I really started thinking in Clojure when I started talking to & teaching others
- Coding dojos
- talks on Clojure (starting with the basics, showing the art of the possible)
- moving on to running conferences
- workshops at hack days
Overtone live performance - MetaX
Overtone live performance - MetaX
Take your own journey into Clojure
Thank you
@jr0cket
jr0cket.co.uk

Get Functional Programming with Clojure