SlideShare una empresa de Scribd logo
1 de 54
Descargar para leer sin conexión
Get Functional Programming
with Clojure
by John Stevenson
@jr0cket clojure.practical.li
@jr0cket
Speaker, author, conference organiser & community obsessed
developer.
Love Clojure, Emacs, Cats, Cycling & Agile development.
@Heroku
@SalesforceDevs
#Trailhead
In a galaxy far, far away… London, UK
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”)
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
- 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 share 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
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
Recursion
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 optomisation
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
Lazy Evaluation
Only return a value when necessary
● maintain precision
● optomise evaluation
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
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
@jr0cket
@Heroku
@SalesforceDevs
#Trailhead
In a galaxy far, far away… London, UK

Más contenido relacionado

La actualidad más candente

Excuse me, sir, do you have a moment to talk about tests in Kotlin
Excuse me, sir, do you have a moment to talk about tests in KotlinExcuse me, sir, do you have a moment to talk about tests in Kotlin
Excuse me, sir, do you have a moment to talk about tests in Kotlin
leonsabr
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
cqtt191
 

La actualidad más candente (20)

Clojure in real life 17.10.2014
Clojure in real life 17.10.2014Clojure in real life 17.10.2014
Clojure in real life 17.10.2014
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.
 
Requery overview
Requery overviewRequery overview
Requery overview
 
Excuse me, sir, do you have a moment to talk about tests in Kotlin
Excuse me, sir, do you have a moment to talk about tests in KotlinExcuse me, sir, do you have a moment to talk about tests in Kotlin
Excuse me, sir, do you have a moment to talk about tests in Kotlin
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
 
Making Java Groovy (JavaOne 2013)
Making Java Groovy (JavaOne 2013)Making Java Groovy (JavaOne 2013)
Making Java Groovy (JavaOne 2013)
 
Seeking Clojure
Seeking ClojureSeeking Clojure
Seeking Clojure
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019
 
A Taste of Clojure
A Taste of ClojureA Taste of Clojure
A Taste of Clojure
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
Scale up your thinking
Scale up your thinkingScale up your thinking
Scale up your thinking
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Why GC is eating all my CPU?
Why GC is eating all my CPU?Why GC is eating all my CPU?
Why GC is eating all my CPU?
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Kotlin - Better Java
Kotlin - Better JavaKotlin - Better Java
Kotlin - Better Java
 

Similar a Fun with Functional Programming in Clojure

Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
elliando dias
 

Similar a Fun with Functional Programming in Clojure (20)

Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
 
Get into Functional Programming with Clojure
Get into Functional Programming with ClojureGet into Functional Programming with Clojure
Get into Functional Programming with Clojure
 
Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally - John Stevenson - Codemotion Rome 2017Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally - John Stevenson - Codemotion Rome 2017
 
Thinking Functionally with Clojure
Thinking Functionally with ClojureThinking Functionally with Clojure
Thinking Functionally with Clojure
 
Functional web with clojure
Functional web with clojureFunctional web with clojure
Functional web with clojure
 
Get Functional Programming with Clojure
Get Functional Programming with ClojureGet Functional Programming with Clojure
Get Functional Programming with Clojure
 
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in ClojurescriptProgscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to Clojure
 
Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners
 
Functional (web) development with Clojure
Functional (web) development with ClojureFunctional (web) development with Clojure
Functional (web) development with Clojure
 
Clojure
ClojureClojure
Clojure
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrency
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
Clojure 1a
Clojure 1aClojure 1a
Clojure 1a
 
Java
JavaJava
Java
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
 
Carlo Sciolla - Above and beyond type systems with clojure.spec - Codemotion ...
Carlo Sciolla - Above and beyond type systems with clojure.spec - Codemotion ...Carlo Sciolla - Above and beyond type systems with clojure.spec - Codemotion ...
Carlo Sciolla - Above and beyond type systems with clojure.spec - Codemotion ...
 

Más de Codemotion

Más de Codemotion (20)

Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
 
Pompili - From hero to_zero: The FatalNoise neverending story
Pompili - From hero to_zero: The FatalNoise neverending storyPompili - From hero to_zero: The FatalNoise neverending story
Pompili - From hero to_zero: The FatalNoise neverending story
 
Pastore - Commodore 65 - La storia
Pastore - Commodore 65 - La storiaPastore - Commodore 65 - La storia
Pastore - Commodore 65 - La storia
 
Pennisi - Essere Richard Altwasser
Pennisi - Essere Richard AltwasserPennisi - Essere Richard Altwasser
Pennisi - Essere Richard Altwasser
 
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
 
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
 
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
 
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 - Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
 
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
 
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
 
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
 
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
 
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
 
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
 
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
 
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
 
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
 
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
 
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
 

Último

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

Fun with Functional Programming in Clojure

  • 1. Get Functional Programming with Clojure by John Stevenson @jr0cket clojure.practical.li
  • 2. @jr0cket Speaker, author, conference organiser & community obsessed developer. Love Clojure, Emacs, Cats, Cycling & Agile development. @Heroku @SalesforceDevs #Trailhead In a galaxy far, far away… London, UK
  • 3. Why Functional Programming it's not just because it's really fun...
  • 5. 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
  • 7. 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)
  • 8. 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
  • 9. 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
  • 10. Clojure General purpose language hosted on JVM, JavaScript & CLR
  • 11. Clojure / ClojureScript A hosted language with simple interoperability with the host language - (java.Util.Date.) - (js/alert “Client side apps are easier in Clojure”)
  • 12. 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”))
  • 13. Persistent Data Structures - 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}
  • 14. Persistent Data Structures share memory Each function creates a new vector Memory space for values is shared between each vector
  • 15. 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
  • 16. Higher Order Functions Functions always return a value & can be used as an argument to another function
  • 17. 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
  • 18. Recursion 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
  • 19. Recursion - tail call optomisation 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
  • 20. Lazy Evaluation Only return a value when necessary ● maintain precision ● optomise evaluation
  • 21. Sequence / List Comprehension Iterate over collections
  • 22. Sequence / List Comprehension Iterating through a range of generated values to create a list of 2 value vectors
  • 23. Immutability - local binding Assignments made locally are immutable
  • 24. 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
  • 25. 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
  • 26. Concurrency syntax - atoms An online card game has players that can join and have their winnings tracked
  • 27. Concurrency syntax - atoms The join-game function adds players to the atom by their name, but only up to 2 players
  • 28. Concurrency syntax - refs for sync updates The join-game-safely adds players to the ref and alters their account & game account
  • 29. Putting it all together Let's find all the most common words used in a popular Science Fiction novel
  • 30. Clojure General purpose language hosted on JVM, JavaScript & CLR
  • 31. Tools to learn Clojure inspire them & build up their motivation
  • 32. Clojure support in many different tools
  • 33. Leiningen - Clojure powered build automation
  • 34.
  • 38.
  • 39. Examples, examples, examples we learn by example...
  • 40. 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.
  • 42.
  • 46.
  • 47. Testing your Clojure skills...
  • 48.
  • 49. Clojurian Community in Person Probably the most active language-specific developer communities in London
  • 50. 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
  • 53. Take your own journey into Clojure