SlideShare a Scribd company logo
Thinking Functionally with
Clojure
by John Stevenson
@jr0cket
www.practical.li
Thinking Functionally with Clojure
Why Functional Programming
it's not just because it's really fun...
Complex Systems
… are simply not
easy to understand
@jr0cket
Josh Smith - Tweet
Original Gif
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
Side Causes term coined by
@krisajenkins
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”)
Clojure - basic syntax for this talk
( ) ;; an empty list. The first element of a list is evaluated as a function call
(function-name data) ;; call a function with the data as its argument
(def name “data-or-value”) ;; assign (bind) a name to a data or legal value
:keyword-name ;; a keyword is a name that points to itself
;; Thread-first macro - chain function calls, passing the result of each call as the first
argument to the next function. The ,,, indicates where the resulting argument goes.
(-> (function-a “data”)
(function-b ,,,) ;; In Clojure commas , are whitespace
(function-c ,,, “data”))
Persistent data structures
Built-in Immutability leading to deterministic code
List, Vector, Map & Set
Clojure’s built-in data structures are all immutable
- returning a new data structure when a function is applied
(list 1 2 3 4 5) ‘(“fish” “chips” 42)
(vec ‘(1 2 3 4)) [1 2 3 4]
{:key “value”} {:name “John” :skill “conferencing”}
(set ‘(1 2 3 4 4)) #{1 2 3 4}
Persistent Data Structures - shared memory
Each function creates a new vector
Memory space for values is shared
between each vector
Persistent Data Structures -
shared memory
By sharing memory you
can apply functions
over and over again
effectively
Values persist until
they are no longer
referenced
Concurrency is Easier
Concurrency is much easier to write and reason about because of
- Pure functions
- Immutability is encouraged by default
- Persistent Data Structures
- All values are immutable
- unless explicitly wrapped in an atom or ref
- state changes managed atomically (software transactional memory)
- core.async library allows you to write asynchronous code as easily as sequential
code
Using
persistent data structures
Iterate over collections
Generate new collections by applying functions
Sequence / List Comprehension
Iterate over collections
Sequence / List Comprehension
Iterating through a range of generated values to create a list of 2 value vectors
Immutability - local binding
Assignments made locally are immutable
- words is a local binding to the result of running the function upper-case on “Hello
World”
- letter->clack is a function that converts a character to a code
Lazy Evaluation
Only return a value when necessary
● maintain precision
● optimise evaluation
Lazy Evaluation
Clojure's lazy sequences
- returns a reference to a file and step through it one line at a time
- line-seq returns a lazy sequence of lines, so we can read files larger than
available memory, one line at a time
Polymorphism
Functions evaluate different algorithms based on arity of arguments
Recursion & Polymorphism
Process a collection of values by feeding the remaining elements back to the function
- the sum function is polymorphic, it has different behaviours that could be
evaluated depending on if passed 1 or 2 arguments
Recursion - tail call optimisation
Protect the heap space from blowing by using the recur function
Using recur in the last line is the same as calling sum, however the memory required
from the previous sum function call is over-written in memory. So only 1 memory slot
is used instead of 10 billion
Higher Order Functions
A function that takes one or more functions as arguments, or
a function that returns a function
Higher Order Functions
Functions always return a value & can be used as an argument to another function
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
Design idiom:
Composing
functions
together
You can think of functional
design as evaluating one or
more functions in series of
functions
Functional Composition - map
Map the function inc over each number of the collection,
returns a new collection of numbers incremented by 1
Functional Composition - reduce
Reduce the numbers inside a collection to a single value by applying the + function,
returns a single value
Higher Order functions - partial, comp
Higher order functions can return a function
- The comp function composes other functions together
- The partial function allows you to lazily add another argument to a function
Managing State
State changes should be managed easily
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
Parallelism
Scale to the maximum
What is Parallelism
In simple terms: run the same code across multiple CPU’s
Parallelism - pmap
pmap - the same as map but in parallel
Parallelism - reduce
Significantly lower completion time running in parallel using Immutable data
structures
https://github.com/aphyr/tesser
Parallelism - reducers & folds
Tools to learn Clojure
inspire them & build up their motivation
Clojure support in many different tools
Leiningen - Clojure powered
build automation
Thinking Functionally with Clojure
LightTable - Instarepl
Emacs & Spacemacs
Figwheel (flappy birds example)
Thinking Functionally with Clojure
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
Thinking Functionally with Clojure
Github
Clojure-through-code Git repository
http://practical.li/clojure-webapps
Thinking Functionally with Clojure
Testing your Clojure skills...
Thinking Functionally with Clojure
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
@jr0cket
@Heroku
@SalesforceDevs
#Trailhead
In a galaxy far, far away… London, UK

