Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active November 6, 2021 15:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mfikes/a55943aa4533ed1a051023cebeed5d46 to your computer and use it in GitHub Desktop.
(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