-
-
Save whoahbot/6263777 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
(ns async-playground.core | |
(:require [clojure.core.async :as async | |
:refer [go chan >! <! >!! <!! timeout close! | |
thread alts! alts!! sliding-buffer]])) | |
(defn walk [t] | |
(cond (nil? t) | |
nil | |
:else | |
(let [[l v r] t] | |
(concat (walk l) [v] (walk r))))) | |
(defn awalk-blocking [t c] | |
(cond (nil? t) | |
nil | |
:else | |
(let [[l v r] t] | |
(do | |
(awalk-blocking l c) | |
(>!! c v) | |
(awalk-blocking r c))))) | |
(defn awalk-background [t c] | |
(cond (nil? t) | |
nil | |
:else | |
(let [[l v r] t] | |
(do | |
(awalk-background l c) | |
;; Moved the put into a go block here. | |
(go (>! c v)) | |
(awalk-background r c))))) | |
(def t [[nil 1 nil] 2 [[nil 4 nil] 3 nil]]) | |
(comment | |
;; create a channel | |
(def c (chan)) | |
;; run modified awalk-background | |
(awalk-background t c) | |
;; Take from the channel | |
(prn (<!! c)) ;; 4 | |
(prn (<!! c)) ;; 2 | |
(prn (<!! c)) ;; 1 | |
(prn (<!! c)) ;; 3 | |
(close! c) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment