Created
December 13, 2017 05:49
-
-
Save saumitra2810/6793ef272f1f5d65190e14cc50b5f8fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
; 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