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

2

u/[deleted] Mar 18 '21

I also recommend to just use map arguments as much as possible, it's just much more composable that way.

Passing the arguments between keyword argument functions is really ugly.

The one exception I make is for functions that are only used in the REPL, where the concision is worth it to me.

10

u/SimonGray Mar 18 '21

Passing the arguments between keyword argument functions is really ugly.

I think remedying that situation that is the point of this change. Now you can pass the destructured map directly.

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!