Contents
1. Clojure Quick Intro
2. Leiningen
3. Clojure REPL
4. Lisp Editing
5. REPL-driven Development in Practice
Slide 4
Slide 4 text
Clojure Quick Intro
Slide 5
Slide 5 text
Clojure
Lisp
S-expressions, macros, etc.
REPL-driven development
functional programming language
dynamic language
JVM language (cf. ClojureScript)
⇒ simple and powerful language
Slide 6
Slide 6 text
literals
type example
string "abc"
character \a
number 1, 2.0, 3N, 4.5M, 6/7, 8r10
boolean true, false
nil nil
keyword :a, :user/a, ::a, ::x/a
symbol 'a, 'user/a, `a, `x/a
Slide 7
Slide 7 text
type example
list '(1 2 3), '(+ 1 2 3)
vector [1 2 3]
set #{1 2 3}
map {:a 1 :b 2}, #:user{:a 1 :b 2},
#::{:a 1 :b 2}, #::x{:a 1 :b 2}
function (fn [x] (* x x))
Slide 8
Slide 8 text
the syntax
operator
function
macro
special form
(op arg1 arg2 ... argn)
Slide 9
Slide 9 text
Leiningen
Slide 10
Slide 10 text
lein repl
starts Clojure REPL
$ lein repl
nREPL server started on port 49482 on host 127.0.0.1 - nrepl://1
27.0.0.1:49482
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_121-b13
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=>
Slide 11
Slide 11 text
lein new
generates a new Clojure project
generate a project with app template
cf.
$ lein new app clj-demo
Generating a project called clj-demo based on the 'app' template
lein-template
clojure.repl/doc
prints documentation
clj-demo.core=> (doc map)
-------------------------
clojure.core/map
([f] [f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])
Returns a lazy sequence consisting of the result of applying f to
the set of first items of each coll, followed by applying f to the
set of second items in each coll, until any one of the colls is
exhausted. Any remaining items in other colls are ignored. Function
f should accept number-of-colls arguments. Returns a transducer when
no collection is provided.
nil
Slide 21
Slide 21 text
clojure.repl/source
prints source code
clj-demo.core=> (source map)
(defn map
"Returns a lazy sequence consisting of the result of applying f to
the set of first items of each coll, followed by applying f to the
set of second items in each coll, until any one of the colls is
exhausted. Any remaining items in other colls are ignored. Function
f should accept number-of-colls arguments. Returns a transducer when
no collection is provided."
{:added "1.0"
:static true}
([f]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(rf result (f input)))
([result input & inputs]
(rf result (apply f input inputs))))))
Slide 22
Slide 22 text
clojure.core/*1, *2, *3
special vars bound to the most recent values
printed
clj-demo.core=> 21
21
clj-demo.core=> 2
2
clj-demo.core=> (* *2 *1)
42
Slide 23
Slide 23 text
clojure.core/*e
special var bound to the most recent exception
caught by the REPL
clj-demo.core=> (+ 1 "2")
ClassCastException java.lang.String cannot be cast to java.lang.Number
clj-demo.core=> *e
#error {
:cause "java.lang.String cannot be cast to java.lang.Number"
:via
[{:type java.lang.ClassCastException
:message "java.lang.String cannot be cast to java.lang.Number"
:at [clojure.lang.Numbers add "Numbers.java" 128]}]
:trace
[[clojure.lang.Numbers add "Numbers.java" 128]
[clojure.lang.Numbers add "Numbers.java" 3640]
[clj_demo.core$eval1328 invokeStatic "form-init3161334546666357405.cl
[clj_demo.core$eval1328 invoke "form-init3161334546666357405.clj" 1]
[clojure.lang.Compiler eval "Compiler.java" 6927]
[clojure.lang.Compiler eval "Compiler.java" 6890]
[clojure.core$eval invokeStatic "core.clj" 3105]
Run koans
$ cd clojure-koans/
$ lein koan run
Starting auto-runner...
Considering /Users/k.ohashi/code/clojure-koans/src/koans/01_equa
lities.clj...
Now meditate upon /Users/k.ohashi/code/clojure-koans/src/koans/0
1_equalities.clj
---------------------
Assertion failed!
clojure.lang.ExceptionInfo: We shall contemplate truth by testin
g reality, via equality
(= __ true) {:line 6}, compiling:(/Users/k.ohashi/code/clojure-k
oans/src/koans/01_equalities.clj:4:1)
Slide 66
Slide 66 text
Open a source le with your favourite editor
$ emacs src/koans/01_equalities.clj
Slide 67
Slide 67 text
Start Clojure REPL
Slide 68
Slide 68 text
Evaluate S-expressions
Slide 69
Slide 69 text
No content
Slide 70
Slide 70 text
No content
Slide 71
Slide 71 text
Send S-expressions to REPL
Slide 72
Slide 72 text
No content
Slide 73
Slide 73 text
No content
Slide 74
Slide 74 text
No content
Slide 75
Slide 75 text
No content
Slide 76
Slide 76 text
No content
Slide 77
Slide 77 text
Save changes and check the meditation result
...
Now meditate upon /Users/k.ohashi/code/clojure-koans/src/koans/0
1_equalities.clj
---------------------
Assertion failed!
clojure.lang.ExceptionInfo: You can test equality of many things
(= (+ 3 4) 7 (+ 2 __)) {:line 12}, compiling:(/Users/k.ohashi/co
de/clojure-koans/src/koans/01_equalities.clj:4:1)
Slide 78
Slide 78 text
4Clojure
Slide 79
Slide 79 text
Open a new buffer with your favourite editor
$ emacs clj-demo/src/clj_demo/4clojure/problem22.clj
Slide 80
Slide 80 text
Start Clojure REPL
Slide 81
Slide 81 text
e.g.
Count a Sequence
Problem #22
Write a function which returns the
total number of elements in a
sequence.
(= (__ '(1 2 3 3 1)) 5)
(= (__ "Hello World") 11)
(= (__ [[1 2] [3 4] [5 6]]) 3)
(= (__ '(13)) 1)
(= (__ '(:a :b :c)) 3)
Special Restrictions: count
Slide 82
Slide 82 text
Write a function
Slide 83
Slide 83 text
Evaluate S-expressions
Slide 84
Slide 84 text
No content
Slide 85
Slide 85 text
No content
Slide 86
Slide 86 text
No content
Slide 87
Slide 87 text
No content
Slide 88
Slide 88 text
No content
Slide 89
Slide 89 text
Send S-expressions to REPL
Slide 90
Slide 90 text
No content
Slide 91
Slide 91 text
No content
Slide 92
Slide 92 text
No content
Slide 93
Slide 93 text
No content
Slide 94
Slide 94 text
No content
Slide 95
Slide 95 text
No content
Slide 96
Slide 96 text
Run your solution on the problem page
(fn my-count [coll]
(reduce (fn [n _] (inc n))
0
coll))