Created
February 24, 2015 14:42
-
-
Save kierendavies/180c2ce42a6fdf859e5c to your computer and use it in GitHub Desktop.
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
; Burrows-Wheeler Transform (and inverse) | |
; http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform | |
(def string-start \u0098) | |
(defn bwt [s] | |
(map last | |
(sort-by vec | |
(take (inc (count s)) | |
(iterate #(concat (rest %) | |
(list (first %))) | |
(cons string-start s)))))) | |
(defn inv-bwt [s] | |
(rest | |
(first | |
(filter #(= (first %) string-start) | |
(nth (iterate #(map cons s (sort-by vec %)) | |
(map list s)) | |
(dec (count s))))))) | |
(let [s "'Twas brillig, and the slithy toves | |
Did gyre and gimble in the wabe: | |
All mimsy were the borogoves, | |
And the mome raths outgrabe."] | |
(println (apply str (bwt s))) | |
(println (apply str (inv-bwt (bwt s))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My attempt: