I have a function taking a function as input as well as other values, e.g.
f2[f_, a_, b_, c_, d_] = f[{a, b}, c, d];
Of course, the behavior differs for f1 being Listable
or not. So if I define
f1[x_, y_, z_] = If[x > 0, x*y + z, -x + y + z]
it will yield
In= Attributes[f1] = {};f2[f1, 1, 2, 3, 4]
Out= If[{1, 2} > 0, {1, 2} 3 + 4, -{1, 2} + 3 + 4]
in contrast to
In= SetAttributes[f1, Listable]; f2[f1, 1, 2, 3, 4]
Out= {7, 10}
.
Now my question is: can I implement that f2
will always treat the given input function as Listable
? So that I don't have to explicitely give the Attribute to the input function beforehand and still get the listable-ish output (in this case {7,10}
.
I was trying
f3[f_, a_, b_, c_, d_] = Assuming[Element[Listable, Attributes[f]], f[{a, b}, c, d]];
but this behaves exactly like f2
.
(Of course, in this simple case, I could map over the list {a,b}
but let's assume this is not possible or not convenient.)
Thread
?Listable
is just an automaticThread
.f3[f_, a_, b_, c_, d_] = Thread[f[{a, b}, c, d]];
$\endgroup$Thread
didn't work in a similar though slightly different context $\endgroup$Thread
does not work? $\endgroup$a
,b
,c
,d
arguments? Can they be lists themselves? Do you wantf
function to thread them simultaneously with list you create inf2
body? Explicit mapping is safest in this respect, since it won't accidentally thread where you don't want it. $\endgroup$