3
$\begingroup$

I am trying to define a "postfix map" operator (denoted $OP$ for now) that behaves thus:

{1, 2, 3} $OP$ # + 1 & == {2, 3, 4}

and

{1, 2, 3} // RotateLeft $OP$ # + 1 & == {3, 4, 2}

and

{1, 2, 3} // RotateLeft $OP$ # + 1 & // Total == 9

I hope not to need parentheses when I pipe results forward using a combination of // and my custom $OP$. Is this possible? My guess is that it is not because there is no operator with the same precedence as //.

I understand that I will need to use either an unassigned operator (e.g., CirclePlus) or another symbol as an infix operator; that I cannot modify precedence; and that the ability to define custom syntax is limited (e.g., @// can't be defined).

Looking at "Operator Input Forms", it seems the closest I can get to // with regard to precedence is either Colon or VerticalSeparator, as

Precedence /@ {Colon, Postfix, VerticalSeparator} == {80., 70., 60.}

Does this mean that there is simply no way to define the operator I seek in a way that will never require parentheses when it is chained with //?

For example, given

SetAttributes[VerticalSeparator, HoldAll];
VerticalSeparator[l_, f_] := f /@ l;

this does not work

{1, 2, 3} \[VerticalSeparator] # + 1 & // Total == {(1 + #1)[1], (1 + #1)[2], (1 + #1)[3]}

Similarly, with

SetAttributes[Colon, HoldAll];
Colon[l_, f_] := f /@ l;

this does not work

{1, 2, 3} // RotateLeft \[Colon] # + 1 & == {2, 3, 1}

I have read, for example, Prefix operator with low precedence , https://stackoverflow.com/questions/5304858/custom-postfix-notation-apply-function .

$\endgroup$
  • 1
    $\begingroup$ In your two examples, you use the precedence of the operator in a manner opposite to what you want to achieve. Note that | has lower precedence than //, yet you use it in a situation where it needs to be higher. Likewise, : has a higher precedence, but your example puts it in a situation where you need it to be lower. You can't have it both ways, so you need to pick one. These work: {1, 2, 3} // RotateLeft \[VerticalSeparator] # + 1 & and {1, 2, 3} \[Colon] # + 1 & // Total (i.e., just switch the two). In addition, your definition for : is incorrect — you haven't used a pattern. $\endgroup$ – rm -rf Oct 3 '13 at 3:01
  • $\begingroup$ According to @rm-rf's comment this is the simple extension of the above linked duplicate, with the added mistake of switching the operators. $\endgroup$ – István Zachar Oct 3 '13 at 8:20
  • $\begingroup$ Just to clarify, my hope was to define an operator that would before or after. The two examples were meant to show that as far as I can tell it won't work both ways. I just wanted to confirm I hadn't missed something. Thanks for your responses. $\endgroup$ – mfvonh Oct 3 '13 at 16:27
  • $\begingroup$ {1, 2, 3} // RotateLeft \[VerticalSeparator] # + 1 == {2, 3, 1} & works as I would expect, but it doesn't produce output I would consider useful. The problem does not appear to be \[VerticalSeparator] but something else. Perhaps you should say what "works" means, that is, what output you expect. $\endgroup$ – Michael E2 Oct 4 '13 at 0:22
  • $\begingroup$ I was hoping I would be able to write in the form list // do something to whole list \[Colon] do something to each element // do something to whole list \[Colon] do something to each element // etc. $\endgroup$ – mfvonh Oct 7 '13 at 11:23

Browse other questions tagged or ask your own question.