Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Same Fringe with core.async
(defn same-fringe
"http://c2.com/cgi/wiki?SameFringeProblem"
[t1 t2]
(letfn [(walk [t c]
(go (if (seq? t)
(doseq [t' t]
(<! (walk t' c)))
(>! c t))))]
(let [c1 (chan)
c2 (chan)]
(go (<! (walk t1 c1)) (close! c1))
(go (<! (walk t2 c2)) (close! c2))
(<!!
(go (loop [x1 (<! c1)
x2 (<! c2)]
(cond
(and (nil? x1) (nil? x2)) true
(= x1 x2) (recur (<! c1) (<! c2))
:else false)))))))
(comment
(same-fringe '((0 1) 2 (3 (4 (5)))) '(0 (1 2) 3 4 ((((5))))))
(same-fringe '((0 1) 2 (3 (4 (5)))) '(0 (1 2) 3 :foo ((((5))))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.