SlideShare a Scribd company logo
1 of 16
Download to read offline
Clojure Introduction
A showcase of data parsing, formatting, state, and
destructuring
Jason Basanese
Iteration
There are many ways to go over a dataset in clojure. Here are a just a few functions
● Reduce
● Map
● Filter
● Doseq
● Reduce-kv
● Plain old recursion
● Merge-with
● Loop
Any I didn't mention that you use frequently?
Reduce
This function is often the bread and butter of clojure projects. It is equivalent in
usefulness to the "for" from java. It's main function is to return a single value.
Super basic example:
(reduce + 1 '(1 1 1 1))
Which is 5. It would also work like
(reduce + '(1 1 1 1 1))
Reduce continued
Here is an example function that will apply a list of changes to a value:
(reduce (fn [value change] (change value)) value-x changes-list)
For instance if value-x was 1 and changes-list was '(inc inc inc) we would get 3.
Map
Map is the next most important clojure function. Here is a simple example:
(map inc `(1 1 1 1 1)) => (2 2 2 2 2)
It takes a function and applies it to every element of a list.
Map is lazy
Beware!!! It is lazy.
As in side effects will not take place. Values are calculated when used. So if we
have:
(defn hello[] (map print '(1 1 1 1)) "hello")
Then: (hello) => "hello" and will not print the 1's
By contrast, this function will print the 1's:
(defn hello-2[] (reduce (fn [_ value] (print value)) nil '(1 1 1 1)) "hello")
Filter
Filter will only keep elements selected by a conditional. For instance:
(filter #(> 1 %) '(0 0 1 2 3)) => (2 3)
Where #(> 1 %) is an anonymous function with % as the single argument. If the
conditional is true then the content is kept.
Rest
Any of these you would like me to cover?
● Doseq
● Reduce-kv
● Plain old recursion
● Merge-with
● Loop
Formatter
After seeing my code you are likely thinking it looks somewhat gross.
That is not nearly as bad as it can get.
Hence we have a formatter which you use like this:
Lein zprint sum.clj
It will create a new file called sum.clj.old that saved the unformatted version.
The formatted version will be in the original file name. These settings can be
changed.
Formatter examples
The following was a one liner:
(defn join
"two number addition"
[number1 number2]
(loop [times number1
total number2]
(if (> times 0) (do (recur (dec times) (inc total))) total)))
Feel free to try it yourself. It works magic. Just look up lein zprint and you should
find it.
State
In clojure we have immutable data types. So how are we supposed to store
changes?
ATOMS!!!
Here is an example atom creation:
(def status (atom false))
If we want the value of status we simply do @status
Changing state
If we want to change status, we could do:
(reset! status 1)
Now it is 1. Atoms are great because they are thread safe. That is why we go to this
trouble.
The other way is to do:
(swap! status inc) which would now give us 2 if we did @status.
For more examples ask about my evolution game.
Destructuring
Here is a super simple example of destructuring on a vector:
(let [[x y] [1 2]] (print x " " y)) prints 1 2
What we are doing here is vector destructuring where in the let statement we are
setting x to 1 and y to 2.
This can also be done with functions
Destructuring with functions
Ever wanted to have an easier way to pass the values of a map as parameters to a
function? Well today is your lucky day.
(def john {:height 10 :age 12 :weight 80 :first "john" :last "doe"})
(defn person-print
"A function that will print stats about the given
person from the person hashmap."
[:keys [height age weight first-name last-name]]
(print height age weight first-name last-name))
(person-print john)
The above will print 10 12 80 john doe
Places to go if you want to learn more
https://clojure.org/api/cheatsheet
https://clojuredocs.org/clojure.core/map
https://clojuredocs.org/clojure.core/reduce
https://clojuredocs.org/clojure.core/filter
https://github.com/kkinnear/lein-zprint
https://clojure.org/reference/atoms
https://clojure.org/guides/destructuring
Thank you for listening!
Please ask me any questions you have
Written and presented by Jason Basanese

More Related Content

What's hot

The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196Mahmoud Samir Fayed
 
Statistics - SoftMax Equation
Statistics - SoftMax EquationStatistics - SoftMax Equation
Statistics - SoftMax EquationAndrew Ferlitsch
 
Functional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersFunctional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersBartek Witczak
 
A practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) LearningA practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) LearningBruno Gonçalves
 
Q1 create a java desktop application to find the largest number among the t...
Q1  create a java desktop application to find the largest number  among the t...Q1  create a java desktop application to find the largest number  among the t...
Q1 create a java desktop application to find the largest number among the t...Manoj Bhakuni
 
NTHU AI Reading Group: Improved Training of Wasserstein GANs
NTHU AI Reading Group: Improved Training of Wasserstein GANsNTHU AI Reading Group: Improved Training of Wasserstein GANs
NTHU AI Reading Group: Improved Training of Wasserstein GANsMark Chang
 
Functions
FunctionsFunctions
FunctionsSV.CO
 
AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals Adam Book
 

What's hot (15)

The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196
 
Maths group ppt
Maths group pptMaths group ppt
Maths group ppt
 
Statistics - SoftMax Equation
Statistics - SoftMax EquationStatistics - SoftMax Equation
Statistics - SoftMax Equation
 
25 Debugging
25 Debugging25 Debugging
25 Debugging
 
Functional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersFunctional JS for everyone - 4Developers
Functional JS for everyone - 4Developers
 
VB 6
VB 6VB 6
VB 6
 
Json.parse() in JavaScript
Json.parse() in JavaScriptJson.parse() in JavaScript
Json.parse() in JavaScript
 
A practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) LearningA practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) Learning
 
Q1 create a java desktop application to find the largest number among the t...
Q1  create a java desktop application to find the largest number  among the t...Q1  create a java desktop application to find the largest number  among the t...
Q1 create a java desktop application to find the largest number among the t...
 
Calculadora
CalculadoraCalculadora
Calculadora
 
NTHU AI Reading Group: Improved Training of Wasserstein GANs
NTHU AI Reading Group: Improved Training of Wasserstein GANsNTHU AI Reading Group: Improved Training of Wasserstein GANs
NTHU AI Reading Group: Improved Training of Wasserstein GANs
 
Functions
FunctionsFunctions
Functions
 
AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals AWS Atlanta meetup CloudFormation conditionals
AWS Atlanta meetup CloudFormation conditionals
 
Avg q lenth
Avg q lenthAvg q lenth
Avg q lenth
 

Similar to Clojure introduction by Jason Basanese

Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in rmanikanta361
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language Md. Mahedi Mahfuj
 
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180Mahmoud Samir Fayed
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operators(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operatorsNico Ludwig
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 
Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojureLucy Fang
 
“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1SethTisue
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 

Similar to Clojure introduction by Jason Basanese (20)

Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in r
 
Writing Macros
Writing MacrosWriting Macros
Writing Macros
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operators(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operators
 
Python speleology
Python speleologyPython speleology
Python speleology
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojure
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
Python advance
Python advancePython advance
Python advance
 
“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 

Recently uploaded

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.comFatema Valibhai
 
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.pdfWave PLM
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
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.pdfkalichargn70th171
 
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 PrecisionSolGuruz
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
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.jsAndolasoft Inc
 
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 WorkerThousandEyes
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
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-...Steffen Staab
 
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.pdfkalichargn70th171
 
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...ICS
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
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 ...MyIntelliSource, Inc.
 

Recently uploaded (20)

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
 
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
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
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
 
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
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
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
 
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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
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-...
 
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
 
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...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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 ...
 

Clojure introduction by Jason Basanese

  • 1. Clojure Introduction A showcase of data parsing, formatting, state, and destructuring Jason Basanese
  • 2. Iteration There are many ways to go over a dataset in clojure. Here are a just a few functions ● Reduce ● Map ● Filter ● Doseq ● Reduce-kv ● Plain old recursion ● Merge-with ● Loop Any I didn't mention that you use frequently?
  • 3. Reduce This function is often the bread and butter of clojure projects. It is equivalent in usefulness to the "for" from java. It's main function is to return a single value. Super basic example: (reduce + 1 '(1 1 1 1)) Which is 5. It would also work like (reduce + '(1 1 1 1 1))
  • 4. Reduce continued Here is an example function that will apply a list of changes to a value: (reduce (fn [value change] (change value)) value-x changes-list) For instance if value-x was 1 and changes-list was '(inc inc inc) we would get 3.
  • 5. Map Map is the next most important clojure function. Here is a simple example: (map inc `(1 1 1 1 1)) => (2 2 2 2 2) It takes a function and applies it to every element of a list.
  • 6. Map is lazy Beware!!! It is lazy. As in side effects will not take place. Values are calculated when used. So if we have: (defn hello[] (map print '(1 1 1 1)) "hello") Then: (hello) => "hello" and will not print the 1's By contrast, this function will print the 1's: (defn hello-2[] (reduce (fn [_ value] (print value)) nil '(1 1 1 1)) "hello")
  • 7. Filter Filter will only keep elements selected by a conditional. For instance: (filter #(> 1 %) '(0 0 1 2 3)) => (2 3) Where #(> 1 %) is an anonymous function with % as the single argument. If the conditional is true then the content is kept.
  • 8. Rest Any of these you would like me to cover? ● Doseq ● Reduce-kv ● Plain old recursion ● Merge-with ● Loop
  • 9. Formatter After seeing my code you are likely thinking it looks somewhat gross. That is not nearly as bad as it can get. Hence we have a formatter which you use like this: Lein zprint sum.clj It will create a new file called sum.clj.old that saved the unformatted version. The formatted version will be in the original file name. These settings can be changed.
  • 10. Formatter examples The following was a one liner: (defn join "two number addition" [number1 number2] (loop [times number1 total number2] (if (> times 0) (do (recur (dec times) (inc total))) total))) Feel free to try it yourself. It works magic. Just look up lein zprint and you should find it.
  • 11. State In clojure we have immutable data types. So how are we supposed to store changes? ATOMS!!! Here is an example atom creation: (def status (atom false)) If we want the value of status we simply do @status
  • 12. Changing state If we want to change status, we could do: (reset! status 1) Now it is 1. Atoms are great because they are thread safe. That is why we go to this trouble. The other way is to do: (swap! status inc) which would now give us 2 if we did @status. For more examples ask about my evolution game.
  • 13. Destructuring Here is a super simple example of destructuring on a vector: (let [[x y] [1 2]] (print x " " y)) prints 1 2 What we are doing here is vector destructuring where in the let statement we are setting x to 1 and y to 2. This can also be done with functions
  • 14. Destructuring with functions Ever wanted to have an easier way to pass the values of a map as parameters to a function? Well today is your lucky day. (def john {:height 10 :age 12 :weight 80 :first "john" :last "doe"}) (defn person-print "A function that will print stats about the given person from the person hashmap." [:keys [height age weight first-name last-name]] (print height age weight first-name last-name)) (person-print john) The above will print 10 12 80 john doe
  • 15. Places to go if you want to learn more https://clojure.org/api/cheatsheet https://clojuredocs.org/clojure.core/map https://clojuredocs.org/clojure.core/reduce https://clojuredocs.org/clojure.core/filter https://github.com/kkinnear/lein-zprint https://clojure.org/reference/atoms https://clojure.org/guides/destructuring
  • 16. Thank you for listening! Please ask me any questions you have Written and presented by Jason Basanese