This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-09-16
Channels
what's the current preferred tool/method to spin up a new clojure deps project that uses tools.build? thanks
I'm pretty sure deps-new does that: https://github.com/seancorfield/deps-new
How can I define a function that has an optional last argument which defaults to an empty map?
a common way is two separate arities, and the n-1 arity calls the n arity with the default value
ah ok!
as a contrived example:
(defn frob
([x] (frob x 5))
([x y] (vector x y)))
(frob 1 2)
=> [1 2]
(frob 1)
=> [1 5]
I tried with associative destructuring and couldnt get it to work.
since the :or
part of associative destructuring is in the map argument, it still requires the argument be passed - but with the n-1 arity, that can pass the empty map, which will cause the :or
part of the destructuring to be used
(defn frob [x & [y]] ...)
will give you y
bound to nil
if it isn't passed in which might be able to stand in for an empty map, depending on what you want to do to it.
(but I generally prefer the multi-arity approach Bob suggested since it makes the default values clearer)