Created
December 4, 2015 18:51
-
-
Save fzakaria/740c16e07fb3835d2285 to your computer and use it in GitHub Desktop.
dissoc-in that handles vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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