mercredi 25 février 2015

Idiomatic string rotation in Clojure

How to idiomatically rotate a string in Clojure for the Burrows-Wheeler transform?


I came up with this, which uses (cycle "string"), but feels a bit imperative:



(let [s (str "^" "banana" "|")
l (count s)
c (cycle s)
m (map #(take l (drop % c)) (range l))]
(apply map str m))
=> ("^banana|" "banana|^" "anana|^b" "nana|^ba" "ana|^ban" "na|^bana" "a|^banan" "|^banana")


I'm not sure if this qualifies as code golf. Is there a cleaner way to do this?


Aucun commentaire:

Enregistrer un commentaire