Skip to content

Instantly share code, notes, and snippets.

Last active Dec 12, 2015
What would you like to do?
LTSV parser by Clojure
;; LTSV (Labeled Tab-separated Values) parser by Clojure
(use '[clojure.string :only [split]])
(defn ltsv-parse-line
"Parses one LTSV line. Returns a map."
[^String line]
(reduce (fn [r s] (let [[k v] (split s #":" 2)] (assoc r k v)))
{} (split line #"\t") ))
(defn ltsv-parse-reader
"Parses LTSV lines from reader. Returns lazy sequence of map."
[^ rdr]
(map ltsv-parse-line (line-seq rdr)) )
(defn ltsv-parse-file
"Parses whole LTSV text file. Returns sequence of maps."
[^String filename]
(with-open [r ( filename)]
(doall (ltsv-parse-reader r)) ))
;; * example: get the sum of field "bar" where field "foo" is "ok"
;; (with-open [rdr ( "sample.log")]
;; (reduce (fn [r x]
;; (if (= (x "foo") "ok") (+ r (Integer/parseInt (x "bar"))) r) )
;; 0 (ltsv-parse-reader rdr) ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment