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

Re: <flip flop> xx Inf

15 views
Skip to first unread message

Luke Palmer

unread,
Dec 3, 2004, 7:37:54 PM12/3/04
to Juerd, perl6-l...@perl.org
Juerd writes:
> What happens to the flip flop operator? Will .. in scalar context
> remain the same? What comes in place of ...? (An adverb?)

The scalar range operator was always a weird one for me, but that isn't
to say that it hasn't been put to good use by wizards. I wouldn't be
surprised if it stayed. On the other hand, I wouldn't be surprised if
it left.

Anyway, to answer what I _do_ know, isn't .. exactly the same as ... in
Perl 5? That was my impression, at least (I've never used the latter in
practice, but my little test script seems to work).

Anyway, in Perl 6, list .. is your good old Perl 5 .. with lazy
semantics.

If you do ... in term position, you get a yada-yada-yada term, which
dies if you execute it. If you do ... in operator position, you get the
equivalent of ..Inf.

say for 1...;

1
2
3
4
5
.
.
.

Luke

Juerd

unread,
Dec 3, 2004, 7:11:30 PM12/3/04
to perl6-l...@perl.org
What happens to the flip flop operator? Will .. in scalar context remain
the same? What comes in place of ...? (An adverb?)


Juerd

Larry Wall

unread,
Dec 3, 2004, 8:31:25 PM12/3/04
to perl6-l...@perl.org
On Sat, Dec 04, 2004 at 01:11:30AM +0100, Juerd wrote:
: What happens to the flip flop operator? Will .. in scalar context remain
: the same?

I don't think so. It's definitely a candidate for a longer
Huffmanization simply in terms of frequency of use. On top of which,
almost no Perl 5 programmers even know what it does. Plus it's
basically opaque state in the pad. Not that opaque is bad when you
want the abstraction, but I would characterize the state of scalar ..
as almost "sneaky" state.

Plus we have the problem that we don't have a $. variable anymore,
nor really the concept of "last input handle". I expect the p5-to-p6
translator will have to detect any references to $. and set some
kind of global $*LASTIN handle on input, and define &p5dot to
be $*LASTIN.chunknum or some such. Then the flip flop might be done with
a real state variable.

So Perl 5's

if (1..10) {...}

could look something like this:

if flipflop(state $x, { p5dot() == 1 }, { p5dot() == 10 }) {...}

Pity it clobbers the current lexical scope with the name $x. Now we
see that it'd be cool to have an anonymous state variable somehow:

if flipflop(state, { p5dot() == 1 }, { p5dot() == 10 }) {...}

That would let us have a state variable that is attached to our scope but
that is nevertheless only named within the flipflop function.

Of course, if flipflop were a macro, that could easily be reduced to

if flipflop(p5dot() == 1, p5dot() == 10) {...}

or even, if you get fancy with the macro:

if flipflop 1, 10 {...}

But then we lose the flexibility of writing any of:

if flipflop(state, { p5dot() == 1 }, { p5dot() == 10 }) {...}
if flipflop(my, { p5dot() == 1 }, { p5dot() == 10 }) {...}
if flipflop(our, { p5dot() == 1 }, { p5dot() == 10 }) {...}

to control when the state gets reset, or who can reset it. So I'd
guess there's a &*flipflop builtin function, and we let people define
a macro if they want it sugary, or sugarier, or sugarierier.

Or if people carp too much, we go ahead and give them a macro too. Call
it p5dotdot or some such.

: What comes in place of ...? (An adverb?)

Presumably a named arg of some sort, maybe

if flipflop(state, { p5dot() == 1 }, { p5dot() == 10 }, :after) {...}

or some such.

Now I'm trying to figure out the uses of a closure that ends with:

return \state;

That would allow a caller to change my state without me keeping track
of it at all. Works sort of a like a (void*) that's reserved for
the user. On the other hand, if it's returning a bare state it's
not gonna be very good at returning anything else useful. But maybe

