Fork me on GitHub
#beginners
<
2021-06-25
>
Vk05:06:16

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

indy05:06:00

Try update-in and assoc-in

Vk05:06:31

update-in looks promising, thanks @UMPJRJU9E! How do I pass in the current iteration's collection to replace ? (update-in (slurp-blob) [:attrs :hobbies] (replace old-taxonomy->new <coll>))

indy05:06:42

Mind sharing how the value of :hobbies looks like and how you want it transformed?

Vk05:06:27

It looks like {:hobbies ["Cycling", "Swimming"]} old-taxonomy->new is a map which looks like {"Cycling" "Cycle" "Swimming" "Swim"}

Vk05:06:45

I essentially want the end result to look like

{:attrs {:id "1234" :hobbies ["Cycle" "Swim"]}}

indy05:06:08

(map (fn [m]
        (update-in m [:attributes :hobbies] #(replace old-taxonomy->new %)))
      (slurp-blob))

Vk05:06:42

Learnt how to use update-in and a new way of defining anonymous functions. Thanks @UMPJRJU9E!

2
Lukas09:06:23

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]))

dgb2310:06:24

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 .

Lukas10:06:16

Hey, this is a good Idea. Thank you.

👍 2
rickheere10:06:25

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.

Nazral10:06:23

you could do (some #(when (= (:s %) "bb") %) l)

Osman Cea10:06:23

do you want to know if a map is part of the vector? or is it something more like a look up by id?

Rupert (All Street)10:06:20

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)

rickheere10:06:36

@UT4TWF2NS 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}]

rickheere10:06:20

@UGH9KH1DF I played around with some but it did not give me what I wanted I think, I got the transformed value.

Nazral10:06:34

note that the when form returns % (the unchanged input), but it really is no different than filter + first I think (because of lazyness)

rickheere10:06:12

That's a good suggestion @UJVEQPAKS also a nice trick with comp

dgb2310:06:24

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.

👍 2
rickheere10:06:08

@UGH9KH1DF sorry for that you code did work. Thanks

👍 2
rickheere10:06:33

Thanks for the clarification @U01EFUL1A8M

👍 2
raspasov15:06:19

@U4XT72NNT 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.

danielneal14:06:19

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

danielneal14:06:05

oh wait, I see the context has been highlighted in emacs

danielneal14:06:11

that’s nice