Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Clojure

Introduction to Clojure

A short introduction to Clojure, the principles, syntax and some function

Avatar for Deon Moolman

Deon Moolman

October 22, 2013
Tweet

Other Decks in Programming

Transcript

  1. “By relieving the brain of all unnecessary work, a good

    notation sets it free to concentrate on more advanced problems.” - Alfred North Whitehead Collections Basic Syntax? bit.ly/cljintro
  2. \c “string” 123 123.0 1/3 fred :name #”[0-9]+” nil true

    Collections Basic Syntax? bit.ly/cljintro
  3. (fred bob mary) [1 2 bob] {:name “bob”, “age” 12}

    #{1 3 5 “mary” joe} Collections Basic Syntax? bit.ly/cljintro
  4. Switching Gears (order :spatula 200) (+ 100 (* 5 2)

    5) - Some random Clojure code Basic Concepts bit.ly/cljintro
  5. (def myvar 123) (def myname “Deon”) (def person {:name “Bob”

    :age 12}) Basic Concepts bit.ly/cljintro Vars
  6. (fn [x] (+ x 10)) (def inc-ten ) #(+ %

    10) Basic Concepts bit.ly/cljintro Function definitions
  7. (def inc-ten ) (fn [x] (+ x 10)) (defn inc-ten

    [x] (+ x 10)) #(+ % 10) Basic Concepts bit.ly/cljintro Function definitions
  8. (ns intro.gears (:use [clojure.tools.logging]) (:require [intro.defs :as defs]) (:import [java.util.concurrent

    LinkedBlockingQueue])) (info “Hello world”) (defs/inc-ten 3) (LinkedBlockingQueue.) Basic Concepts bit.ly/cljintro Namespaces
  9. (def q (LinkedBlockingQueue.)) (.add q "test") (.peek q) (java.util.Calendar/getInstance) Calendar/MAY

    (proxy [Runnable] (run [] (println “Hello World”))) Basic Concepts bit.ly/cljintro Java interop
  10. (defn add-vector [pos vector] [(+ (first pos) (first vector)) (+

    (second pos) (second vector))]) (defn add-vector [[x y] [vx vy]] [(+ x vx) (+ y vy)]) (add-vector [1 2] [5 6]) => [6 7] Basic Concepts bit.ly/cljintro Destructuring
  11. (defn id-name-pair [person] [(:id person) (:name person)]) (defn id-name-pair [{name

    :name id :id}] [id name]) (defn id-name-pair [{:keys [name id]}] [id name]) Basic Concepts bit.ly/cljintro Destructuring
  12. “The four building blocks of the universe are fire, water,

    gravel and vinyl.” - Dave Barry Groundwork Functions bit.ly/cljintro
  13. if let (let [x 1, y 2] (+ x (*

    64 y))) (let [person (find-person id) account (find-account person)] (:balance account)) Groundwork Functions bit.ly/cljintro
  14. cond (cond (nil? name) “No name” (nil? age) “No age”

    :else “All fine”) if let Groundwork Functions bit.ly/cljintro
  15. loop recur (loop [x 1] (if (> x 5) x

    (recur (inc x))) if let cond (defn sum [x xs acc] (cond (nil? x) acc :else (recur (first xs) (rest xs) (+ acc x))) Groundwork Functions bit.ly/cljintro
  16. and or (and 1 5) => 5 (or 1 false)

    => 1 (and (old-enough? person) (qualifies? person)) (or (old-enough? person) (qualifies? person)) if let loop recur cond Groundwork Functions bit.ly/cljintro
  17. apply (defn sum3 [x y z] (+ x y z))

    (let [x [2 3 4]] (sum3 (nth x 0) (nth x 1) (nth x 2)) (let [x [2 3 4]] (apply sum3 x)) if let loop recur and or cond Groundwork Functions bit.ly/cljintro
  18. complement (defn too-big? [x] (> x 50)) (defn small-enough? [x]

    (not (too-big? x))) if let loop recur and or cond apply (defn complement [f] (fn [x] ...)) Groundwork Functions bit.ly/cljintro
  19. complement (defn too-big? [x] (> x 50)) (defn small-enough? [x]

    (not (too-big? x))) (def small-enough? (complement too-big?)) if let loop recur and or cond apply (defn complement [f] (fn [x] (not (f x)))) Groundwork Functions bit.ly/cljintro
  20. if let loop recur and or cond apply complement juxt

    (defn id-name-pair [person] [(:id person) (:name person)]) (def id-name-pair (juxt :id :name)) (map (juxt :width :length :height) items) (defn juxt [& fs] (fn [x] ...)) Groundwork Functions bit.ly/cljintro
  21. if let loop recur and or cond apply complement juxt

    (defn id-name-pair [person] [(:id person) (:name person)]) (def id-name-pair (juxt :id :name)) (map (juxt :width :length :height) items) (defn juxt [& fs] (fn [x] (vec (map ... fs)))) Groundwork Functions bit.ly/cljintro
  22. if let loop recur and or cond apply complement juxt

    (defn id-name-pair [person] [(:id person) (:name person)]) (def id-name-pair (juxt :id :name)) (map (juxt :width :length :height) items) (defn juxt [& fs] (fn [x] (vec (map #(% x) fs)))) Groundwork Functions bit.ly/cljintro
  23. if let loop recur and or cond apply complement juxt

    partial memoize fnil identity Groundwork Functions Core bit.ly/cljintro
  24. conj (conj [1 2 3] 4) => [1 2 3

    4] (conj ’(1 2 3) 4) => ’(4 1 2 3) (conj #{1 2 3} 4) => #{1 2 3 4} Groundwork Functions bit.ly/cljintro
  25. conj assoc (assoc {:name “Deon”} :age 21) => {:name “Deon”

    :age 21} Groundwork Functions bit.ly/cljintro
  26. conj assoc sort-by (sort-by :age [{:id 1 :age 21} {:id

    2 :age 18}]) => ’({:age 18, :id 2} {:age 21, :id 1}) Groundwork Functions bit.ly/cljintro
  27. conj assoc sort-by map (def xs [{:id 1 :age 21}

    {:id 2 :age 18}]) (map :id xs) => ’(1 2) (map #(+ 10 (:id %)) xs) => ’(11 12) Groundwork Functions bit.ly/cljintro
  28. conj assoc sort-by map reduce (reduce + [1 2 3

    4 5]) => 15 (defn condsum [acc x] (if (odd? x) (+ acc x) acc) (reduce condsum 0 [1 2 3 4 5]) => 9 Groundwork Functions bit.ly/cljintro
  29. conj assoc sort-by map reduce filter (filter odd? [1 2

    3 4 5]) => ’(1 3 5) (filter #(> 3 %) [1 2 3 4 5]) => ’(4 5) Groundwork Functions bit.ly/cljintro
  30. conj assoc sort-by map reduce filter for (for [x (range

    3) y (range 3 6)] (+ x y)) => ’(3 4 5 4 5 6 5 6 7) Groundwork Functions bit.ly/cljintro
  31. conj assoc sort-by map reduce filter for take lazy-seq partition

    concat Groundwork Functions Collection bit.ly/cljintro
  32. atom (let [a (atom 1) x @a] (swap! a +

    2) (not= @a x)) Groundwork Functions bit.ly/cljintro
  33. atom ref (let [r1 (ref 0), r2 (ref 100)] (dosync

    (alter r1 + 12) (alter r2 - 12))) Groundwork Functions bit.ly/cljintro
  34. atom ref agent (def ant (agent 100)) (defn damage [health

    damage] (- health damage)) (send ant damage 10) (= @ant 90) Groundwork Functions bit.ly/cljintro
  35. Create a new project: > lein new sugsa-prj Bootstrapping Time

    Add folder to workspace in LightTable Open core.clj, evaluate foo with ⌘㾑 Open Light Table console bit.ly/cljintro
  36. Bootstrapping Time add (defn -main [& args] (foo 1)) add

    :main sugsa-prj.core to project.clj add (:gen-class) to (ns sugsa-prj.core) > lein run bit.ly/cljintro Real Project
  37. Bootstrapping Time Web Dev: ring, compojure, enlive, hiccup Database: java.jdbc,

    monger http://www.clojure-toolbox.com/ https://clojars.org/ bit.ly/cljintro Libraries
  38. Reference Material Bootstrapping Time Books: Programming Clojure, Joy of Clojure

    Cheatsheet: http://clojure.org/cheatsheet Videos: http://www.infoq.com/clojure/ bit.ly/cljintro