Skip to content

Instantly share code, notes, and snippets.

@gilbertw1
Created October 3, 2013 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbertw1/6810049 to your computer and use it in GitHub Desktop.
Save gilbertw1/6810049 to your computer and use it in GitHub Desktop.
Clojure Intro Lighttable Presentation
(ns clojintro.core
(:require [clojure.string :as str]))
;;;;;;;;;;;;;;;;
;; Primitives ;;
;;;;;;;;;;;;;;;;
5 ; => integer
25.0 ; => float
true ; => boolean
"clojure" ; => string
#"[a-zA-Z]+" ; => regular expression
:name ; => keyword
'variable ; => symbol
;;;;;;;;;;;;;;;;;;;;;
;; Data Structures ;;
;;;;;;;;;;;;;;;;;;;;;
;; lists
(list 1 2 3 4)
'(1 2 3 4)
(conj '(1 2 3 4) 5)
;; vectors
(vector 1 2 3 4)
[1 2 3 4]
(conj [1 2 3 4] 5)
;; maps
{:name "Bryan" :age 28}
(assoc {:name "Bryan" :age 28} :profession "engineer")
(conj {:name "Bryan" :age 28} [:profession "engineer"])
;; sets
#{1 2 3 4}
(conj #{1 2 3 4} 3 5 6)
;;;;;;;;;;;;;;;;
;; Assignment ;;
;;;;;;;;;;;;;;;;
;; global
(def x 1)
x
;; local let binding
(let [x 1
y 2]
(+ x y))
;;;;;;;;;;;;;;;
;; Functions ;;
;;;;;;;;;;;;;;;
;; invocation
;; (function arg1 arg2)
(+ 1 2)
;; definition
(defn plus-one [x]
(+ x 1))
;; anonymous
(fn [x] (+ x 1))
#(+ % 1)
;;;;;;;;;;;;;;;;;;
;; Conditionals ;;
;;;;;;;;;;;;;;;;;;
(= 1 2)
(= 1 1)
(and
(= 1 1)
(= 1 2))
(or
(= 1 1)
(= 1 2))
(if (even? 3)
"even"
"odd")
(when (even? 2)
"even")
;; if-not, when-not, if-let, when-let...
(do
(+ 1 2)
(+ 3 4)
(+ 5 6))
;;;;;;;;;;;
;; Loops ;;
;;;;;;;;;;;
;; loop over sequence
(doseq [x [1 2 3 4]]
(println x))
;; for comprehension (lazy!)
(for [x [1 2 3 4]]
(println x))
;; Higher order collection iteration
;;;;;;;;;;;;;;;;;;;;
;; Tail Recursion ;;
;;;;;;;;;;;;;;;;;;;;
;; recursion
(defn factorial [x]
(if (= x 1)
x
(* x (factorial (dec x)))))
;; loop / recur
(defn trfactorial [x]
(loop [curr x acc 1]
(if (= curr 1)
acc
(recur (dec curr) (* curr acc)))))
;; reduce
(defn rfactorial [x]
(reduce * (range 1 (inc x))))
;;;;;;;;;;;;;;;;;;;
;; Destructuring ;;
;;;;;;;;;;;;;;;;;;;
(def a-vec ["Clojure" "is" "really" "very" "Awesome!"])
(def a-map {:name "Bryan" :age 27 :is ["cool" "awesome" "great" "fantastic"]})
;; vector destructuring
(let [[a b] a-vec]
a)
(let [[a b] a-vec]
b)
(let [[a b & rest] a-vec]
rest)
;; map destructuring
(let [{name :name age :age} a-map]
name)
(let [{:keys [name age]} a-map]
name)
(let [{[a b & rest] :is} a-map]
rest)
;;;;;;;;;;;;;;;;;;
;; Java Interop ;;
;;;;;;;;;;;;;;;;;;
;; call system function
(System/currentTimeMillis)
;; call java method
(.toString (System/currentTimeMillis))
;; create new instance
(java.util.HashMap.)
;; chaining method calls
(.. (System/currentTimeMillis) toString length)
;; side effecting method calls
(doto (java.util.HashMap.)
(.put "HOME" "/home/me")
(.put "SRC" "src")
(.put "BIN" "classes"))
;;;;;;;;;;;;;;;;;;;;;;;;
;; Exception Handling ;;
;;;;;;;;;;;;;;;;;;;;;;;;
(try
(reduce * (range 1 1000))
(catch Exception e (str "caught: " e)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Collection Processing ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def bryan {:name "Bryan" :age 27})
(def billy {:name "Billy" :age 24})
(def steve {:name "Steve" :age 32})
(def people [bryan billy steve])
;; indexed access
(nth people 2)
;; key access
(get people 2)
(get bryan :name)
;; using keys as functions
(:name bryan)
;; using maps as functions
(bryan :name)
(map :name people)
(map :age people)
(reduce + (map :age people))
(filter #(> (:age %) 30) people)
;; using sets as functions
(def important-names #{"Bryan"})
(def peoples-names (map :name people))
(filter important-names peoples-names)
(filter (complement important-names) peoples-names)
(remove important-names peoples-names)
;; reduce
(reduce + 0 [1 2 3 4 5])
(reductions + 0 [1 2 3 4 4])
;;;;;;;;;;;;;;;;;;;;;
;; Reference Types ;;
;;;;;;;;;;;;;;;;;;;;;
;; Reference types - atom, ref, agent
;; atoms
(def shared-atom (atom {}))
@shared-atom
(deref shared-atom)
;; reset
(reset! shared-atom {})
;; swap
(swap! shared-atom assoc :name "bryan")
(swap! shared-atom assoc :age 28)
;; compare-and-set
(def old-val @shared-atom)
old-val
(compare-and-set! shared-atom old-val {:name "bryan"})
;; watcher
(add-watch shared-atom :hawk (fn [key shared old new]
(println "old: " old)
(println "new: " new)))
;; refs
(def language-favs (ref []))
(def votes (ref 0))
@language-favs
@votes
(dosync
(alter language-favs conj "clojure")
(alter votes inc))
(dosync
(ref-set language-favs (conj @language-favs "scala"))
(ref-set votes (inc @votes)))
;; agents
(def secret-agent (agent []))
@secret-agent
(defn to-the-store [inventory item]
(conj inventory item))
(send secret-agent to-the-store :milk)
(defn to-war [inventory]
(conj inventory :blood))
(send-off secret-agent to-war)
;;;;;;;;;;;;;;;;;
;; Parallelism ;;
;;;;;;;;;;;;;;;;;
;; pmap (parallel map)
(map inc [1 2 3 4])
(pmap inc [1 2 3 4])
;; future
(def some-time (future (map inc [1 2 3 4])))
@some-time
;;;;;;;;;;;;;;;;
;; Data Types ;;
;;;;;;;;;;;;;;;;
;; Records
(defrecord Person [name age])
(Person. "bryan" 28)
(:name (Person. "bryan" 28))
(keys (Person. "bryan" 28))
;; multimethods
(defmulti area :shape)
(defmethod area :rect [{:keys [h w]}]
(* h w))
(defmethod area :circle [{:keys [r]}]
(* Math/PI (* r r)))
(area {:shape :rect :h 10 :w 5})
(area {:shape :circle :r 5})
(defmulti name class)
(defmethod name Person [p]
(:name p))
(name (Person. "bryan" 28))
;; reify and proxy
(reify java.lang.Runnable
(run [this] (println "Hello")))
(proxy [Object] []
(toString []
"Hello World")
(hashCode []
"klsjk34jklj43kljfkldsjlk"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment