3
$\begingroup$

Suppose that I have

f[a_, b_, x_] := ...

I'd like to create a list of n pure functions

{f[1, 4, #]&, f[2, 5, #]&, f[3, 6, #]&, ...}

from two lists of length n like

as = {1, 2, 3, ...}
bs = {4, 5, 6, ...}

What can I do?

Given g[a_, b_] = ..., I know that to get {g[1, 4], g[2, 5], g[3, 6], ...} is to use MapThread[g, {as, bs}], but I don't know how to extend this idea to solve the above problem.

$\endgroup$
2
$\begingroup$

You're very close to the solution. You already have

as = {1, 2, 3}
bs = {4, 5, 6}

MapThread[g, {as, bs}]

(* ==> {g[1, 4], g[2, 5], g[3, 6]} *)

Now all you need is

g[a_, b_][x_] := f[a, b, x]

Or alternatively write Function[{a, b}, f[a, b, #] &] in place of g in MapThread.

$\endgroup$
  • $\begingroup$ Damn! you can't type so fast :) $\endgroup$ – Dr. belisarius Oct 22 '14 at 16:55
  • $\begingroup$ Alternatively, you could use With to do it for you, MapThread[With[{a = #1, b = #2}, f[a, b, #]&]&, {as, bs}] $\endgroup$ – rcollyer Oct 22 '14 at 17:19
2
$\begingroup$

Also:

Transpose[{as, bs}] /. {a_, b_} :> (f[a, b, #] &)
(* {f[1, 4, #1] &, f[2, 5, #1] &, f[3, 6, #1] &} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.