Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pron
Created July 26, 2013 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pron/6092559 to your computer and use it in GitHub Desktop.
Save pron/6092559 to your computer and use it in GitHub Desktop.
Recursive go blocks with Pulsar
(ns co.paralleluniverse.pulsar.examples.walker
(:use [co.paralleluniverse.pulsar.core :exclude [close!]]
[co.paralleluniverse.pulsar.async]))
(defn walk [tree ch]
(letsfn [(walker [t]
(when t
(walker (:left t))
(>! ch (:value t))
(walker (:right t))))]
(go
(walker tree)
(close! ch))))
(defn same [t1 t2]
(let [ch1 (chan)
ch2 (chan)
drain #(loop [v (<!! %) res []]
(if v (recur (<!! %) (conj res v)) res))]
(walk t1 ch1)
(walk t2 ch2)
(= (drain ch1) (drain ch2))))
(defrecord Tree [left value right])
(def t1 (Tree.
(Tree.
(Tree. nil 1 nil)
1
(Tree. nil 2 nil))
3
(Tree.
(Tree. nil 5 nil)
8
(Tree. nil 13 nil))))
(def t2 (Tree.
(Tree.
(Tree.
(Tree. nil 1 nil)
1
(Tree. nil 2 nil))
3
(Tree. nil 5 nil))
8
(Tree. nil 13 nil)))
(defn -main []
(same t1 t2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment