1

Suppose I want to apply the parameter p1 and p2 to a list of function calls:

(defn some-func [] (let [p1 a p2 b] (f1 p1 p2) (f2 p1 p2) (f3 p1 p2) ... )) 

What's a good and concise way of doing this?

2
  • 3
    It would be great if you accepted some of the answers to your questions. Commented Feb 10, 2020 at 4:48
  • 4
    Do you want to apply those functions just for "side effects" or you want to use their return values? Commented Feb 10, 2020 at 5:11

3 Answers 3

3

Assuming you want just side-effects, I'd use doseq to force the calls; iterate the functions you want to call. e.g.

user=> (doseq [f [println println println]] (f 1)) 1 1 1 nil 
Sign up to request clarification or add additional context in comments.

Comments

1

You may want to review the Clojure CheatSheet. The function juxt can do what you want.

Beware, though, juxt is somewhat obscure and can make it hard to read your code.

3 Comments

juxt returns a vector. What I really want is these function calls in a sequence. I tried (lazy-seq (juxt...)), but it says "Can't convert lazyseq to IFn".
i.e., how do I convert the vec to IFn?
Read the docs on the 2 links above, esp the 2nd one to clojuredocs.org
0

You can use for or map, both of which return a clojure.lang.LazySeq. Small working example:

(defn f1 [x y] (+ x y)) (defn f2 [x y] (- x y)) (defn f3 [x y] (* x 1)) ;; Method 1 - for (for [f [f1 f2 f3]] (f 10 5)) ;; => (15 5 50) ;; Method 2 - map (map #(% 10 5) [f1 f2 f3]) ;; => (15 5 50) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.