This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-11-20
Channels
- # babashka (56)
- # beginners (151)
- # calva (3)
- # cider (31)
- # clj-kondo (17)
- # cljs-dev (5)
- # clojure (26)
- # clojure-australia (21)
- # clojure-dev (7)
- # clojure-europe (23)
- # clojure-nl (5)
- # clojure-spec (54)
- # clojure-uk (11)
- # clojuredesign-podcast (8)
- # clojurescript (77)
- # community-development (10)
- # core-typed (1)
- # cursive (3)
- # datomic (7)
- # docker (67)
- # emacs (10)
- # expound (6)
- # figwheel-main (3)
- # graalvm (67)
- # helix (10)
- # java (13)
- # jobs (6)
- # kaocha (4)
- # leiningen (15)
- # malli (2)
- # meander (31)
- # off-topic (40)
- # pedestal (9)
- # rdf (7)
- # reagent (5)
- # reitit (9)
- # remote-jobs (5)
- # shadow-cljs (94)
- # sql (7)
- # testing (12)
- # tools-deps (75)
- # vim (13)
How would you write a function that does this: based on some parameters, it produces a map. IF one of the parameters is non-nil, THEN the map needs to contain an extra key/value pair based on that parameter. I can do something like this:
(defn myfun [a b]
(let [result {:a a
:foo (foo-fun a b)
:bar (bar-fun a b}]
(if b
(assoc result :b (extra-fun b))
result)))
or:
(defn myfun [a b]
(merge {:a a
:foo (foo-fun a b)
:bar (bar-fun a b}
(when b {:b (extra-fun b)})
or:
(defn myfun [a b]
(let [result {:a a
:foo (foo-fun a b)
:bar (bar-fun a b}
result (when b (assoc result :b (extra-fun b)))]
result))
And I could probably come up with half a dozen more ways to write it. How would you write it?!I'd go with option b personally, not sure if there are better ways of writing it
cond->
might be an option as well
(defn myfun [a b]
(cond-> {:a a
:foo (foo-fun a b)
:bar (bar-fun a b)}
b (assoc :b (extra-fun b))))
@stefan.van.den.oord The last alternative returns nil if b is false, in contrast to the first two. (Note that you're testing against truthiness, not against nil, but I assume b isn't a bool.)
I would also go with the second alternative. But, does it matter if :b
is present and nil
?
So,
(defn myfun [a b]
{:a a
:foo (foo-fun a b)
:bar (bar-fun a b)
:b (when b
(extra-fun b))})
instead. Or I guess you'd want the key to be named extra
, now that I think about it.not exactly the same problem, but I have written this in a few projects already
(defn maybe-assoc
"Assocs value v at key k to a map only if v is not `nil`"
[m k v]
(cond-> m
(some? v)
(assoc k v)))
I would probably have a maybe-update
flavor of the above for this case, accepting a function as input