-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathconcurrency.clj
More file actions
43 lines (36 loc) · 952 Bytes
/
concurrency.clj
File metadata and controls
43 lines (36 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(ns examples.concurrency
(:use examples.chat))
; START: counter
(def counter (ref 0))
; END: counter
; START: next-counter
(defn next-counter [] (dosync (alter counter inc)))
; END: next-counter
; START: slow-double
(defn ^:dynamic slow-double [n]
(Thread/sleep 100)
(* n 2))
; END: slow-double
; START: calls-slow-double
(defn calls-slow-double []
(map slow-double [1 2 1 2 1 2]))
; END: calls-slow-double
; START: demo-memoize
(defn demo-memoize []
(time
(dorun
(binding [slow-double (memoize slow-double)]
(calls-slow-double)))))
; END: demo-memoize
; START: backup-agent
(def backup-agent (agent "output/messages-backup.clj"))
; END: backup-agent
; START: add-message-with-backup
(defn add-message-with-backup [msg]
(dosync
(let [snapshot (commute messages conj msg)]
(send-off backup-agent (fn [filename]
(spit filename snapshot)
filename))
snapshot)))
; END: add-message-with-backup