2

I have this function that takes a collection of functions as parameters:

(defn- main-func
  [fn-list]
...)

I usually call it like this:

(main-func [f1 f2 ...])

But f1 and f2 are all no-arg functions. How can I include a function with parameters in this call? I searched Google but with no success. Thanks

3
  • 2
    You can wrap the function with parameters with an anonymous function containing the arguments, e.g. #(foo 1 2 3 4). Is your main-func using the fn-list for their side-effects? Commented Jul 1, 2013 at 13:47
  • It works, thanks. Can you write an answer so I can mark it? <code>main-fun</code> does not depend on the result or effects of the functions provided to it. The only catch is that the have to be executed after some code from <code>main-func</code> is executed. I did some tests and it seems to be working. Commented Jul 1, 2013 at 14:59
  • Sure. The reason I asked is that purely functional functions of no arguments are just constants. Commented Jul 1, 2013 at 17:12

2 Answers 2

5

A function with parameters, e.g.

(defn foo [w x y z] ...)

may be wrapped by an anonymous function containing the arguments, e.g.

#(foo 1 2 3 4)
Sign up to request clarification or add additional context in comments.

5 Comments

I think (partial foo 1 2 3 4) is more canonical? Jay Fields seems like he knows what he's talking about. blog.jayfields.com/2012/10/…
@event_jr Thanks for the link, an interesting read, but I'm not convinced his point is to avoid every use of anonymous function syntax. The anonymous function here seems natural, whereas for me the partial option mentally signals that there are additional arguments to be supplied, which is not the case here. Note that (partial foo 1 2 3 4) is not "no-arg" function but a variadic one.
What are the practical consequences of the function being variadic?
Practical, as in performance? Nothing I would worry about from an avoid premature optimization perspective, but a no-argument pure function can potentially be substituted with a constant more easily by an optimizing (JIT) compiler. Whether one is or the other is not, I don't know. This is a matter of taste -- partial to me signals additional arguments. If there are additional arguments, I would not write #(foo 1 2 3 %) where partial is natural, but I certainly would not bend over backwards to avoid #(foo 1 % 3 4).
Psychologically, I would prefer (constantly (foo 1 2 3 4)) over (partial (foo 1 2 3 4)) because while both are variadic, the first signals that additional arguments are ignored. Personally though, I would still choose #(foo 1 2 3 4).
4

Apart from the anon function suggested by A. Webb, you can use the function partial, which given a fn, creates another fn with some parameters already set. If you specify all the parameters, it will return a fn that you can call with no params:

 (def plus-12 (partial + 1 2))
 (plus-12) => 3
 (plus-12 3) => 6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.