specter

2024-02-01T19:33:57.768969Z

what's a better way to write this using paths? basically trying to sum the values taking into account nil.

(def data {:a [{:val 1} {:val 2}]
           :b [{:val 4} {:val nil}]
           :c [{:val 7} {:val 8}]})

(sp/transform [sp/MAP-VALS] #(->> % (map :val) (filter number?) (apply +)) data)
=> {:a 3, :b 4, :c 15}

nathanmarz 2024-02-01T19:58:25.962919Z

this is more efficient (transform [MAP-VALS (view #(select [ALL :val some?] %))] #(reduce + 0 %) data)

🙏 1