On Github julienXX / clojure-is-love
A presentation by Julien Blanchard
Small introduction to the language
Some features I like about Clojure:
Macros Multi-methods Pattern matching; hello ; Comment 123456789 ; Long 1234567898765N ; Arbitrary precision integer 1.234 ; Doubles 1.234M ; Big decimals 22/7 ; Rational numbers "chunky bacon" ; Strings chunky bacon ; Symbols :chunky :bacon ; Keywords true false ; Boolean nil ; Null #"^chunky\s+bacon$" ; RegExp \A \b \u2603 ; Characters
; Lists
(1 2 3 4 5)
(chunky bacon)
(list 1 2 3)
; Vectors
[1 2 3 4 5]
[chunky bacon]
; Maps
{:chunky "bacon", :foo "bar"}
{:one 1 :two 2}
; Sets
#{:chunky :bacon}
(if (> 2 1) ; test
"this will be returned" ; true
"this won't") ; false
(cond
(< n 0) "negative"
(> n 0) "positive"
:else "zero"))
plus and, or, when, if-not...
(println "Hello Functional programmers!") ; Hello Functional programmers! ; => nil (println (+ 1 2 3)) ; 6 ; => nil
(defn hello
"Print a greeting"
[first-name last-name]
(let [full (str first-name " " last-name)]
(println full)))
Parentheses are weird!
defn hello
"Print a greeting"
[first-name last-name]
let [full (str first-name " " last-name)]
println full
(unless (> 1 2)
"this will be returned"
"this won't'")
(defmacro unless
[test-exp then else]
`(if (not ~test-exp)
~then
~else))
(defmacro assert-equals [expected actual]
`(when-not (= ~actual ~expected)
(throw
(AssertionError.
(str "Expected " ~expected " but was " ~actual)))))
(assert-equals 6 5)
; => AssertionError Expected 6 but was 5
(defmulti thoughts-of (fn [person] (:name person))) (defmethod thoughts-of "Guido" [person] (str (person :name) " has one way to do it.")) (defmethod thoughts-of "Rich" [person] (str (person :name) " values laziness.")) (defmethod thoughts-of :default [person] (str (person :name) " is not a well-known thinker."))
(thoughts-of {:name "Guido"})
; => "Guido has one way to do it."
(thoughts-of {:name "Rich"})
; => "Rich values laziness."
(thoughts-of {:name "Julien"})
; => "Julien is not a well-known thinker."
(def some-numbers [1 2 3 4 5]) (let [[_ _ _ x _] some-numbers] x) ; => 4
(defn get-parts
[ [x y z & others ] ]
(println "First three are: " x y z)
(println "Rest is: " others))
(get-parts [ 1 2 3 4 5 ] )
; => First three are: 1 2 3
; => Rest is: (4 5)
(use '[clojure.core.match :only (match)])
(doseq [n (range 1 101)]
(println
(match [(mod n 3) (mod n 5)]
[0 0] "FizzBuzz"
[0 _] "Fizz"
[_ 0] "Buzz"
:else n)))
I'm @julienXX on Github or Twitter.
Slides available at http://clojure-is-love.herokuapp.com