Paul Graham, the founder of startup incubator YCombinator, put it best when he described LISP as his old company's secret weapon. Think about, if you use all of the same tools as everyone else, how do you expect to achieve better results?
Clojure is a LISP language created in 2009 by Rich Hickey. Built initially on the Java Virtual Machine (JVM) it has since been ported to run on Microsoft and JavaScript. (That's right the browser). Clojure gives you all of the power and stability of the JVM without the clunkiness of Java.
Most developers have never worked with a functional language before and many who have found the use of parenthesis instead of braces intimidating. Don't worry. Once it is broken down to you, I think you will see the beauty of it.
In this fast and fun session, we will build an app using Clojure. We will enhance it, test it and explore why functional is a better programming model than OOPs. We will even explore why such programs are better at multitasking than object oriented ones.
Diamond Application Development Crafting Solutions with Precision
Functional Programming in Clojure
1. SoCal Code Camp 12+13 November 2016
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
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
25. Sample Languages
mostly functional mixed mostly imperative
Lisp/Scheme JavaScript Java
ML Scala C#
Haskell Python C++
Clojure Dart Swift
F# Lua Ruby
Erlang R Kotlin
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
28. First-Class Functions
• Assigned to variables
• Stored in arrays
• Passed as arguments to other functions
• Returned from functions
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