Skip to content

Instantly share code, notes, and snippets.

@velppa
Created August 5, 2014 05:38
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 velppa/b57f0484d2d7e2903c91 to your computer and use it in GitHub Desktop.
Save velppa/b57f0484d2d7e2903c91 to your computer and use it in GitHub Desktop.
(ns ledger-reports.core
(:gen-class))
(require '[clojure.data.csv :as csv]
'[clojure.java.io :as io])
(defn read-csv
[filename]
(def csv
(with-open [in-file (io/reader filename)]
(doall
(map (partial zipmap [:category :value]) (csv/read-csv in-file)))))
(def replace-newline #(clojure.string/replace % #"\n" " + "))
(map #(update-in % [:value] replace-newline) csv)
)
(defn write-csv
[input]
(let [filename (first input)
data (second input)
sorted-data (sort (map vec data))]
(println "i'm writing csv into" filename "file")
(with-open [out-file (io/writer filename)]
(csv/write-csv out-file sorted-data))))
(defn all-categories
"Get seq of csv, return set of categories"
[csvs]
(set (apply concat (map #(map :category %) x)))
#_(sort (set (apply concat (map #(map first %) csvs)))))
(defn search-for-category
"Search for expenses in given category"
[category csv]
(or (first (filter #(= (:category %) category) csv))
{:category category :value nil}))
(defn search-in-csv
"Search for all categories in given file"
[result csv]
(map #(search-for-category % csv) categories))
(defn write-files
"Processes given files"
[filenames]
(let [csvs (zipmap filenames (map read-csv filenames))
;csvs (map read-csv filenames)
#_(categories (map #(hash-map :name %) (all-categories (vals csvs))))
]
csvs
)
#_(let [csvs (zipmap filenames (map read-wo-newlines filenames))
categories-map (all-categories (vals csvs))
out (zipmap (map #(str % ".out") (keys csvs))
(map #(search-in-csv cats %) (vals csvs)))]
#_(println "out")
#_(println out)
#_(map write-csv (seq out))
#_(println cats))
)
(def filenames ["/Users/rbd/Documents/ledger/Jun_2014.txt"
"/Users/rbd/Documents/ledger/Jul_2014.txt"])
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, another World!")
#_(println args)
(apply write-files args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment