Skip to content

Instantly share code, notes, and snippets.

@zentrope zentrope/get-bars.clj
Last active Dec 12, 2015

Embed
What would you like to do?
Stringing out XML blobs
#!/usr/bin/env clj
(defn get-bars
[xml]
(loop [xml xml
bars []]
(if (clojure.string/blank? xml)
bars
(let [[_ bar xml2 :as r] (clojure.string/split xml #"<bar>|</bar>" 3)]
(if (nil? bar)
bars
(recur xml2 (conj bars bar)))))))
(defn lazy-bars
[xml]
(letfn [(ex [xml]
(let [[_ bar xml2 :as r] (clojure.string/split xml #"<bar>|</bar>" 3)]
[bar xml2]))]
(let [[bar xml2] (ex xml)]
(if (nil? bar)
nil
(cons bar (lazy-seq (ex xml2)))))))
(prn (get-bars "<foo><baz><bar>a</bar><bar>B</bar></baz></foo>"))
(prn (lazy-bars "<foo><baz><bar>a</bar><bar>B</bar></baz></foo>"))
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.