develooper Front page | perl.perl6.language | Postings from November 2005

Re: till (the flipflop operator, formerly ..)

Thread Previous | Thread Next
From:
Larry Wall
Date:
November 20, 2005 22:21
Subject:
Re: till (the flipflop operator, formerly ..)
Message ID:
20051121062128.GA9451@wall.org
On Sun, Nov 20, 2005 at 08:51:03PM +0100, Ingo Blechschmidt wrote:
: Hi,
: 
: according to the new S03, till is the new name for the flipflop
: operator.

Presuming we can make it work out as an infix macro.

: Do the flipflop operators of subroutines maintain own
: per-invocation-of-the-sub states? I.e.:
: 
:     sub foo (&x) { x() till 0 }
: 
:     foo { 0 };  # evaluates to a false value, of course
: 
:     foo { 1 };  # evaluates to a true value, of course
:     foo { 0 };
:     # still true?
:     #   (Argumentation: The flipflop is in the "true" state,
:     #   so the LHS is not evaluated.)
:     # Or is it false?
:     #   (Argumentation: The flipflop operator of the previous
:     #   invocation is not the flipflop operator of the current
:     #   invocation, so the return value is false.)

It's still true.  Ignoring the "E0" issue, the desugar of "A till B"
is something like:

    do {
	state $flipflop = 0;
	my $retval;
	if $flipflop or A {
	    $retval = ++$flipflop;
	    if B { $flipflop = 0 }
	}
	else {
	    $retval = 0;
	}
	$retval;
    }

or more succinctly,

    do {
	state $flipflop = 0;
	$flipflop or A
	    ?? (++$flipflop, not B || ($flipflop = 0))[0]
	    !! 0;
    }

The rewrite of x() till 0 looks like:

    do {
	state $flipflop = 0;
	my $retval;
	if $flipflop or x() {
	    $retval = ++$flipflop;
	    if 0 { $flipflop = 0 }
	}
	else {
	    $retval = 0;
	}
	$retval;
    }

or

    do {
	state $flipflop = 0;
	$flipflop or x()
	    ?? (++$flipflop, not 0 || ($flipflop = 0))[0]
	    !! 0;
    }

(Note that P5's ... syntax has other semantics which I'm not sure how
to combine with the "till" proposal.  But the ... semantics are kind
of bogus anyway--it basically doesn't test for falsification till
the next time through, so if you're searching for the beginning and
ending lines with patterns, the two lines can't be the same line.
Maybe that's the "tilll" operator. :-)

: Also, all operators can be called using the subroutine form (which is a
: very good thing), e.g.:
: 
:     say infix:<->(42, 19);  # 23
: 
: Is this true for till as well?
: 
:     say infix:<till>(LHS, RHS);

Probably not.  Calling macros as functions is a bit of a problem.

: But how would &infix:<till> maintain the state then, as no explicit ID
: is passed to it? Does &infix:<till> access an internal %states hash,
: using $CALLER::POSITION as keys?

That feels like a hack to me.  I'd rather find a way of poking a real
state variable into the caller's scope if we have to support that.

: Perl 5's flipflop operator appends "E0" to the final sequence number in
: a range, allowing searches for /E/. My guess is that this is superseded
: by "$sequence_number but this_is_the_endpoint_of_the_range" (you get
: the idea). Correct?

I was just thinking that you'd use till^ if you wanted to exclude the
endpoint.  And ^till to exclude the beginning, and ^till^ to exclude
both, just as with ..^, ^.., and ^..^.

In fact, that's really my main motivation for wanting it to be infix.
Otherwise it might as well be an ordinary flipflip() macro, or fromto().

Oh, we also haven't really dealt with the implicit comparison to
the current line number, but that's perhaps something we don't really
need to support any more.

Larry

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About