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

4 thoughts on “C’est une pipe for Maxima”

  1. 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 %>%.

    Like

    1. 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).

      Like

  2. How do I format my comments with italics, fixed-width fonts, etc. in this blog? I’ve tried the usual conventions (*xxx*, > xxx, SPC SPC SPC SPC xxx, Ctrl-I, , etc.) and none of them seem to work (at least not while I’m composing — test below to see what happens when posted).

    > test
    | test
    test
    testtest
    test
    [math]a^b[/math]
    —test&test<>

    Like

    1. Ah! They come out when posted. Could you share a link to the documentation on which codes are supported?

      This is a test.


      This is a test.
      And some indentation.

      Like

Leave a comment