Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created January 22, 2010 02:24
Show Gist options
  • Select an option

  • Save francoisdevlin/283450 to your computer and use it in GitHub Desktop.

Select an option

Save francoisdevlin/283450 to your computer and use it in GitHub Desktop.
(ns user)
(defn assoc-in
"Associates a value in a nested associative structure, where ks is a
sequence of keys and v is the new value and returns a new nested structure.
If any levels do not exist, hash-maps will be created."
[m [k & ks] v]
(if ks
(assoc m k (assoc-in (get m k) ks v))
(assoc m k v)))
(defn assoc-in-2
"Forces a sorted map"
[m [k & ks] v]
(if ks
(assoc m k (assoc-in-2 (get m k (sorted-map)) ks v))
(assoc m k v)))
(defn assoc-in-with
"supply a default-map"
[m default-map [k & ks] v]
(if ks
(assoc m k (assoc-in-as (get m k default-map) default-map ks v))
(assoc m k v)))
(defn assoc-in-empty
"Same type as top"
[m ks v]
(assoc-in-by m (empty m) ks v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment