clojure is hosted on JVM
clojure is lisp
clojure is functional programming language
clojure offer innovative solutions to the challenges inherent in concurrency and parallelization
clojure is a dynamic programming language
-
Obtaining Clojure
-
The Clojure REPL
R - Read : 입력되어진 코드를 읽는다. E - Eval : 코드를 평가하고, 결과를 도출해낸다. P - Print : 결과를 출력한다. L - Loop : read 단계로 돌아간다.
- 런타임 페널티가 없다.
- clojure의 REPL 동작방식을 이해한다면, Clojure 자체를 이해할 수 있다.
java -cp clojure-1.4.0.jar clojure.main Clojure 1.4.0 user=> ;; user가 namespace
def average (numbers):
return sum(numbers) / len(numbers)
(defn average [numbers]
(/ (apply + numbers) (count numbers)))
(average [60 80 100 400])
- 괄호
- 괄호가 Clojure를 homoiconic language로 만들어 준다.
- prefix
- syntax를 굉장히 간소화시켜준다.
- 표현식의 애매모호함 제거.
(operator parameter1 parameter2 parameter3 ...)
AST로 변형될 수 있는 syntax를 정의하는 대신, AST로 직접 표현될 수 있는 Clojure data structure로 Clojure 프로그램이 짜여졌다.
-
https://clojuredocs.org/clojure.core/read-string
string "hello there" booleans true / false nil null in Java Chracter \c \u00ff \o41 \space \newline \formfeed \return \backspace \tab
Keywords :keyword
Symbols (average [60 80 100 400])
numbers 41 long(64bit signed integer) 3.14 double(64-bit IEEE floating point decimal) 42N clojure.lang.BigInt 0.01M java.math.BigDecimal 22/7 clojure.lang.Ratio
0xff => 255 040 => 32 16rff => 255 arbitrary-precision numbers : 428p 참조
regular expressions (re-seq #"(...) (...)" "foo bar") ;= (["foo bar" "foo" "bar"])
comments ;; hello (comment (* 1 1)) #_ reader macro
Whitespace and comma (= [1 2 3] [1, 2, 3])
collection literals
'(a b :name 12.5) ;; list
['a 'b :name 12.5] ;; vector {:name "Chas" :age 31} ;; map #{1 2 3} ;; set
Miscellaneous Reader Sugar pass
Namespaces clojure에서의 namespace는 Java의 package와 같다. ns (def x 1) #'user/x
Symbol Evaluation #'average (average [60 80 100 400])
all of clojure is built on top of its special forms.
quote
(quote x)
(symbol? (quote x))
'x
do
(do
(println "hi")
(+ 1 1))
def
(def a (+ 1 1))
let
(let [x 1
y 2]
(+ x y))
destructuring
(def v [42 "foo" 99.2 [5 12]])
first
second
last
nth
(v 2)
(.get v 2) // java.util.List
(let [[x y z] v]
(+ x z))
;= 141.2
(def m {:a 10 :b 2})
(let [{a :a b :b} m]
(+ a b))
pass..
fn
(def my-inc (fn my-inc
[x] (+ x 1)))
(defn my-inc [x] (+ x 1))
if
(if true 1)
(if false 1 2)
recur
(loop [x 5]
(if (neg? x)
x
(recur (dec x))))
var
(def x 5)
(var x)
#'x
. | new
(new java.util.ArrayList 100)
(. Math pow 2 10)
try / throw
362page
set!
mutation of state
monitor-enter / monitor-exit
225page
(eval :foo)
(eval '(+ 1 2))
(eval (read-string "(+ 1 2)"))