Fork me on GitHub
#specter
<
2024-02-01
>
wei19:02:57

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}

nathanmarz19:02:25

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

🙏 1