Suggestion for implementation of infix 'map' and 'apply'
Subject: Suggestion for implementation of infix 'map' and 'apply'
From: Žiga Lenarčič
Date: Mon, 19 Jan 2009 18:26:02 +0100
Hi!
I have another suggestion regarding infix versions of map an apply.
Infix notation is in many cases easier for humans to read than
prefix. Consider
plus(a,b,c) vs a + b + c.
Maxima functions 'map' and 'apply' are very common in maxima code and
infix versions would help improve readability and reduce the number
of () in the code. Consider
map( lambda([x], x+1), [1,2,3,4]) vs
lambda([x],x+1) /@ [1,2,3,4].
I know that it's easily implemented into maxima via 'infix()', and I
have done so with mathematica's "//" infix operator:
infix("//")$
"//"(expr, func) := func(expr)$
It works nicely. However I don't like the fact that a highly
customised 'maxima-init.mac' lead to somewhat unportable code - I
can't send my code to friends and expect to work in an out-of-the-box
maxima.
Therefore I suggest, that such infix operators are introduced into
maxima - if everyone agrees ofcourse. In this mail, I used
Mathematicas infix map, which is "/@". Mathematicas infix apply looks
like "@@": f @@ [1,2,3] = f(1,2,3). I don't think /@, @@ and // are
the best possible choice (they look quite cryptic, except //), but I
don't have any other ideas right now. The name of these infix
operators should be debated here on the mailing list before
implementation, so the best names, suitable for maxima are chosen.
The other day I was thinking of making lambda expressions shorter,
something like this:
(_ + 1)& -> lambda([x], x + 1) and (_1 + _2 + 1)& -> lambda([x,y], x
+ y + 1)
This is not hard to implement, but I'd be most happy with a standard
maxima implementation of this macro (instead of my private 'maxima-
init.mac' version). I later found out that the symbol "_" is already
taken, though I thought it would be most intuitive or mathematically
correct symbol to use in such a definition. So besides infix map and
apply and "//" I would like to debate, should such short lambda
function notation be introduced to maxima? If so, it would be nice if
the mailing list users would come up with a short, yet intuitive/
clear notation for such lambda functions. I think, this would also
benefit maxima code. For instance - to square every list entry and
make it a matrix and transpose would in such notation be:
( 'matrix @@ (_^2)& /@ [1,2,3,4] ) // transpose vs
transpose(apply('matrix,map(lambda([x],x^2),[1,2,3,4])))
Is there any interest for such infix notation to be included in
maxima? If there is, then 'names' of these operators should be
debated and then implemented into maxima. I for one, would really
like this to be a part of maxima.
Ziga