More Related Content

What's hot (20)

PDF
A Reflective Approach to Actor-Based Concurrent Context-Oriented Systems
Takuo Watanabe
 
PDF
Learning Financial Market Data with Recurrent Autoencoders and TensorFlow
Altoros
 
PDF
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
AI Frontiers
 
PPTX
Task and Data Parallelism: Real-World Examples
Sasha Goldshtein
 
PDF
Functional Programming and Composing Actors
legendofklang
 
PDF
A Reflective Implementation of an Actor-based Concurrent Context-Oriented System
Takuo Watanabe
 
ODP
Generative Programming In The Large - Applied C++ meta-programming
Schalk Cronjé
 
PDF
FCN-Based 6D Robotic Grasping for Arbitrary Placed Objects
Kusano Hitoshi
 
PDF
Dynomite Nosql
elliando dias
 
PDF
RNN, LSTM and Seq-2-Seq Models
Emory NLP
 
PDF
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
PDF
iOS: Frameworks and Delegation
Jussi Pohjolainen
 
PDF
iOS Selectors Blocks and Delegation
Jussi Pohjolainen
 
PPT
Java 7
Tomasz Wrobel
 
PDF
Comparing different concurrency models on the JVM
Mario Fusco
 
PDF
Attention mechanisms with tensorflow
Keon Kim
 
PDF
Seq2Seq (encoder decoder) model
佳蓉 倪
 
PDF
Understanding Large Social Networks | IRE Major Project | Team 57
Raj Patel
 
PDF
Scaling Deep Learning with MXNet
AI Frontiers
 
PPTX
Class 32: Interpreters
David Evans
 
A Reflective Approach to Actor-Based Concurrent Context-Oriented Systems
Takuo Watanabe
 
Learning Financial Market Data with Recurrent Autoencoders and TensorFlow
Altoros
 
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
AI Frontiers
 
Task and Data Parallelism: Real-World Examples
Sasha Goldshtein
 
Functional Programming and Composing Actors
legendofklang
 
A Reflective Implementation of an Actor-based Concurrent Context-Oriented System
Takuo Watanabe
 
Generative Programming In The Large - Applied C++ meta-programming
Schalk Cronjé
 
FCN-Based 6D Robotic Grasping for Arbitrary Placed Objects
Kusano Hitoshi
 
Dynomite Nosql
elliando dias
 
RNN, LSTM and Seq-2-Seq Models
Emory NLP
 
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
iOS: Frameworks and Delegation
Jussi Pohjolainen
 
iOS Selectors Blocks and Delegation
Jussi Pohjolainen
 
Comparing different concurrency models on the JVM
Mario Fusco
 
Attention mechanisms with tensorflow
Keon Kim
 
Seq2Seq (encoder decoder) model
佳蓉 倪
 
Understanding Large Social Networks | IRE Major Project | Team 57
Raj Patel
 
Scaling Deep Learning with MXNet
AI Frontiers
 
Class 32: Interpreters
David Evans
 

Viewers also liked (16)

PPTX
Evaluaton of parentrals
SUJIT DAS
 
