2
votes

I have defined a function f(x, y, z) in Julia and I want to parallely compute f for many values of x, holding y and z fixed. What is the "best practices" way to do this using pmap?

It would be nice if it was something like pmap(f, x, y = 5, z = 8), which is how the apply family handles fixed arguments in R, but it doesn't appear to be as simple as that. I have devised solutions, but I find them inelegant and I doubt that they will generalize nicely for my purposes.

  • I can wrap f in a function g where g(x) = f(x, y = 5, z = 8). Then I simply call pmap(g, x). This is less parsimonious than I would like.
  • I can set 5 and 8 as default values for y and z when f is defined and then call pmap(f, x). This makes me uncomfortable in the case where I want to fix y at the value of some variable a, where a has (for good reason) not been defined at the time that f is defined, but will be by the time f is called. It works, but it kind of spooks me.
1

1 Answers

7
votes

A good solution, which turns your apparently inflexible first option into a flexible one, is to use an anonymous function, e.g.

g(y, z) = x -> f(x, y, z)

pmap(g(5, 8), x)

or just

pmap(x -> f(x, 5, 8), x)

In Julia 0.4, anonymous functions have a performance penalty, but this will be gone in 0.5.