Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

till (the flipflop operator, formerly ..)

1 view
Skip to first unread message

Ingo Blechschmidt

unread,
Nov 20, 2005, 2:51:03 PM11/20/05
to perl6-l...@perl.org
Hi,

according to the new S03, till is the new name for the flipflop
operator.

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


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

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?


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?


--Ingo

Larry Wall

unread,
Nov 21, 2005, 1:21:28 AM11/21/05
to perl6-l...@perl.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

Ingo Blechschmidt

unread,
Nov 21, 2005, 10:02:17 AM11/21/05
to perl6-l...@perl.org
Hi,

Larry Wall wrote:
> On Sun, Nov 20, 2005 at 08:51:03PM +0100, Ingo Blechschmidt wrote:
> : 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.

Ah, it's a macro. This clarifies things.

> : 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:

[...]

Thanks very much, this code is very clear. :)

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

Yep. (I assumed &infix:<till> would be an ordinary subroutine.)

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

Agreed. The desugar you provides feels far more sane.

> : 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 ^..^.

Ok.

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

Makes sense.


--Ingo

Larry Wall

unread,
Jan 25, 2006, 2:37:42 PM1/25/06
to perl6-l...@perl.org
I've changed the flipflop operator/macro to "ff", short for "flipflop".
This has several benefits. It's a doubled char like other short-circuit
operators. It lets us add an "fff" to be equivalent to p5's scalar
... operator, and either of them can take the ^ modifiers to exclude
endpoints. In trying to explain p5's flipflip operator, GrandFather
on perlmonks just abbreviated it as "f/f", so it seems pretty natural
that way. Also, upon a bit of psychological reflection, it's probably
actually *good* for this operator to be an obscure one, because
the very readability of "till" would tend to give people the false
impression they know what's going on. Much better to send them to
the manual to look up what "ff" means than to have them guessing wrong.
(And being alphabetic it's easier to look up than a doubled punctuation
character would be, if we had any punctuation characters to spare for
a rare operator, which we don't.)

Larry

Juerd

unread,
Jan 25, 2006, 2:59:19 PM1/25/06
to perl6-l...@perl.org
Patrick R. Michaud skribis 2006-01-25 13:47 (-0600):

> On Wed, Jan 25, 2006 at 11:37:42AM -0800, Larry Wall wrote:
> > I've changed the flipflop operator/macro to "ff", short for "flipflop".
> > This has several benefits. ...
> ...another of which is that we can use "ff" and "fff" to mean "loud"
> and "really loud" in our perl poetr^H^H^H^H^Hmusic. :-)

We need pp and ppp for balance.


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Patrick R. Michaud

unread,
Jan 25, 2006, 2:47:47 PM1/25/06
to perl6-l...@perl.org
On Wed, Jan 25, 2006 at 11:37:42AM -0800, Larry Wall wrote:
> I've changed the flipflop operator/macro to "ff", short for "flipflop".
> This has several benefits. ...

...another of which is that we can use "ff" and "fff" to mean "loud"
and "really loud" in our perl poetr^H^H^H^H^Hmusic. :-)

Pm

Rob Kinyon

unread,
Jan 25, 2006, 3:16:16 PM1/25/06
to Juerd, perl6-l...@perl.org
On 1/25/06, Juerd <ju...@convolution.nl> wrote:
> Patrick R. Michaud skribis 2006-01-25 13:47 (-0600):
> > On Wed, Jan 25, 2006 at 11:37:42AM -0800, Larry Wall wrote:
> > > I've changed the flipflop operator/macro to "ff", short for "flipflop".
> > > This has several benefits. ...
> > ...another of which is that we can use "ff" and "fff" to mean "loud"
> > and "really loud" in our perl poetr^H^H^H^H^Hmusic. :-)
>
> We need pp and ppp for balance.

/me wonders who signed up "The Little Einsteins"(tm) for P6l ...

Jonathan Scott Duff

unread,
Jan 25, 2006, 3:49:51 PM1/25/06
to perl6-l...@perl.org
On Wed, Jan 25, 2006 at 11:37:42AM -0800, Larry Wall wrote:
> I've changed the flipflop operator/macro to "ff", short for "flipflop".

Two questions:

1) Will ff (and fff) require whitespace around them?
2) Do we get a more punctuationish unicode equivalent?

-Scott
--
Jonathan Scott Duff
du...@pobox.com

Juerd

unread,
Jan 25, 2006, 3:59:53 PM1/25/06
to perl6-l...@perl.org
Jonathan Scott Duff skribis 2006-01-25 14:49 (-0600):

> 1) Will ff (and fff) require whitespace around them?

I hope it will be exactly like x and xx. They need whitespace around
them if otherwise it'd be part of an identifier.

> 2) Do we get a more punctuationish unicode equivalent?

I fear someone will suggest the ff ligature.

Nicholas Clark

unread,
Jan 25, 2006, 4:21:07 PM1/25/06
to perl6-l...@perl.org
On Wed, Jan 25, 2006 at 02:49:51PM -0600, Jonathan Scott Duff wrote:
> On Wed, Jan 25, 2006 at 11:37:42AM -0800, Larry Wall wrote:
> > I've changed the flipflop operator/macro to "ff", short for "flipflop".
>
> Two questions:
>
> 1) Will ff (and fff) require whitespace around them?
> 2) Do we get a more punctuationish unicode equivalent?

Sadly I can only find

1D191;MUSICAL SYMBOL FORTE;So;0;L;;;;;N;;;;;

It sems that fortissimo doesn't appear to have a single code point, let alone
fff

Nicholas Clark

0 new messages