3

I have a function that I'd like to run multiple times, generating a list of the results:

(take 10 (repeatedly #(myfunc))) 

I realized I could run them in parallel with pmap:

(pmap (fn [_] (myfunc)) (range 10)) 

But it is a bit untidy. Is there a standard function that lets me do this Something like:

(prun 10 #(myfunc)) 

?

3 Answers 3

2

You may also be interested in The Claypoole library for managing threadpools and parallel processing. Look at their version of pmap and pfor.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think there's an existing function, but using pcalls rather than pmap seems a little closer to what you want:

(defn prun [n f] (apply pcalls (repeat n f))) 

You don't need to wrap myfunc with #() in the call torepeatedly, btw, nor calling prun as defined above:

(prun 10 myfunc) 

You may find pvalues useful as well.

2 Comments

Wow, that is precise what I was looking for. Thanks. Didn't come across those calls while looking at the documentation myself.
Glad it was helpful, Ana. You may want to upvote and possibly accept. Up to you.
1

You can use dotimes

(dotimes [_ 10] (myfunc)) 

This will run your function 10 times. Be sure to run this in the same namespace as your function

Comments