Created
August 29, 2012 00:21
-
-
Save orb/3505585 to your computer and use it in GitHub Desktop.
clojure light table demo code
This file contains 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
;; code from Austin Clojure Meetup light table live coding | |
;; implement union alogrithm from Sedgewick/Wayne class | |
;; -- https://class.coursera.org/algs4partI-2012-001/ | |
;; lecture notes week 1 - https://d19vezwu8eufl6.cloudfront.net/algs4partI/slides%2F15UnionFind.pdf | |
;; every state has a unique group id | |
(def no-connections | |
[0 1 2 3 4 5 6 7 8 9]) | |
;; two states are connected if they have the | |
;; same group id | |
(defn connected? [state i j] | |
(= (nth state i) | |
(nth state j))) | |
;; if the two states have different group ids, | |
;; move all the items in one group to the other | |
;; group | |
(defn union [state i j] | |
(let [igroup (nth state i) | |
jgroup (nth state j)] | |
(map #(if (= % jgroup) igroup %) state))) | |
;; first part of slide 4 | |
(def newstate1 | |
(-> no-connections | |
(union 4 3) | |
(union 3 8) | |
(union 6 5) | |
(union 9 4) | |
(union 2 1))) | |
(connected? newstate1 0 7) | |
(connected? newstate1 8 9) | |
;; second part of slide 4 | |
(def newstate2 | |
(-> newstate1 | |
(union 5 0) | |
(union 7 2) | |
(union 6 1) | |
(union 1 0))) | |
(connected? newstate2 0 7) | |
(connected? newstate2 8 9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment