C’est une pipe for Maxima

 

I really love the pipe operator $>$ introduced into R by the package magrittr.  Not least because of the reference to the piece pictured here — “The Treachery of Images” by the Belgian surrealist painter René Magritte.

I don’t know yet whether this will be a really useful Maxima feature, but it is easy enough to make a simple pipe operation using infix():

infix("%>%",1,1); 
"%>%" (a,b):=b(a);

pipe1

Notice the 2nd and 3rd arguments of infix() control the precedence of operations (the left and right hand binding powers lbp and rbp).  We want to set those values lower than lbp and rbp for multiplication * in 2*%pi  and for for addition + in 1+%i (which by default in Maxima have lbp=180 and rbp=180).  Here’s the maxima document that gives the details about operators and binding power.

To make the pipe idea slightly more powerful so as to work with functions like integrate() and diff() which require more than one argument, we could try something like this, which makes use of the matlab-like find() I wrote about recently.

infix("%>%",1,1);
"%>%" (a,b):=block([i,bb],
 if atom(b) then 
     b(a)
 else (
     i:find(b=%P),
     bb:append(firstn(b,i[1]-1),[a],rest(b,i[1])),
     apply(first(bb),rest(bb,1))
     )
);

pipe2.PNG

Note that we’ve introduced the convention that the piped expression is referred to as %P, and that we call functions using a lisp-like construction where diff(%P,x) becomes [diff,%P,x].