Last active
March 14, 2016 23:05
-
-
Save nha/2003f3a5b738e72cb75e 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
;; MUST be possible | |
;; http://funcool.github.io/cats/latest/#alet | |
;; https://gist.github.com/aclemmensen/11272381 | |
;; http://www.lispcast.com/core-async-browser-motivation | |
;; https://gist.github.com/aclemmensen/11272381 | |
(defn sample-async-fn [t s] | |
(println "sample-async-fn" t s js/Date.) | |
(go (<! (timeout t)) | |
s)) | |
(defn sample-async-fn2 [t s] | |
(let [ch (chan)] | |
(js/setTimeout (fn [] | |
(put! ch s)) t) | |
ch)) | |
;; MUST be possible | |
;; http://funcool.github.io/cats/latest/#alet | |
;; https://gist.github.com/aclemmensen/11272381 | |
(defn test-core-async-parallel-exec [] | |
(println "BEFORE GO-LOOP") | |
(let [before (.getTime (js/Date.))] | |
(go-loop [options {:a "replace-me" | |
:b "replace-me"}] | |
;; how can I run this in parallel? | |
(cond | |
(= (:a options) "replace-me") | |
(recur (assoc options :a (<! (sample-async-fn 1000 "ok")))) | |
(= (:b options) "replace-me") | |
(recur (assoc options :b (<! (sample-async-fn 2000 "ok")))) | |
;; all set - exit | |
;; now all the values are filled in | |
:default | |
(js/console.log "DONE " (- (.getTime (js/Date.)) before))))));; roughly 1000 + 2000 | |
(defn test-core-async-parallel-exec2 [] | |
(println "BEFORE GO-LOOP") | |
(let [before (.getTime (js/Date.))] | |
(go | |
(println "IN GO BLOCK") | |
(println "DONE" | |
{:a (<! (sample-async-fn2 1000 "ok")) | |
:b (<! (sample-async-fn2 2000 "ok"))} | |
(- (.getTime (js/Date.)) before) )))) ;; 3000 too | |
;;(test-core-async-parallel-exec2) | |
(defn test-core-async-parallel-exec3 [] | |
(println "BEFORE GO-LOOP") | |
(let [before (.getTime (js/Date.)) | |
options {:a (sample-async-fn 1000 "ok") | |
:b (sample-async-fn 2000 "ok")}] | |
(go | |
(println "IN GO BLOCK") | |
(println "DONE" (-> pending | |
(update :b #(take! %)) | |
(update :a #(take! %))))))) | |
(test-core-async-parallel-exec3) | |
;; (println "TEST CORE ASYNC") | |
;; (test-core-async-parallel-exec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment