8
$\begingroup$

I have a function of, say 4 arguments f[a,b,{c,e},d] where some of the arguments are lists. Given a list of triplets that are possible last-three-arguments to f I'd like to paste them as arguments to f as in

Map[f[2, # /. List -> Sequence] &, {{1, {2, -2}, 3}, {0, {2, -2}, 4}, {3, {5, -5}, 7}}]

but this gives me

{f[2, 1, 2, -2, 3], f[2, 0, 2, -2, 4], f[2, 3, 5, -5, 7]}

whereas I wanted

{f[2, 1, {2, -2}, 3], f[2, 0, {2, -2}, 4], f[2, 3, {5, -5}, 7]}

How can I get the result I want?

$\endgroup$
1
  • $\begingroup$ Apply to the first level is the best solution (in my opinion) $\endgroup$ – DIEGO R. Aug 11 '20 at 20:39
10
$\begingroup$
lst = {{1, {2, -2}, 3}, {0, {2, -2}, 4}, {3, {5, -5}, 7}};

f[2, ##] & @@@ lst
{f[2, 1, {2, -2}, 3], f[2, 0, {2, -2}, 4], f[2, 3, {5, -5}, 7]}

Alternatively,

Apply[f[2, ##] &, lst, 1]
Map[f[2, ## & @@ #] &, lst]
Map[f[2, Sequence @@ #] &, lst]
FlattenAt[f[2, #], 2] & /@ lst
f @@ Prepend[#, 2] & /@ lst
Prepend[f @@ #, 2] & /@ lst
$\endgroup$
1
  • 1
    $\begingroup$ One more variation for your list: f[2, Delete[#, 0]] & /@ lst. However, I still prefer your very first solution. $\endgroup$ – J. M.'s torpor Aug 19 '20 at 7:37

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.