Skip to content

Instantly share code, notes, and snippets.

Last active October 28, 2022 04:11
What would you like to do?
map-vals and map-keys

NOTE: Clojure 1.11 Alpha 2 added update-keys and update-vals which provide similar functionality directly in clojure.core, but with the hash map first and the function second.

Running this code:

clj -Sdeps '{:deps 
              {:git/url ""
               :sha "cfde3c2c83379e93ab1a49752611ae663008129f"}}}'

That starts a REPL:

user=> (require '[map-utils :refer [map-vals map-keys]])
user=> (map-vals inc {:a 1 :b 2})
{:b 3, :a 2}
{:paths ["."]}
(ns map-utils)
(defn map-vals [f m] (reduce-kv (fn [m k v] (assoc m k (f v))) {} m))
(defn map-keys [f m] (reduce-kv (fn [m k v] (assoc m (f k) v)) {} m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment