Skip to content

Instantly share code, notes, and snippets.

@rasmusto
Created August 16, 2013 18:11
Show Gist options
  • Save rasmusto/6252155 to your computer and use it in GitHub Desktop.
Save rasmusto/6252155 to your computer and use it in GitHub Desktop.
(ns clj-async-tests.core
(:require [clojure.core.async :as async
:refer [go chan >! <! >!! <!! timeout close! thread alts! alts!!]]))
(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]
;; TODO implementation here is tripping me up probably
(cond (nil? t)
nil
:else
(let [[l v r] t]
(do
(awalk-background l c)
(>! c v)
(awalk-background r c)))))
(def t [[nil 1 nil] 2 [[nil 4 nil] 3 nil]])
(comment
; no channel
(prn (walk t))
; blocking reads
(let [c (chan)]
(thread (awalk-blocking t c))
(prn (<!! c))
(prn (<!! c))
(prn (<!! c))
(prn (<!! c)))
; thread + alts
(let [c (chan)]
(thread (awalk-blocking t c))
(thread (while true
(let [[v ch] (alts!! [c])]
(println "Read" v "from" ch))))
nil)
; FIXME doesn't work
(let [c (chan)
newc (go (awalk-background t c))]
(<!! (go (<! newc)))
(close! newc)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment