24
Clojure )))) How not to be afraid of parentheses Dmytro Bignyak

Pivorak Clojure by Dmytro Bignyak

Embed Size (px)

Citation preview

Page 1: Pivorak Clojure by Dmytro Bignyak

Clojure ))))How not to be afraid of parentheses

Dmytro Bignyak

Page 2: Pivorak Clojure by Dmytro Bignyak

Me

Dmytro Bignyak

GitHub: deril Twitter: @derilok

Page 3: Pivorak Clojure by Dmytro Bignyak

It is not about

• in Ruby it takes 5 lines of code but in Clojure only 4

• I show you much code, how cool Clojure is

Page 4: Pivorak Clojure by Dmytro Bignyak

Clojure ≠ Closure

Page 5: Pivorak Clojure by Dmytro Bignyak

I ♥ Clojure

Page 6: Pivorak Clojure by Dmytro Bignyak

Is that easy?

(defn select "Returns a set of the elements for which pred is true" {:added "1.0"} [pred xset] (reduce (fn [s k] (if (pred k) s (disj s k))) xset xset))

Page 7: Pivorak Clojure by Dmytro Bignyak

Clojure is very powerful

Page 8: Pivorak Clojure by Dmytro Bignyak

• It is a functional language

• It has functional advantages

Page 9: Pivorak Clojure by Dmytro Bignyak

• Clojure can used as functional style

(map (partial * 10) [1 2 3]) ; => [10 20 30]

• It also can be used for structures

(defrecord Color [red green blue]) (def b (assoc a :alpha 0.1)) ; b => {:alpha 0.1, :red 0.5 :green 0.6, :blue 0.7}

Page 10: Pivorak Clojure by Dmytro Bignyak

Clojure is very useful when you want some

ASYNC

Page 11: Pivorak Clojure by Dmytro Bignyak

Clojure has 4 types of links

1. var (thread local)

2. atom (can be synchronically changed, but not coordinately)

3. agent (async state; analogue actor)

4. ref (can be synchronically and coordinately changed)

Page 12: Pivorak Clojure by Dmytro Bignyak

var

(def a 123) (println a) ; => 123

Page 13: Pivorak Clojure by Dmytro Bignyak

atom

(let [x (atom 0)] (println @x) ; => 0 (swap! x inc) (println @x)) ; => 1

Page 14: Pivorak Clojure by Dmytro Bignyak

agent

(def a (agent 0)) ; initial state

(send a inc) (println @a) ; => 1

(send a (fn [x] (Thread/sleep 100) (inc x))) (println @a) ; => 1

; in 100ms (println @a) ; => 2

Page 15: Pivorak Clojure by Dmytro Bignyak

• Agents change their state asynchronically

• In any time you can call deref and get the state of agent

Page 16: Pivorak Clojure by Dmytro Bignyak

Clojure can be used for MVCC (Multiversion Concurrency Control)(def account1 (ref 100) (def account2 (ref 0))

(dosync (alter account1 - 30) (alter account2 + 30))

(println @account1) ; => 70 (println @account2) ; => 30

(dosync (alter account1 * 0) (alter account2 / 0)) ; => ArithmeticException

; values weren’t changed (println @account1) ; => 70 (println @account2) ; => 30

Page 17: Pivorak Clojure by Dmytro Bignyak

I prefer core.async

Page 18: Pivorak Clojure by Dmytro Bignyak

Example core.async(defonce log-chan (chan))

(defn loop-worker [msg] (println msg))

(go-loop [] (let [msg (<! log-chan)] (loop-worker msg) (recur)))

Page 19: Pivorak Clojure by Dmytro Bignyak

Clojure (as all Lisps) is very powerful in metaprogramming

(defmacro unless [pred a b] `(if (not ~pred) ~a ~b))

(unless (> 1 10) (println "1 > 10. ok") (println "1 < 10. wat"))

Page 20: Pivorak Clojure by Dmytro Bignyak

Language is nothing without infrastructure

Page 21: Pivorak Clojure by Dmytro Bignyak

• Clojure uses JVM

• Eclipse, IDEA plugins

• Leiningen building tool

• Web frameworks (LuminusWeb, Noir)

• Async servers (https://github.com/ztellman/aleph)

Page 22: Pivorak Clojure by Dmytro Bignyak

ClojureScript (silver bullet for

JavaScript)

Page 23: Pivorak Clojure by Dmytro Bignyak

What’s next?

Page 24: Pivorak Clojure by Dmytro Bignyak

(println "Thank you!")