Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active Nov 6, 2021
Embed
What would you like to do?
(require '[cljs-bean.core :refer [->clj ->js]])
(defn update-vals
"m f => {k (f v) ...}
Given a map m and a function f of 1-argument, returns a new map where the keys of m
are mapped to result of applying f to the corresponding values of m."
{:added "1.11"}
[m f]
(with-meta
(persistent!
(reduce-kv (fn [acc k v] (assoc! acc k (f v)))
(if (implements? IEditableCollection m)
(transient m)
(transient {}))
m))
(meta m)))
(clj->js (update-vals (js->clj #js {:a 1 :b 2 :c 3 :d 4}) inc))
;; => #js {:a 2, :b 3, :c 4, :d 5}
(->js (update-vals (->clj #js {:a 1 :b 2 :c 3 :d 4}) inc))
;; => #js {:a 2, :b 3, :c 4, :d 5} 3.2x faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment