Triangle.rb Notes - Rubyist Guide To Functional Programming With Clojure
Rubyist Guide To Functional Programming With Clojure
2015-09-08 by Michael Stalker (Meetup)
What is Clojure?
Created by Rich Hickey in 2007
; a comment
; no single quoted strings please
"double quoted strings"
; numbers
42
; similar to symbols in ruby. see http://clojure.org/special_forms
:keyword
; map
{:key 42}
; vector
[1 2 42]
; quoted list
'(1 2 3)
; functional application
(list 1 2 3)
Lisp dialect
; add 1 plus 3
(+ 1 3) ; + is a function
;= 4
(+ 1 2 3 4) ; 10
(def my-number 42) ; a var; like a variable
(fn [x] (* 2 x)) ; create a function
(def double (fn [x] (* 2 x))) ; bind anonymous function to double var
(defn double2 [x] (* 2 x)) ; shorthand for defining a "named" function
Functional
Pass functions to and from other functions. Functions are first-class citizens.
Function as data
(defn double [x] (* 2 x))
(defn add-and-double [x y] (* 2 (+ x y)))
Book recommendation: Structure And Interpretation Of Computer Programs
Why
- Powerful abstractions
- It will make you a better programmer
"Clojure demands that you raise your game and it pays you back for doing so." - https://twitter.com/cemerick/status/16779517145 - It's fast
- It makes concurrency easy
See Moore's Law graph - It uses the JVM. Access any library in Java
- It favors immutability
- Immutability leads to understanding
- Understandability leads to fewer errors
How To Get Started
Book recommendation: Clojure Programming by Chas Emerick
Book recommendation: Clojure For The Brave And True by Daniel Higginbotham (companion website)
How Will It Help
- Don't mutate if you don't have to!
- Learn to love the block
- Learn to love Enumerable#inject
- Use the right tool for the job