Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Clojure
Search
Deon Moolman
October 22, 2013
Programming
1
580
Introduction to Clojure
A short introduction to Clojure, the principles, syntax and some function
Deon Moolman
October 22, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
640
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
2.5k
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
Grafana:建立系統全知視角的捷徑
blueswen
0
330
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
4
260
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
160
Architectural Extensions
denyspoltorak
0
280
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
400
AI時代の認知負荷との向き合い方
optfit
0
150
ThorVG Viewer In VS Code
nors
0
770
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
980
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
690
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.6k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
0
310
The SEO identity crisis: Don't let AI make you average
varn
0
67
The SEO Collaboration Effect
kristinabergwall1
0
350
Game over? The fight for quality and originality in the time of robots
wayneb77
1
110
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
450
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Designing Experiences People Love
moore
144
24k
Un-Boring Meetings
codingconduct
0
200
Transcript
Deon Moolman @CmdrDats Introduction to Clojure bit.ly/cljintro
Download Light Table http://www.lighttable.com/ bit.ly/cljintro
“Programming is not about typing. It’s about thinking” - Rich
Hickey bit.ly/cljintro
The Clojure Philosophy - Michael Fogus Simplicity Empowerment Focus What
is Clojure? bit.ly/cljintro
Functional What is Clojure? bit.ly/cljintro
Functional Concurrent What is Clojure? bit.ly/cljintro
Functional Concurrent Dynamic What is Clojure? bit.ly/cljintro
Functional Concurrent Dynamic Lisp What is Clojure? bit.ly/cljintro
Lisp Functional Concurrent Dynamic running on the JVM What is
Clojure? bit.ly/cljintro
“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
\c “string” 123 123.0 1/3 fred :name #”[0-9]+” nil true
Collections Basic Syntax? bit.ly/cljintro
(fred bob mary) [1 2 bob] {:name “bob”, “age” 12}
#{1 3 5 “mary” joe} Collections Basic Syntax? bit.ly/cljintro
Evaluation Reader Evaluator JVM Effect Source REPL bit.ly/cljintro
Switching Gears (order :spatula 200) (+ 100 (* 5 2)
5) - Some random Clojure code Basic Concepts bit.ly/cljintro
(def myvar 123) (def myname “Deon”) (def person {:name “Bob”
:age 12}) Basic Concepts bit.ly/cljintro Vars
(fn [x] (+ x 10)) (def inc-ten ) #(+ %
10) Basic Concepts bit.ly/cljintro Function definitions
(def inc-ten ) (fn [x] (+ x 10)) (defn inc-ten
[x] (+ x 10)) #(+ % 10) Basic Concepts bit.ly/cljintro Function definitions
(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
(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
(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
(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
“The four building blocks of the universe are fire, water,
gravel and vinyl.” - Dave Barry Groundwork Functions bit.ly/cljintro
Core Groundwork Functions bit.ly/cljintro
if (if x 1 2) (if (account-empty? account) (send-alert) (move-money))
Groundwork Functions bit.ly/cljintro
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
cond (cond (nil? name) “No name” (nil? age) “No age”
:else “All fine”) if let Groundwork Functions bit.ly/cljintro
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
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
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
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
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
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
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
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
if let loop recur and or cond apply complement juxt
partial memoize fnil identity Groundwork Functions Core bit.ly/cljintro
Collection Groundwork Functions bit.ly/cljintro
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
conj assoc (assoc {:name “Deon”} :age 21) => {:name “Deon”
:age 21} Groundwork Functions bit.ly/cljintro
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
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
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
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
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
conj assoc sort-by map reduce filter for take lazy-seq partition
concat Groundwork Functions Collection bit.ly/cljintro
Concurrency Groundwork Functions bit.ly/cljintro
atom (let [a (atom 1) x @a] (swap! a +
2) (not= @a x)) Groundwork Functions bit.ly/cljintro
atom ref (let [r1 (ref 0), r2 (ref 100)] (dosync
(alter r1 + 12) (alter r2 - 12))) Groundwork Functions bit.ly/cljintro
atom ref agent (def ant (agent 100)) (defn damage [health
damage] (- health damage)) (send ant damage 10) (= @ant 90) Groundwork Functions bit.ly/cljintro
atom ref agent future promise deliver await Groundwork Functions Concurrency
bit.ly/cljintro
Bootstrapping Time emacs Light Table CounterClockWise VimClojure Cursive Sublime Text
bit.ly/cljintro Editor Options
Install Leiningen: http://leiningen.org/ Bootstrapping Time Install Light Table: http://www.lighttable.com/ bit.ly/cljintro
Install
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
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
Bootstrapping Time Add maven [groupid/artifactid “1.2.3”] to :dependencies vector Open
project.clj bit.ly/cljintro Add Dependencies
Bootstrapping Time Web Dev: ring, compojure, enlive, hiccup Database: java.jdbc,
monger http://www.clojure-toolbox.com/ https://clojars.org/ bit.ly/cljintro Libraries
Bootstrapping Time http://www.4clojure.com/ http://clojurekoans.com/ http://tryclj.com/ bit.ly/cljintro Learning #clojure on irc.freenode.net
Reference Material Bootstrapping Time Books: Programming Clojure, Joy of Clojure
Cheatsheet: http://clojure.org/cheatsheet Videos: http://www.infoq.com/clojure/ bit.ly/cljintro
Thank you. #clojure.za on irc.freenode.net @CmdrDats @clj_ug_ct bit.ly/cljintro