This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-04-06
Channels
- # announcements (14)
- # babashka (14)
- # beginners (22)
- # calva (56)
- # cider (20)
- # clerk (8)
- # clj-commons (10)
- # clj-kondo (18)
- # cljs-dev (11)
- # clojure (87)
- # clojure-conj (3)
- # clojure-europe (29)
- # clojure-nl (1)
- # clojure-poland (5)
- # clojure-portugal (1)
- # clojurescript (100)
- # data-science (3)
- # datahike (1)
- # datomic (13)
- # events (2)
- # fulcro (10)
- # funcool (2)
- # helix (19)
- # hoplon (6)
- # humbleui (2)
- # hyperfiddle (40)
- # leiningen (5)
- # lsp (22)
- # malli (26)
- # nrepl (2)
- # off-topic (19)
- # reagent (32)
- # releases (1)
- # shadow-cljs (266)
- # spacemacs (6)
- # tools-build (9)
- # vim (1)
I routinely need to put big maps toghether to send to web services, and I have yet to find a satisfying way to achieve the following: (defn some-fn [{:keys opt1 opt2 opt3 req1 req2 req3}])
Since that isn't legal Clojure syntax, can you provide a bit more detail about what you're trying to achieve and a more concrete example?
Yeah sorry I hit enter too soon and am in middle of adding the rest 🙂 stupid return vs. shift+return 🙂
So a fn like this:
(defn ex [{:keys [req1 req2 req3 opt1 opt2 opt3]}]
(let [result {:req1 req1 :req2 req2 :req3 req3}
result (if (some? opt1) (conj result {:opt1 opt1}))
result (if (some? opt2) (conj result {:opt2 opt2}))
result (if (some? opt3) (conj result {:opt3 opt3}))]
result
))
There are a bunch of approaches that look promising, but I can’t decide between them all.If it were only as simple as having a backend service that was cool with :opt1 nil .. but no such luck.
(defn ex [{:keys [opt1 opt2 opt3] :as data}]
(cond-> (select-keys data [:req1 :req2 :req3])
(some? opt1) (assoc :opt1 opt1)
(some? opt2) (assoc :opt2 opt2)
(some? opt3) (assoc :opt3 opt3)))
Is that more appealing?If optN
is not boolean, you could just use optN
instead of (some? optN)
in each case.
If you're open to a lib, https://weavejester.github.io/medley/medley.core.html#var-assoc-some could be worth a look
If you have a lot of optional keys, you could reduce
over them and conditional assoc
in each non-`nil` value from data
.
(defn ex [data]
(reduce (fn [result k] (if-some [v (get data k)] (assoc result k v) result))
(select-keys data required-keys)
optional-keys))
medley looks like its packed full of a couple things I’ve crudely done in my util ns 🙂 thanks for the pointer
I need to think to use reduce more! I do end up with a lot of optional keys for this particular problem
Can't recall how many times I added assoc-some to my project. Sounds like a super common use case, would be awesome if it could be on clojure.core
assoc-some
's effect can be delayed, if you are ok filtering out nil values from your return object. sometimes that's not a good option. cond->
acts are a good alternative, and if you go to the extreme, you may be interested in a schema based solution or something like pathom (rule engine solution). it all depends how much of a pain in the ass this stuff is in your codebase
filtering out nil values is almost never an option in my experience, because you don't really know if that key is nil
because it shouldn't exist, or if it is nil
because nil
is the actual value you want for that key
@U0LAJQLQ1 ooh good food for thought.. I believe schemas will be coming to this codebase sooner or later, just a bit of analysis paralysis between Schema, Malli, Spec, for doing that sort of thing
And I usually use assoc-some
when I'm constructing some data to match a certain schema with optional keys. I still use it very often even using schema libs.
Yeah and payload-wise its a drop-in-the-bucket compared to the material design icon library’s giant collection of svg path data (can’t really tree-shake it either since the icons we’re using arem’t predictable
> Yeah sorry I hit enter too soon and am in middle of adding the rest 🙂 stupid return vs. shift+return 🙂
Click <user icon in top-right corner>
--> Preferences
--> Advanced
. You should find a setting that reads:
> When writing a message, press Enter
to…
> ● Send the message
> ⚬ Start a new line (use Ctrl
Enter
to send)
Click on the second one to swap the active radio button. Then click the 𝖷
or press Esc
. You should be free of this frustrating default behavior.