Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created December 4, 2015 18:51
Show Gist options
  • Save fzakaria/740c16e07fb3835d2285 to your computer and use it in GitHub Desktop.
Save fzakaria/740c16e07fb3835d2285 to your computer and use it in GitHub Desktop.
dissoc-in that handles vectors
(defn str->int [s]
(when-let [d (re-find #"-?\d+" s)] (Integer. d)))
(defn vec-remove
"remove elem in coll"
[coll pos]
(vec (concat (subvec coll 0 pos) (subvec coll (inc pos)))))
(defn dissoc-in
"The purpose of this method is to handle vector indices as well as maps."
[m [k & ks :as keys]]
(if ks
(if-let [nextmap (get m k)]
(let [newmap (dissoc-in nextmap ks)]
(assoc m k newmap))
m)
(cond
(vector? m) (vec-remove m (str->int k))
:else (dissoc m k) )
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment