Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created November 5, 2013 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiensonqin/7312721 to your computer and use it in GitHub Desktop.
Save tiensonqin/7312721 to your computer and use it in GitHub Desktop.
(def d (delay (println "Running>>>")
:done!))
(deref d)
(defn get-document
[id]
{:content (delay (slurp "http://www.baidu.com"))})
(get-document "some-id")
(def long-calculation (future (apply + (range 1e8))))
@long-calculation
(def f (future (Thread/sleep 10000) (println "done") 100))
@f
(defn call-service
[arg1 arg2 callback-fn]
; ...perform service call, eventually invoking callback-fn with results...
(future (callback-fn (+ arg1 arg2) (- arg1 arg2))))
(defn sync-fn
[async-fn]
(fn [& args]
(let [result (promise)]
(apply async-fn (conj (vec args) #(deliver result %&)))
@result)))
((sync-fn call-service) 8 7)
(conj (vec (list 1 2)) 3)
(defn future-addition
[arg1 arg2 callback-fn]
(future (callback-fn (+ arg1 arg2))))
(future-addition 2 3
(fn [n]
(printf "Async result: %s\n" n)))
(defn sync-fn
[async-fn]
(fn [& args]
(let [result (promise)]
(apply async-fn (conj (vec args) #(deliver result %)))
@result)))
(printf "Sync result: %s\n"
((sync-fn future-addition) 2 3))
(defn phone-numbers
[string]
(re-seq #"(\d{3})[\.-]?(\d{3})[\.-]?(\d{4})" string))
(phone-numbers " Sunil: 123.345.5678, Betty: 435.532.2345, Kevin: 456-4524523")
(def files (repeat 100000
(apply str
(concat (repeat 1000 \space)
"Sunil: 123.345.5678, Betty: 435.532.2345, Kevin: 456-4524523"))))
(time (dorun (map phone-numbers files)))
(time (dorun (pmap phone-numbers files)))
(time (->> files
(partition-all 250)
(pmap (fn [chunk] (doall (map phone-numbers chunk))))
(apply concat)
dorun))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment