Functional Programming in Clojure
- 2. Troy Miles
• Troy Miles aka the RocknCoder
• Over 37 years of programming
experience
• Speaker and author
• Author of jQuery Essentials
• bit.ly/rc-jquerybook
• rockncoder@gmail.com
• @therockncoder
- 3. Build Mobile Apps!
• Develop mobile apps with
Ionic and AngularJS
• Learn the Ionic CLI
• Fetch data via ajax
• Deploy your app to Android &
iOS
• bit.ly/ionicvideo
- 7. –Paul Graham
“Lisp is worth learning for the profound
enlightenment experience you will have when
you finally get it; that experience will make you
a better programmer for the rest of your days,
even if you never actually use Lisp itself a lot.”
- 8. What is Clojure?
• A dynamic, general-purpose programming
language
• A Modern Lisp
• Designed to be hosted
• Functional, but practical
- 9. Other Hosts for Clojure
• ClojureScript - Compiles to JavaScript
• Clojure CLR - Compiles to IL, for Microsoft’s CLR
- 10. ClojureScript
• Clojure + ClojureScript used together by 66% of the
community
• Om / Reagent - Interfaces for Facebook’s React
Library
• Mori - ClojureScript’s Immutable data structure for
vanilla JavaScript
- 11. Companies using Clojure
• Walmart Labs
• Puppet Labs
• ThoughtWorks
• Amazon
• Facebook
• Groupon
• Intuit
• Salesforce
• Zendesk
- 13. Lisp? Lisp!
• Created in 1958 by John McCarthy
• Second oldest high-level language still in use today
• Influenced by Alonzo Church’s lambda calculus
• A family of languages including: Common Lisp,
Scheme, Emacs Lisp
- 15. The Java Virtual Machine
• An abstract computing machine that allows a
computer run a Java program
• Type system
• Garbage collection
• Threads
• Just-in-time compiler (JIT)
• Favorite target of criminal hackers
- 16. JVM Languages
• Ceylon - Java competitor from Red Hat
• Groovy - OOP langage
• JRuby - Ruby on the JVM
• Jython - Python on the JVM
• Kotlin - Java competitor from JetBrains
• Rhino/Nashorn - JavaScript engines
• Scala - OOP / Functional language
- 18. Leiningen
• A tool for automating Clojure projects
• Written in Clojure
• Open source and maintained by a large community
• A play on another famous build tool, Apache Ant
- 21. Editors / IDEs
• Emacs - created in 1976
• Vim - created in 1991
• Eclipse + Counterclockwise - created in 2001
• Intellij IDEA + Cursive - created in 2001
- 23. Key Functional Features
• Pure functions
• First-class / High order functions
• Immutable data
• Recursion
• Referential transparency
- 24. Functional vs. Imperative
what? functional imperative
primary construct function class instance
state change bad important
order of execution not important important
flow control
function calls
recursion
loops, conditionals,
method calls
- 26. Pure Functions
• Must return a value
• Must accept at least one argument
• Can’t produce any side-effects
• Must return the same output for a given input
- 27. Pure Functions Are Super
• Cacheable
• Portable
• Self-documenting
• Testable
• Reasonable
- 29. Higher-Order Functions
• Accept other functions as parameter
• And/or return a function
• Allows for the creation of function factories
• This is the core of the curry function
- 31. 1 (function () {
2 'use strict';
3 const fizzBuzz = function () {
4 for (let i = 1; i <= 100; i += 1) {
5 let printVal = i + ' ';
6 if (i % 3 === 0) {
7 printVal += 'Fizz';
8 }
9 if (i % 5 === 0) {
10 printVal += 'Buzz';
11 }
12 console.info(printVal);
13 }
14 };
15 fizzBuzz();
16 }());
FizzBuzz in JavaScript
- 32. FizzBuzz in Clojure
1 (defn fizzbuzz [start finish]
2 (map (fn [n]
3 (cond
4 (zero? (mod n 15)) "FizzBuzz"
5 (zero? (mod n 3)) "Fizz"
6 (zero? (mod n 5)) "Buzz"
7 :else n))
8 (range start finish)))
9
10 (fizzbuzz 1 100)
- 33. Links
• Clojure - http://clojure.org/
• ClojureScript - http://clojurescript.org/
• ClojureCLR - http://clojure.org/about/clojureclr
• Clojure Docs - http://clojure-doc.org/
• Try Clojure - http://www.tryclj.com/
- 34. More Links
• Om - https://github.com/omcljs/om
• Reagent - https://reagent-project.github.io/
• Mori - https://github.com/swannodette/mori
• Clojure/Android - https://github.com/clojure-android
• Leiningen - http://leiningen.org/
- 35. And Even More Links
• SICP Online - http://web.mit.edu/alexmv/6.037/
sicp.pdf
• Paul Graham - http://paulgraham.com/avg.html
- 36. Summary
• Clojure is functional language on the JVM
• It is interoperable with Java
• It has a Lisp syntax which is not