Skip to content

Instantly share code, notes, and snippets.

@whoahbot
Forked from rasmusto/treewalk
Last active December 21, 2015 06:19
Show Gist options
  • Save whoahbot/6263777 to your computer and use it in GitHub Desktop.
Save whoahbot/6263777 to your computer and use it in GitHub Desktop.
(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