PDF
Go conference 2017 Lightning talk
mokelab
 
DOCX
Читання мовчки. Джерельце, Старий рибалка
Ковпитська ЗОШ
 
PDF
Concourse CI meetup-2017-03-24
VMware Tanzu Korea
 
PDF
Long Tail SEO w e-commerce
Tomasz Kołkiewicz
 
PDF
Membangun Sinergitas Perencanaan Melalui Roadmap Inovasi Sektor Publik
Tri Widodo W. UTOMO
 
PDF
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
Spark Summit
 
PDF
Getting into public speaking at conferences
John Stevenson
 
PPT
Salesforce Summer of Hacks London - Introduction
John Stevenson
 
PDF
5 Plans for Nasty Weather
Buildium
 
PDF
Some basic FP concepts
Falko Riemenschneider
 
PDF
Clojure - Why does it matter?
Falko Riemenschneider
 
PDF
ClojureScript Introduction
Falko Riemenschneider
 
PDF
Clojure+ClojureScript Webapps
Falko Riemenschneider
 
PDF
Making design decisions in React-based ClojureScript web applications
Falko Riemenschneider
 
PDF
ClojureScript - A functional Lisp for the browser
Falko Riemenschneider
 
Evaluaton of parentrals
SUJIT DAS
 
Go conference 2017 Lightning talk
mokelab
 
Читання мовчки. Джерельце, Старий рибалка
Ковпитська ЗОШ
 
Concourse CI meetup-2017-03-24
VMware Tanzu Korea
 
Long Tail SEO w e-commerce
Tomasz Kołkiewicz
 
Membangun Sinergitas Perencanaan Melalui Roadmap Inovasi Sektor Publik
Tri Widodo W. UTOMO
 
Fault Tolerance in Spark: Lessons Learned from Production: Spark Summit East ...
Spark Summit
 
Getting into public speaking at conferences
John Stevenson
 
Salesforce Summer of Hacks London - Introduction
John Stevenson
 
5 Plans for Nasty Weather
Buildium
 
Some basic FP concepts
Falko Riemenschneider
 
Clojure - Why does it matter?
Falko Riemenschneider
 
ClojureScript Introduction
Falko Riemenschneider
 
Clojure+ClojureScript Webapps
Falko Riemenschneider
 
Making design decisions in React-based ClojureScript web applications
Falko Riemenschneider
 
ClojureScript - A functional Lisp for the browser
Falko Riemenschneider
 
Ad

Similar to Thinking Functionally with Clojure (20)

PDF
Fun with Functional Programming in Clojure
Codemotion
 
KEY
Clojure Intro
thnetos
 
PDF
Introduction to clojure
Abbas Raza
 
PDF
Clojure values
Christophe Grand
 
PPT
Clojure 1a
Krishna Chaytaniah
 
PPTX
Clojure 7-Languages
Pierre de Lacaze
 
PDF
I know Java, why should I consider Clojure?
sbjug
 
PDF
Clojure
Rohit Vaidya
 
PDF
Full Stack Clojure
Michiel Borkent
 
PDF
Clojure intro
Basav Nagur
 
PDF
Introduction to Clojure
Renzo Borgatti
 
KEY
(map Clojure everyday-tasks)
Jacek Laskowski
 
ODP
Getting started with Clojure
John Stevenson
 
PDF
Clojure - An Introduction for Lisp Programmers
elliando dias
 
PDF
Clojure made-simple - John Stevenson
JAX London
 
PDF
Pune Clojure Course Outline
Baishampayan Ghose
 
PDF
Functional programming with clojure
Lucy Fang
 
PDF
Introductory Clojure Presentation
Jay Victoria
 
ODP
Clojure basics
Knoldus Inc.
 
PDF
Clojure - A new Lisp
elliando dias
 
Fun with Functional Programming in Clojure
Codemotion
 
Clojure Intro
thnetos
 
Introduction to clojure
Abbas Raza
 
Clojure values
Christophe Grand
 
Clojure 7-Languages
Pierre de Lacaze
 
I know Java, why should I consider Clojure?
sbjug
 
Clojure
Rohit Vaidya
 
Full Stack Clojure
Michiel Borkent
 
Clojure intro
Basav Nagur
 
Introduction to Clojure
Renzo Borgatti
 
(map Clojure everyday-tasks)
Jacek Laskowski
 
Getting started with Clojure
John Stevenson
 
Clojure - An Introduction for Lisp Programmers
elliando dias
 
Clojure made-simple - John Stevenson
JAX London
 
Pune Clojure Course Outline
Baishampayan Ghose
 
Functional programming with clojure
Lucy Fang
 
Introductory Clojure Presentation
Jay Victoria
 
Clojure basics
Knoldus Inc.
 
Clojure - A new Lisp
elliando dias
 
Ad

More from John Stevenson (20)

PDF
ClojureX Conference 2017 - 10 amazing years of Clojure
John Stevenson
 
PDF
Confessions of a developer community builder
John Stevenson
 
PDF
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
John Stevenson
 
PDF
Introduction to Functional Reactive Web with Clojurescript
John Stevenson
 
PDF
Communication improbable
John Stevenson
 
PDF
Guiding people into Clojure
John Stevenson
 
PDF
Git and github - Verson Control for the Modern Developer
John Stevenson
 
PDF
So you want to run a developer event, are you crazy?
John Stevenson
 
PPTX
Trailhead live - Overview of Salesforce App Cloud
John Stevenson
 
PDF
Clojure for Java developers
John Stevenson
 
PPTX
Introducing the Salesforce platform
John Stevenson
 
PPT
Dreamforce14 Metadata Management with Git Version Control
John Stevenson
 
PPTX
Heroku Introduction: Scaling customer facing apps & services
John Stevenson
 
PPT
Developers guide to the Salesforce1 Platform
John Stevenson
 
PPTX
Developer week EMEA - Salesforce1 Mobile App overview
John Stevenson
 
PPT
Dreamforce 13 developer session: Git for Force.com developers
John Stevenson
 
PPT
Dreamforce 13 developer session: Introduction to Heroku
John Stevenson
 
PPTX
Salesforce CCT Munich 2013 Introducing heroku - elastic, polyglot platform as...
John Stevenson
 
PPTX
Building a great mobile experience on the force.com platforms
John Stevenson
 
PPTX
Introduction to Heroku - CCT London 2013
John Stevenson
 
ClojureX Conference 2017 - 10 amazing years of Clojure
John Stevenson
 
Confessions of a developer community builder
John Stevenson
 
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
John Stevenson
 
Introduction to Functional Reactive Web with Clojurescript
John Stevenson
 
Communication improbable
John Stevenson
 
Guiding people into Clojure
John Stevenson
 
Git and github - Verson Control for the Modern Developer
John Stevenson
 
So you want to run a developer event, are you crazy?
John Stevenson
 
Trailhead live - Overview of Salesforce App Cloud
John Stevenson
 
Clojure for Java developers
John Stevenson
 
Introducing the Salesforce platform
John Stevenson
 
Dreamforce14 Metadata Management with Git Version Control
John Stevenson
 
Heroku Introduction: Scaling customer facing apps & services
John Stevenson
 
Developers guide to the Salesforce1 Platform
John Stevenson
 
Developer week EMEA - Salesforce1 Mobile App overview
John Stevenson
 
Dreamforce 13 developer session: Git for Force.com developers
John Stevenson
 
Dreamforce 13 developer session: Introduction to Heroku
John Stevenson
 
Salesforce CCT Munich 2013 Introducing heroku - elastic, polyglot platform as...
John Stevenson
 
Building a great mobile experience on the force.com platforms
John Stevenson
 
Introduction to Heroku - CCT London 2013
John Stevenson
 

Recently uploaded (20)

PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 

Thinking Functionally with Clojure