My solution look then like this :
(defn my-update-in [m k f]
(let [new_value (f (get m k))]
(assoc m k new_value)))
good enough or is there more that I can do with my little knowlegde about clojure
My solution look then like this :
(defn my-update-in [m k f]
(let [new_value (f (get m k))]
(assoc m k new_value)))
good enough or is there more that I can do with my little knowlegde about clojure
that looks more like an alternative to update
. update-in
would take a path, rather than a key. You can do a similar ting using assoc-in
. The actual update-in
is also variadic, but I guess you already know how to use apply
for those cases.
??
I looked at the clojure page and found this examples
(def p {:name "James" :age 26})
;;=> #'user/p
(update-in p [:age] inc)
;;=> {:name "James", :age 27}
so what are you meaning with a path ?
the point of update-in
is that you can use it with nested maps:
(update-in {:a {:b 1}} [:a :b] inc)
will yield {:a {:b 2}}
.
If you try your approach, you’ll get a null pointer exception. (you’re using [:a]
as the key, instead of a path to dig into.
hmm, then back to the drawing board I think.
Then I have to find a way to find the value of the right key and update that
so like this :
def m {:1 {:value 0, :active false}, :2 {:value 0, :active false}})
(update-in m [:1] assoc :value 1 :active true)
;;=>{:1 {:value 1, :active true}, :2 {:value 0, :active false}}
hmm, still doing something wrong here :
(defn my-update-in [m ks f args]
(assoc m ks (apply f (get m ks) args)))
(my-update-in p [:1] :age inc)
; Execution error (IllegalArgumentException) at chapter5/my-update-in (form-init95645667318631163.clj:51).
; Don't know how to create ISeq from: clojure.core$inc
I can peek to the source but I rather do it without because otherwise I have to feeling I do not learn anything
Think about each step your update-in has to perform:
The important piece of the ‚-in‘ variants of assoc/update is that they’re working with paths, not just 1 key, so step 1 and 3 are what you need to focus on
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.