return $value, \state;

has something useful in it.

Larry

Jon Ericson

unread,
Dec 3, 2004, 9:16:16 PM12/3/04
to perl6-l...@perl.org
Luke Palmer <lu...@luqui.org> writes:

> Juerd writes:
>> What happens to the flip flop operator? Will .. in scalar context
>> remain the same? What comes in place of ...? (An adverb?)

> Anyway, to answer what I _do_ know, isn't .. exactly the same as ... in


> Perl 5? That was my impression, at least (I've never used the latter in
> practice, but my little test script seems to work).

Not exactly. From perlop:

In scalar context, ".." returns a boolean value. The
operator is bistable, like a flip-flop, and emulates the
line-range (comma) operator of sed, awk, and various
editors. Each ".." operator maintains its own boolean
state. It is false as long as its left operand is false.
Once the left operand is true, the range operator stays true
until the right operand is true, AFTER which the range
operator becomes false again. It doesn't become false till
the next time the range operator is evaluated. It can test
the right operand and become false on the same evaluation it
became true (as in awk), but it still returns true once. If
you don't want it to test the right operand till the next
evaluation, as in sed, just use three dots ("...") instead
of two. In all other regards, "..." behaves just like ".."
does.

So it's a bit obscure. :-)

Jon

Larry Wall

unread,
Dec 3, 2004, 8:42:53 PM12/3/04
to perl6-l...@perl.org
On Fri, Dec 03, 2004 at 05:37:54PM -0700, Luke Palmer wrote:
: Juerd writes:
: > What happens to the flip flop operator? Will .. in scalar context
: > remain the same? What comes in place of ...? (An adverb?)
:
: The scalar range operator was always a weird one for me, but that isn't
: to say that it hasn't been put to good use by wizards. I wouldn't be
: surprised if it stayed. On the other hand, I wouldn't be surprised if
: it left.

It's leaving syntactically but not semantically.

: Anyway, to answer what I _do_ know, isn't .. exactly the same as ... in


: Perl 5? That was my impression, at least (I've never used the latter in
: practice, but my little test script seems to work).

I think you'll find there's a difference between

perl -ne 'print if 2..2';
perl -ne 'print if 2...2';

But as they say: "When all else fails, read the directions." :-)

: Anyway, in Perl 6, list .. is your good old Perl 5 .. with lazy
: semantics.

Yes, but it has to do that in scalar position now too, since it constructs
a lazy Range object. That's the strongest argument for changing Perl 5's
flipflop to some other syntax, actually. (I thought about putting it into
the other message I was composing, except I was driving at the time I
thought of it, and forgot it by the time I got back to the house. But hey,
I'm getting old enough I can start blaming it on senility, right?)

Larry

Michele Dondi

unread,
Dec 10, 2004, 7:39:19 AM12/10/04
to perl6-l...@perl.org
On Fri, 3 Dec 2004, Larry Wall wrote:

> On Sat, Dec 04, 2004 at 01:11:30AM +0100, Juerd wrote:
> : What happens to the flip flop operator? Will .. in scalar context remain
> : the same?
>
> I don't think so. It's definitely a candidate for a longer
> Huffmanization simply in terms of frequency of use. On top of which,
> almost no Perl 5 programmers even know what it does. Plus it's

Well, I have the vague feeling that you may have some more experience with
Perl than I do, however such a claim seems a bit exaggerate to me. For
example I do know what it does and even though I consider myself to be at
most an advanced beginner rather than an expert, I think that I use it
correctly and to great advantage, when I do use it, that is...

For sure it can't be just me!!


Michele
--
DAX ODIA ANCORA
- Scritta su diversi muri milanesi

Veky None

unread,
Dec 13, 2004, 5:16:42 PM12/13/04
to
//* For sure it can't be just me!! *//

Of course. I use it, and I'm really just a beginner. I think Larry has
underestimated us a little... ;-)

0 new messages