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);

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))
)
);

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].**

### Like this:

Like Loading...

Great post! I had the idea for the pipe operator a couple years ago and it is indeed very convenient. But I never took the time to implement it for n-ary functions. Now I know how 🙂 My operator is |> which I find more readable than %>%.

LikeLike

I agree, the pipe is a great idea, but something nicer to look and easier to type than %>% would be nice (I’m too used to the simple vertical pipe | used in bash).

|> works (and I think is the same way Julia does it).

LikeLike