Skip to content

Instantly share code, notes, and snippets.

@saumitra2810
Created December 13, 2017 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saumitra2810/6793ef272f1f5d65190e14cc50b5f8fe to your computer and use it in GitHub Desktop.
Save saumitra2810/6793ef272f1f5d65190e14cc50b5f8fe to your computer and use it in GitHub Desktop.
; SOLUTION 1
(defn change-title-if-not-in-ignore-list [book changed-title ignore-list]
(let [in-ignore-list (some #(= (:serial-no book) %) ignore-list)]
(if (not in-ignore-list)
(assoc book :title changed-title)
book)))
(defn change-title [books changed-title ignore-list]
(let [books-vec (:books books)]
(->>
books-vec
(mapv #(change-title-if-not-in-ignore-list % changed-title ignore-list))
(hash-map :books)
)))
(change-title books-store "Sam" [12])
; SOLUTION 2
(defn change-title
[dict title toIgnore]
(def l (:books dict))
(hash-map
:books
(reduce
(fn
[ans current]
(if (= false (contains? (set toIgnore) (:serial-no current)) )
(conj ans (update current :title (fn [x] title)))
(conj ans current)
)
)
[]
l
)
)
)
(def books-store
{:books [{:serial-no 12 :title "ABC" :author "Test1"},
{:serial-no 13 :title "ABD" :author "Test2"},
{:serial-no 14 :title "ABE" :author "Test3"}]
}
)
(println (change-title books-store "Sam" [12]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment