Thanks for the help everyone!
Hey everyone, if I wish to have the value of a nested key within the map replaced, and return the entire map with the updated nested value, what are the best relevant core functions?
(map #(replace old-taxonomy->new (get-in % [:attributes :hobbies])) (slurp-blob))
The above returns a list of the replaced value instead of an entire map. Can't seem to figure out how I can retain the original map structure
Try update-in
and assoc-in
update-in
looks promising, thanks @kslvsunil! How do I pass in the current iteration's collection to replace
?
(update-in (slurp-blob) [:attrs :hobbies] (replace old-taxonomy->new <coll>))
Mind sharing how the value of :hobbies looks like and how you want it transformed?
It looks like {:hobbies ["Cycling", "Swimming"]}
old-taxonomy->new
is a map which looks like {"Cycling" "Cycle" "Swimming" "Swim"}
I essentially want the end result to look like
{:attrs {:id "1234" :hobbies ["Cycle" "Swim"]}}
(map (fn [m]
(update-in m [:attributes :hobbies] #(replace old-taxonomy->new %)))
(slurp-blob))
Learnt how to use update-in
and a new way of defining anonymous functions. Thanks @kslvsunil!
Hey, is it possible to model a spec "type" which shape depends on another spec? For example, to model an entity like this
(s/def ::entity
(s/keys
:req
[::number-a
::number-b-always-greater-than-number-a]))
I have a vector with a bunch of maps [{:s "aa"} {:s "bb"}]
how do I get {:s "bb"}
? I can do (first (filter #(=(:s %) "bb") [{:s "aa"} {:s "bb"}]))
. In javascript I would use Array.find but in clojure I can not find function like that. It could also be that I'm thinking in a not clojure way about this problem.
you could do (some #(when (= (:s %) "bb") %) l)
Also a beginner.
I would model it with s/and
in ::entity
with an inlined spec that checks for this dependency.
Why? because the dependency only makes sense in the context of ::entity
.
do you want to know if a map is part of the vector? or is it something more like a look up by id?
I think your`(first (filter...` approach is a good one. It can also be written without the anonymous function and less nesting as:
(->> [{:s "aa"} {:s "bb"}] (filter (comp #{"bb"} :s)) first)
@osman.cea Its more like an fine element by id kind of the. In this specific case I have a vector of maps with one of the maps not having the keyword :price I need to get the value that is in :asset
[{:asset "BTC", :free 0.01371726}
{:asset "BAT", :free 2.73 :price 0.002222}
{:asset "BNB", :free 1.48792745 :price 0.003333}]
@archibald.pontier_clo I played around with some but it did not give me what I wanted I think, I got the transformed value.
Hey, this is a good Idea. Thank you.
note that the when
form returns %
(the unchanged input), but it really is no different than filter
+ first
I think (because of lazyness)
That's a good suggestion @rupert also a nice trick with comp
using first
on filter
is an idiomatic way of doing this.
The expectation of using Array.prototype.find
is that it would terminate at the first truthy value and return the respective element.
In Clojure using first
on filter
does essentially the same thing, because filter
returns a lazy sequence. You are just looking at the first filtered element.
@archibald.pontier_clo sorry for that you code did work. Thanks
Thanks for the clarification @denis.baudinot
When I get an error (as opposed to a failure) in my tests, I get an “uncaught error not in assertion”. Is there anyway to get the (testing...
context reported when this happens
oh wait, I see the context has been highlighted in emacs
that’s nice
@rickheere Taking a step back, when such a situation arises, it’s good to evaluate whether there’s a better data structure that is more appropriate for the problem at hand; for example, Clojure supports map keys which can be any data structure themselves:
(let [m {{:s "aa"} {:x 42}
{:s "bb"} {:y 43}}]
(get m {:s "aa"}))
;=> {:x 42}
There are also sorted maps, sorted sets, etc. It all depends on the overall problem 🙂
For example, when dealing with UIs, often I’ve sometimes needed both a sorted order, and an ability to randomly lookup things by a key that’s not the index of a vector. Sorted maps work well in that case.