Skip to content

Instantly share code, notes, and snippets.

@kaskichandrakant
Created July 8, 2018 05:35
Show Gist options
  • Save kaskichandrakant/888f7d055941886c704bb40b578d8399 to your computer and use it in GitHub Desktop.
Save kaskichandrakant/888f7d055941886c704bb40b578d8399 to your computer and use it in GitHub Desktop.
(refer 'clojure.set :only '[rename-keys])
(require '[clojure.string :as str])
; -------------------Creating the map---------------
(def person {:name "any" :age 12})
(:name person)
(:age person)
(def train {:engine "train Engine" :containers ["first" "second" "third"]})
(:containers train)
(def boy {:name "boys name" :age 10 :pet {:name "dog" :age 3}})
(:pet boy)
(:name (:pet boy))
; -------------renaming the key of map---------------------
; IMP -->> Everything is immutable by default
;value of person will remain same after changing as well
;so to use changed data of person need to create new variable
(:name person)
(rename-keys person {:name :person-name})
;value of person will remain same after changing as well
;so to use changed data of person need to create new variable
(def person-with-changed-key (rename-keys person {:name :person-name}))
;immutable so check the values of both i.e person and person-with-changed-key
; -------------changing the value of map---------------------
; ------(value as string and int)
; update (map-name :key fn)
(def boy-with-new-name (update boy :name (fn [name] str "new-Name")))
(def boy-with-new-age (update boy :age inc))
; ------(value as map)
; update-in (map-name [key inception sequence] fn)--like boy->pet->name = pet-name
(def boy-with-new-pet (update-in boy [:pet :name] (fn [name] str "new-pet-name") ))]
; ------(value as array)
(def train {:engine "train Engine" :containers [1 2 3]})
(:containers train)
(def new-train (update train :containers (map (fn [container] (str "new-" container)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment