1
$\begingroup$

For given list $\{a_1, \cdots, a_n\}$ and a function $f[a_,x]$, I want to make a function whose input is $[\{a1,\cdots, a_n\}, f[A,x]]$ and produce $\{ f[a_1,x], f[a_2, x], \cdots, f[a_n,x]\}$, i.e., plugging $a_1, a_2,$ into $a$ and make a list out of $f$.

My first trial was

 List[f[#, x]@{a1, a2}]

which seems bad. I know

  f@a1 

produce $f[a1]$, and my simple idea was using this but it seems not working well


Simply I can make

 List[f[x, y] //. {x -> a1}, f[x, y] //. {x -> a2}] 

and obtain the desired result but I want to make a simple code, not the code above.

$\endgroup$
2
  • 1
    $\begingroup$ f[#, x] & /@ {a1, a2} $\endgroup$
    – Syed
    Apr 13, 2023 at 6:11
  • 1
    $\begingroup$ Alternatively, you can just make a Table: Table[f[k, x], {k, {a1, a2}}]. $\endgroup$
    – march
    Apr 13, 2023 at 6:30

1 Answer 1

2
$\begingroup$

So many ways.

Table[f[i, x], {i, {a1, a2, a3}}]

But it's probably more typical to use table with an iterator expression rather than an explicit list. With an explicit list, Map is probably more typical:

f[#, x] & /@ {a1, a2, a3}

If you need to build something out of pieces fetched and brought together, might want

OperatorApplied[f][x] /@ {a1, a2, a3}

I often do my own currying with SubValues:

f[param_][arg_] := f[param, arg];
f[x] /@ {a1, a2, a3}

This actually switched the order, but I like my parameters to come before regular arguments. Or course, you could have done

f[param_][arg_] := f[arg, param]

Or

Thread[f[{a1, a2, a3}, x]]

Construct sometimes comes in handy. The list goes on...

$\endgroup$

Your Answer

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

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