3
$\begingroup$

Nested pure functions can be messy, code length wise and syntax wise. It seems that the ampersand (&) in pure functions is always either proceeded by nothing:

list // SortBy[#, Last[#]] &
Select[list, First[#]==="potato" &]

or is proceeded by a special operator:

{#, f[#]} & @ item
g[#,f[#]] & /@ list
(#1 -> #2) & @@@ list
f[g[#1, #2], h[#1, #2]] & [a, b]

Which leads me to wonder—at the risk of being off topic—is it feasible for argument names to be used in the shorthand notation by having it precede the ampersand symbol? Or is there some unforeseen use case in Mathematica which makes this impossible? Hopefully the yes-no question will make it on topic.

To illustrate this, here are some methods of nesting pure functions, the first two are from kglr's linked answer above, and the last is based off Henrik Schumacher's comment:

Function[{x}, Select[x, # == Nearest[x, 4.][[1]] &]] /@ lists
With[{x = Nearest[#1, 4.`][[1]]}, Select[#1, # == x &]] & /@ lists
# /. x_ :> Select[x, # == Nearest[x, 4.][[1]] &] & /@ lists
(x \[Function] Select[x, # == Nearest[x, 4.][[1]] &]) /@ lists

Here is the same code using the hypothetical method:

Select[x, # == Nearest[x, 4.][[1]] &] & x /@ lists

and here is how multiple arguments would look:

{f[x], g[y]} & {x, y} @@@ lists

I didn't include patterns as it seems like Module, With and the shorthand method are not able to use them.

$\endgroup$
8
  • $\begingroup$ And for comparison, a & b currently evaluates to b (a &) or Times[b, Function[a]] in full form. Edit: Was responding to deleted comment about a & b potentially being evaluated as Function[b, a]. $\endgroup$
    – user55405
    Dec 4, 2019 at 22:45
  • 1
    $\begingroup$ mathprogramming-intro.org/book/node210.html $\endgroup$
    – Alan
    Dec 4, 2019 at 22:59
  • 1
    $\begingroup$ Maybe you are just looking for the infix form \[Function] of Function? $\endgroup$ Dec 4, 2019 at 23:57
  • $\begingroup$ @HenrikSchumacher That's pretty neat actually, I didn't know there was a symbol for it. I'm not sure how different it it is to the third example though, and it would need encompassing brackets around the whole function. I'll update my question to include it. $\endgroup$
    – user55405
    Dec 5, 2019 at 0:05
  • 1
    $\begingroup$ You could setup your own notation for Function with the Notation package but why would you want to obfuscate your code like this? $\endgroup$
    – Edmund
    Dec 5, 2019 at 3:02

1 Answer 1

3
$\begingroup$

I would like to agree with Henrik Schumacher's comment and advice to use infix form of Function:

enter image description here

Infix form (the arrow) can be easily entered using EscfnEsc or \[Function]

$\endgroup$
1
  • $\begingroup$ Oh my. I have not see this before!! Does it serve the same purpose as an ampersand? Or a combo of piping and ampersand? $\endgroup$ Dec 8, 2019 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.