Here's an example of a pure function:
3^# > 100 &
I can supply any value I please to the function, by placing that value after the prefix. E.g.:
3^# > 100 & @ 8
True
But suppose we have a pure function that already has an argument. E.g., here the argument is {2, 6, 8, 4}
:
AllTrue[{2, 6, 8, 4}, # < 10 &]
True
Further suppose I want to create a new pure function that takes the test value in the inequality as an argument. I.e., instead of using a fixed test value (in this case, 10), I want the function to be able to take any test value.
I can accomplish this using a traditional function definition:
f[n_] := AllTrue[{2, 6, 8, 4}, # < n &]
f[10]
True
But is there a simple way to accomplish this entirely with a pure function? In pseudocode, it would look something like this, where the value after the prefix is inserted in place of the ?
:
AllTrue[{2, 6, 8, 4}, # < ? &] @ 10
True
Function
with named arguments to avoid the collision, e.g(n |-> (#<n&))[10]
. (Replace the|->
with\[Function]
if you are using an older version) $\endgroup$AllTrue[{2, 6, 8, 4}, x |-> x <#] & @ 10
. $\endgroup$Function[n, AllTrue[{2, 6, 8, 4}, # < n &]]
or if you prefer more verbose code. $\endgroup$AllTrue[{2, 6, 8, 4,11}, LessThan[#]]&[10]
(orAllTrue[{2, 6, 8, 4}, LessThan[#]]&/@{10,8}
)? $\endgroup$|->
: it's just not all that clear what they mean to many people. Here's a version of that code that does work:AllTrue[First /@ FactorInteger@#, n |-> n\[Divides](#/n - 1)]&
. Be sure to check theInputForm
orFullForm
of that as well. $\endgroup$