Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new key-value pair to struct-map in clojure

Tags:

clojure

I have struct map like this

(def admin (struct-map person :first-name "Name" :last-name "Last name"))

So, now I would like to add new key-value pair to this map, and have it look something like

(def admin (struct-map person :first-name "Name" :last-name "Last name" :username "username"))

How do I do this?

I know that it won't be the same structure after this, but it doesn't matter.

like image 878
Vuk Stanković Avatar asked Sep 19 '13 07:09

Vuk Stanković


1 Answers

If you are not concerned with retaining the structure, assoc will add your new key-value pair.

(defstruct person :first-name :last-name)

(def admin (struct-map person 
             :first-name "Name" 
             :last-name "Last name"))

(assoc admin :username "username")
  ;=> {:first-name "Name", :last-name "Last name", :username "username"}
like image 88
Jared314 Avatar answered Nov 04 '22 21:11

Jared314