Skip to content

Instantly share code, notes, and snippets.

@zentrope
Last active December 12, 2015 03:32
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 zentrope/9d09c91deab0351421e5 to your computer and use it in GitHub Desktop.
Save zentrope/9d09c91deab0351421e5 to your computer and use it in GitHub Desktop.
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