Skip to content

Instantly share code, notes, and snippets.

@varfrog
Last active August 2, 2021 22:23
Show Gist options
  • Save varfrog/9f1494972a1dcc40aec77e8eeae98342 to your computer and use it in GitHub Desktop.
Save varfrog/9f1494972a1dcc40aec77e8eeae98342 to your computer and use it in GitHub Desktop.
Transformation demo
(defrecord BalanceSheet [liabilities equity])
(defn calc-assets
[^BalanceSheet bs]
(+ (:liabilities bs) (:equity bs)))
(defn replace-vals-with-assets
"{:key1 BalanceSheet, ...} -> {:key1 (calc-assets BalanceSheet), ...}."
[sheets]
(reduce-kv (fn [m k v]
(assoc m k (calc-assets v))) {} sheets))
(let [sheets {:1950 (->BalanceSheet 7000 15000)
:1951 (->BalanceSheet 14000 100000)
:1953 (->BalanceSheet 45000 350000)}]
(-> sheets replace-vals-with-assets vals))
;; First, the call to (replace-vals-with-assets sheets) takes place and a new map is returned:
;; {:1950 22000, :1951 114000, :1953 395000}
;; Then a list of its values is made by calling (vals {:1950 22000, :1951 114000, :1953 395000}):
;; (22000 114000 395000).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment