Last active
December 12, 2015 08:39
-
-
Save emasaka/4745302 to your computer and use it in GitHub Desktop.
LTSV parser by Clojure
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
;; 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." | |
[^java.io.BufferedReader 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 (clojure.java.io/reader filename)] | |
(doall (ltsv-parse-reader r)) )) | |
;; * example: get the sum of field "bar" where field "foo" is "ok" | |
;; (with-open [rdr (clojure.java.io/reader "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