Skip to content

Instantly share code, notes, and snippets.

@usametov
Created June 23, 2022 06:08
Show Gist options
  • Save usametov/63024f2a08c07086c2f9228412ce5244 to your computer and use it in GitHub Desktop.
Save usametov/63024f2a08c07086c2f9228412ce5244 to your computer and use it in GitHub Desktop.
babashka script to calculate total expenses
(require '[babashka.fs :as fs])
(require '[clojure.string :as s])
(require '[clojure.edn :as edn])
(require '[clojure.tools.cli :refer [parse-opts]])
(require '[clojure.pprint :as pp])
(def cli-options
[["-i" "--input-file FILE" "total expenses" :default "medical.txt"]
["-h" "--help" "each line in the input file should contain 'CAD' string, e.g. 'child-care-1500CAD.jpg' "]
["-v" nil "Verbosity level"
:id :verbosity ]])
(def opts (->> (parse-opts *command-line-args* cli-options)))
(def input-file
(or (->> opts :arguments first)
(->> opts :options :input-file)))
(when (->> opts :options :verbosity)
(pp/pprint (parse-opts *command-line-args* cli-options)))
(def amounts
(if (fs/exists? input-file)
(->> (fs/read-all-lines input-file)
(mapcat #(s/split % #" "))
(filter #(s/includes? % "CAD"))
(map #(s/split % #"CAD"))
(map first)
(map #(s/split % #"\-"))
(map last)
(map #(edn/read-string %)))
[]))
(prn "--------------amounts------------------" )
(pp/pprint amounts)
(prn "----------------------------total------------------")
(prn (reduce + amounts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment