Skip to content

Instantly share code, notes, and snippets.

@michalmarczyk
Last active December 19, 2015 10:09
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 michalmarczyk/5938333 to your computer and use it in GitHub Desktop.
Save michalmarczyk/5938333 to your computer and use it in GitHub Desktop.
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