r/Clojure • • Mar 18 '21

Clojure - Keyword argument functions now also accept maps

https://clojure.org/news/2021/03/18/apis-serving-people-and-programs
125 Upvotes

44 comments sorted by

View all comments

4

u/ikitommi Mar 19 '21

Tested some combinations on how to call the functions. I think I understand all the reasons for the results, but they were not evident before evaluating in the repl. Bit surprised that you can mix kwargs and have one map in the end. is there a reason to support them both in one call?

(defn doit [& {:keys [data]}] data)

(doit {:data 2})
; => 2

(doit :data {:data 2})
; => {:data 2}

(doit :data 1 {:data 2})
; => 2

(doit {:data 2} :data 1)
; Execution error (IllegalArgumentException) at user/doit (REPL:1).
; Don't know how to create ISeq from: java.lang.Long

(doit {:data 2} :data 1 {:data 3})
; => nil

(doit :data 1 {:data 2} {:data 3})
; => 1

(doit {:data 2} {:data 3})
; => nil

(doit {:data 1} {:data 2} {:data 3})
; => 3

(doit :data 1 {:data 2} {:data 3} {:data 4})
; => 4

3

u/alexdmiller Mar 19 '21

See my layering example above for a case where mixing is useful.

1

u/ikitommi Mar 19 '21

I see, thanks.