On Tue, Feb 24, 2009 at 04:54:35PM -0800, Jon Lang wrote:
: Half-baked idea here: could we somehow use some dwimmery akin to
: Whatever magic to provide some meaning to a postfix:<%> operator?
: Something so that you could say:
: 
:   $x within 5%
: 
: And it would translate it to:
: 
:   $x within 0.05 * $x
: 
: ?
: 
: Something along the lines of:
: 
: 1. infix:<within> sets Whatever to its lhs while evaluating its rhs
: 2. postfix:<%> returns the requested percentage of Whatever; if
: Whatever is undefined, return the requested percentage of 1.

The semantic magic is trivial; what we'd say is that if C<within> has
a 1-ary closure for its right argument, it calls that with its left
argument to calculate the epsilon.  The syntactic magic is another
matter.  We could easily define postfix:<%> that just translates 5% to
{ 0.05 * $_ }, but we'd have to weigh that against the infix use of %.
So basically it could be done, but we'd have to require whitespace
on the infix:<%> form.

Interestingly, though, assuming we have multies for Code:($) as well
as as Whatever, things like $x * 5% would also work automatically.
Which we'll probably have anyway, if we want things like

    grep (* % 3) == 1, @list

to work.  It basically looks like Whatever is mutating into a
mechanism for currying 1-ary functions, though of course it won't
work if you try to curry infix:<..> with it, or any other operator
that treats Whatever idiosyncratically.  You'd basically have to
know which operators Do What You Mean, which could be construed as an
arbitrary list.  Makes me wonder if this is one of those "new mistakes"
we're trying to make with Perl 6.  :)

But my gut-level feel is that, although this is a feature that
you can easily shoot your foot off with, the actual code comes
out quite readable.  And I think people can pretty easily learn
the default transformation of (* op 42) to { $_ op 42 }.  On the other
hand, * is so hardwired in to our consciences as wildcarding and
quantifying that constructs like "abc" xx * also look natural.

So I'm gambling that this is a new mistake we want to make.  The
alternative would be to create a different way of writing simple
closures, but we already have that in { $_ op 42 } if you want it.

I do very much like the fact that, unlike in Haskell, you curry
with an explicit placeholder, not by leaving things out.  (Of course,
this is a requirement in Perl anyway because its parser depends
heavily on term/infix alternation.)

Larry

Reply via email to