Created
November 5, 2013 02:14
-
-
Save tiensonqin/7312721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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