beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
2021-06-25T04:54:18.016300Z

Thanks for the help everyone!

2021-06-25T05:00:16.019400Z

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

indy 2021-06-25T05:04:00.019700Z

Try update-in and assoc-in

2021-06-25T05:22:31.019900Z

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

indy 2021-06-25T05:24:42.020200Z

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

2021-06-25T05:26:27.020500Z

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

2021-06-25T05:27:45.020700Z

I essentially want the end result to look like

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

indy 2021-06-25T05:33:08.021Z

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

2021-06-25T05:36:42.021200Z

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

1
Lukas 2021-06-25T09:13:23.022500Z

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

rickheere 2021-06-25T10:10:25.027300Z

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.

Nazral 2021-06-25T10:12:23.027400Z

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

dgb23 2021-06-25T10:12:24.027600Z

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 .

Osman Cea 2021-06-25T10:14:23.027800Z

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) 2021-06-25T10:19:20.028100Z

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)

rickheere 2021-06-25T10:28:36.028400Z

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

rickheere 2021-06-25T10:29:20.028600Z

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

Lukas 2021-06-25T10:30:16.028800Z

Hey, this is a good Idea. Thank you.

đź‘Ť 1
Nazral 2021-06-25T10:32:34.029Z

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

rickheere 2021-06-25T10:33:12.029200Z

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

dgb23 2021-06-25T10:33:24.029400Z

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.

đź‘Ť 1
rickheere 2021-06-25T10:35:08.029600Z

@archibald.pontier_clo sorry for that you code did work. Thanks

đź‘Ť 1
rickheere 2021-06-25T10:36:33.030Z

Thanks for the clarification @denis.baudinot

đź‘Ť 1
danielneal 2021-06-25T14:28:19.031300Z

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

danielneal 2021-06-25T14:28:42.031400Z

danielneal 2021-06-25T14:30:05.031700Z

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

danielneal 2021-06-25T14:30:11.031900Z

that’s nice

raspasov 2021-06-25T15:56:19.032Z

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