0
(defn bar[{:keys [a b] :as args}] (prn "got" args))

If we are calling the above function as

(bar {:a 1})

it returns

{:a 1}
nil 

I want to have

{:a 1 :b nil}
Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
Ravi Bhanushali
  • 145
  • 2
  • 9

2 Answers2

5

I don't think this can be done without changing the implementation of Clojure's destructuring code. Is below satisfactory?

user=> (defn bar [{:keys [a b]}] (prn "got" {:a a, :b b}))
#'user/bar
user=> (bar {:a 1})
"got" {:a 1, :b nil}
user2609980
  • 10,264
  • 15
  • 74
  • 143
1

Not sure if it's worth the trouble, but you can get closer using the vals->map macro from the Tupelo library:

(defn whiffer
  [{:keys [a b]}]
  (vals->map a b) )

with result:

(whiffer {:a 1, :b 2})  => {:a 1, :b 2}
(whiffer {:a 1})        => {:a 1, :b nil}
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48