Functional Programming in Clojure
-
Upload
troy-miles -
Category
Software
-
view
405 -
download
1
Transcript of Functional Programming in Clojure
![Page 1: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/1.jpg)
SoCal Code Camp 12+13 November 2016
Functional Programming in Clojure
![Page 2: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/2.jpg)
Troy Miles• Troy Miles aka the RocknCoder
• Over 37 years of programming experience
• Speaker and author
• Author of jQuery Essentials
• bit.ly/rc-jquerybook
• @therockncoder
![Page 3: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/3.jpg)
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
![Page 4: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/4.jpg)
Slides Online
• http://www.slideshare.net/rockncoder/functional-programming-in-clojure-68544774
![Page 5: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/5.jpg)
Our Agenda• Clojure?
• Lisp
• The Java Virtual Machine
• Leiningen
• Functional Programming
![Page 6: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/6.jpg)
Clojure?
![Page 7: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/7.jpg)
–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.”
![Page 8: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/8.jpg)
What is Clojure?
• A dynamic, general-purpose programming language
• A Modern Lisp
• Designed to be hosted
• Functional, but practical
![Page 9: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/9.jpg)
Other Hosts for Clojure
• ClojureScript - Compiles to JavaScript
• Clojure CLR - Compiles to IL, for Microsoft’s CLR
![Page 10: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/10.jpg)
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
![Page 11: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/11.jpg)
Companies using Clojure• Walmart Labs
• Puppet Labs
• ThoughtWorks
• Amazon
• Groupon
• Intuit
• Salesforce
• Zendesk
![Page 12: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/12.jpg)
–Paul Graham
“I suppose I should learn Lisp, but it seems so foreign.”
![Page 13: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/13.jpg)
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
![Page 14: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/14.jpg)
Lisp Innovations• recursive function
• dynamically allocated memory
• garbage collection
• lexical closures
• macros
![Page 15: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/15.jpg)
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
![Page 16: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/16.jpg)
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
![Page 17: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/17.jpg)
Build Tools
• Apache Maven
• Leiningen
![Page 18: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/18.jpg)
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
![Page 19: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/19.jpg)
Using Leiningen
• Searches from repos
• lein new app my-stuff
![Page 20: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/20.jpg)
Directory├── CHANGELOG.md ├── LICENSE ├── README.md ├── doc │ └── intro.md ├── project.clj ├── resources ├── src │ └── my_app │ └── core.clj └── test └── my_app └── core_test.clj
![Page 21: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/21.jpg)
Editors / IDEs
• Emacs - created in 1976
• Vim - created in 1991
• Eclipse + Counterclockwise - created in 2001
• Intellij IDEA + Cursive - created in 2001
![Page 22: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/22.jpg)
What is Functional Programming?
![Page 23: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/23.jpg)
Key Functional Features• Pure functions
• First-class / High order functions
• Immutable data
• Recursion
• Referential transparency
![Page 24: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/24.jpg)
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
![Page 25: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/25.jpg)
Sample Languagesmostly functional mixed mostly imperative
Lisp/Scheme JavaScript Java
ML Scala C#
Haskell Python C++
Clojure Dart Swift
F# Lua Ruby
Erlang R Kotlin
![Page 26: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/26.jpg)
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
![Page 27: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/27.jpg)
Pure Functions Are Super
• Cacheable
• Portable
• Self-documenting
• Testable
• Reasonable
![Page 28: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/28.jpg)
First-Class Functions
• Assigned to variables
• Stored in arrays
• Passed as arguments to other functions
• Returned from functions
![Page 29: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/29.jpg)
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
![Page 30: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/30.jpg)
Code Samples
![Page 31: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/31.jpg)
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
![Page 32: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/32.jpg)
FizzBuzz in Clojure1 (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)
![Page 33: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/33.jpg)
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/
![Page 34: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/34.jpg)
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/
![Page 35: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/35.jpg)
And Even More Links
• SICP Online - http://web.mit.edu/alexmv/6.037/sicp.pdf
• Paul Graham - http://paulgraham.com/avg.html
![Page 36: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/36.jpg)
Summary
• Clojure is functional language on the JVM
• It is interoperable with Java
• It has a Lisp syntax which is not
![Page 37: Functional Programming in Clojure](https://reader033.fdocuments.in/reader033/viewer/2022051709/5870f7a71a28ab5f528b4f45/html5/thumbnails/37.jpg)
–Edsger Dijkstra
“Object-oriented programming is an exceptionally bad idea which could only have
originated in California.”