Skip to content

Instantly share code, notes, and snippets.

@seancorfield
Last active October 28, 2022 04:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seancorfield/6e8dd10799e9cc7527da5510c739e52f to your computer and use it in GitHub Desktop.
Save seancorfield/6e8dd10799e9cc7527da5510c739e52f to your computer and use it in GitHub Desktop.
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 
             {seancorfield/map-utils 
              {:git/url "https://gist.github.com/seancorfield/6e8dd10799e9cc7527da5510c739e52f"
               :sha "cfde3c2c83379e93ab1a49752611ae663008129f"}}}'

That starts a REPL:

user=> (require '[map-utils :refer [map-vals map-keys]])
nil
user=> (map-vals inc {:a 1 :b 2})
{:b 3, :a 2}
user=>
{: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