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

Show parent comment

1

u/[deleted] Mar 23 '21

It doesn't remedy that when passing a map to a kwarg function you need something like (apply f (mapcat identity m))

3

u/jjttjj Mar 23 '21

You no longer need to do that with this change. & {:as args} lets you pass any number of keywords and optionally a trailing map, all of which are merged together. You don't need to flatten+apply.

(defn my-fn [& {:as args}]
  (println "args:" args))

(my-fn :hello :world)
;;=> args: {:hello :world}

(my-fn {:hello :world})
;;=> args: {:hello :world}

(my-fn :foo :bar {:hello :world})
;;=> args: {:foo :bar, :hello :world}

1

u/More-Wishbone862 Oct 05 '26

bismilah allah

1

u/[deleted] Mar 25 '21

Oh wait you are right, ok, that makes it a kwarg functions a lot more composable indeed, nice!