Tag Info

New answers tagged

1

The point is that List has the special property that the head doesn't disappear even with one element. So the solution (as belisarius) noted in the comment is to make the code generate expr with Head List. So that , expr = List[a[i],b[i],c[i],d[i]] Then I can safely pass this to Map as in Map[f, expr], and it doesn't matter how many elements f has. Now ...


2

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


2

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.


5

You can almost always turn to replacement patterns when you need to transform expressions: Cases[ {{a, b}, {c, d}, {e, f}}, {x_, y_} :> (x[#]/y[#] &) ] {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} Cases defaults to levelspec {1} so this is safer than using /..


4

Also: With[{a = #1, b = #2}, a[#]/b[#] &] & @@@ {{a, b}, {c, d}, {e, f}} or x[#]/y[#] & /. {x -> #1, y -> #2} & @@@ {{a, b}, {c, d}, {e, f}} (* {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *)


11

This perhaps: Function[{a, b}, a[#]/b[#] &] @@@ {{a, b}, {c, d}, {e, f}} (* Out: {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *) Mr.Wizard's way of writing it (see comment) looks like this in the frontend:


0

This question is done and dusted but it is such a good one (together with the existing answers) for demonstrating and encapsulating the different ways Associations can be modified, that I'd like to belatedly summarise while suggesting some other takeaways. In particular, how it can further highlight a mutable and immutable dichotomy or perhaps more ...



Top 50 recent answers are included