# How to supply argument to pure function when slot is already defined?

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


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

• You can use Function with named arguments to avoid the collision, e.g (n |-> (#<n&)). (Replace the |-> with \[Function] if you are using an older version) Jul 5, 2021 at 8:01
• ..., i. e., AllTrue[{2, 6, 8, 4}, x |-> x <#] & @ 10.
– kglr
Jul 5, 2021 at 8:10
• Or Function[n, AllTrue[{2, 6, 8, 4}, # < n &]] or if you prefer more verbose code. Jul 5, 2021 at 8:23
• Although not an answer to the general question you ask, for the example you cite you could use AllTrue[{2, 6, 8, 4,11}, LessThan[#]]& (or AllTrue[{2, 6, 8, 4}, LessThan[#]]&/@{10,8})? Jul 5, 2021 at 8:33
• @theorist This is the exact reason I don't like glyphs like |->: 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 the InputForm or FullForm of that as well. Jul 5, 2021 at 9:24

If you don't want to follow some suggestions made in comments and use the Function with named arguments (and there may be valid reasons to be willing to avoid that), you can use With instead:

fn = With[{n = #}, AllTrue[{2, 6, 8, 4}, # < n &]] &


So that

{fn, fn}

(* {False, True} *)

• I like that construction because of its intuitive nature. Plus it led me to realize it can be written even more simply as: (n = #; AllTrue[{2, 6, 8, 4}, # < n &]) &. Are there examples in which this wouldn't work, and you would need With? Jul 7, 2021 at 22:59
• @theorist Your version introduces n as a global variable, which is generally a bad idea. You could wrap your code in Module (or, in this case, Block), to localize it, like e.g. Block[{n}, (n = #; AllTrue[{2, 6, 8, 4}, # < n &]) &]. But then it essentially becomes the same as using With. Using With seems the cleanest of such methods here, since we know that n is not going to change inside the code. As to examples of failures: in your case, the predicate # < n & is very simple. There could be more complex ones defined separately and referring to global n, which could fail. Jul 8, 2021 at 13:20