Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a map to a function's rest argument

Tags:

clojure

In Clojure, if I have a function f,

(defn f [& r] ... )

and I have a seq args with the arguments I want to call f with, I can easily use apply:

(apply f args)

Now, say I have another function g, which is designed to take any of a number of optional, named arguments - that is, where the rest argument is destructured as a map:

(defn g [& {:keys [a b] :as m}] ... )

I'd normally call g by doing something like

(g :a 1 :b 2)

but if I happen to have a map my-map with the value {:a 1 :b 2}, and I want to "apply" g to my-map - in other words, get something that would end up as the above call, then I naturally couldn't use apply, since it would be equivalent to

(g [:a 1] [:b 2])

Is there a nice way to handle this? May I have gone off track in my design to end up with this? The best solution I can find would be

(apply g (flatten (seq my-map)))

but I surely don't like it. Any better solutions?

EDIT: A slight improvement to the suggested solution might be

(apply g (mapcat seq my-map))

which at least removes one function call, but it may still not be very clear what's going on.

like image 610
Marxama Avatar asked May 02 '13 20:05

Marxama


People also ask

How do you pass an argument to a map in Python?

Pass multiple arguments to the map() function in Python #Pass the handler function and the arguments to the functools. partial() method. Pass the result and the iterable to the map() function. The handler function will get called the arguments on each iteration.

What are necessary arguments to write a map?

The map function has two arguments (1) a function, and (2) an iterable. Applies the function to each element of the iterable and returns a map object. The function can be (1) a normal function, (2) an anonymous function, or (3) a built-in function.

Can map function have more than 2 arguments?

Passing Multiple Arguments to map() function Suppose we pass n iterable to map(), then the given function should have n number of arguments. These iterable arguments must be applied on given function in parallel. In multiple iterable arguments, when shortest iterable is drained, the map iterator will stop.

What happens if you do not use rest parameter as a last argument?

Note: The rest parameter have to be the last argument, as its job is to collect all the remaining arguments into an array. So having a function definition like the code below doesn't make any sense and will throw an error.


2 Answers

I have stumbled into this problem myself and ended up defining functions to expect one map. A map can have a variable amount of key/value pairs, and if flexible enough, so there is no need for & rest arguments. Also there is no pain with apply. Makes life a lot easier!

(defn g [{:keys [a b] :as m}] ... )
like image 157
Michiel Borkent Avatar answered Oct 21 '22 16:10

Michiel Borkent


There is no better direct way than converting to a seq.

You are done. You have done all you can.

It's just not really clojurish to have Common Lisp style :keyword arg functions. If you look around Clojure code you will find that almost no functions are written that way.

Even the great RMS is not a fan of them:

"One thing I don't like terribly much is keyword arguments (8). They don't seem quite Lispy to me; I'll do it sometimes but I minimize the times when I do that." (Source)

At the moment where you have to break a complete hash map into pieces just to pass all of them as keyword mapped arguments you should question your function design.

I find that in the case where you want to pass along general options like :consider-nil true you are probably never going to invoke the function with a hash-map {:consider-nil true}.

In the case where you want to do an evaluation based on some keys of a hash map you are 99% of the time having a f ([m & args]) declaration.

When I started out defining functions in Clojure I hit the same problem. However after thinking more about the problems I tried to solve I noticed myself using destructoring in function declaration almost never.

like image 42
Leon Grapenthin Avatar answered Oct 21 '22 16:10

Leon Grapenthin