Mathematica allows to define pure function, like
Function[{a, b},Length[Unevaluated@a]{b}][1+2,2+3]
(*
==> {0}
*)
Pure functions in Mathematica can take an arbitrary number of arguments, but only if not naming them, for example:
Function[Length[Unevaluated@#1]{##2}][1+2,2+3,3+1]
(*
==> {0,0}
*)
Also, pure functions can optionally have attributes, for example:
Function[{a,b},Length[Unevaluated@a]{b},{HoldFirst}][1+2,2+3]
(*
==> {10}
*)
However what I haven't found is a way to have both arbitrary many arguments and attributes:
Function[(* what, if anything, to put here? *)][1+2,2+3,3+1]
(*
==> {10, 8}
*)
Therefore my question:
Is it possible to define pure functions which take an arbitrary number of arguments and at the same time have attributes? And if so, how would one define them?
The obvious solution doesn't work:
Function[Length[Unevaluated@#1]{##2},{HoldFirst}][1+2,2+3,3+1]
(*
Function::flpar: Parameter specification Length[Unevaluated[#1]] {##2} in
Function[Length[Unevaluated[#1]] {##2},{HoldFirst}] should be a symbol or
a list of symbols. >>
*)
Adding an empty parameter list disables parameter substitution for ##
;
Function[{},Length[Unevaluated@#1]{##2},{HoldFirst}][1+2,2+3,3+1]
(*
==> {##2}
*)
Of course, a workaround is easy; for example, have the pure function take a list (which in the example above would actually have been the better alternative anyway), or simply using a named function. So it's more of a curiosity. It just seems odd to have two completely orthogonal features of pure functions, and yet not being able to combine them.
Function[, ##, <Attributes>]
, for instance. Is that what you were asking? $\endgroup$Function[, {Unevaluated@#, Unevaluated@#2}, HoldFirst][2 + 2, 2 + 2]
... $\endgroup$HoldAll
andListable
are used in the examples. $\endgroup$Slot
s for arguments. $\endgroup$