Created
July 26, 2013 22:01
-
-
Save pron/6092559 to your computer and use it in GitHub Desktop.
Recursive go blocks with Pulsar
This file contains hidden or 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 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