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

Re: Pre elevator pitch: Loop Info

2 views
Skip to first unread message

Paul "LeoNerd" Evans

unread,
Jan 7, 2024, 1:15:05 PMJan 7
to Paul Webster" via perl5-porters, paul.g....@googlemail.com
On Sun, 7 Jan 2024 16:16:38 -0000
"Paul Webster" via perl5-porters <perl5-...@perl.org> wrote:

> Here is the syntax that I’m proposing.
> ${^LOOPCOUNT} and ${^LOOPINDEX} (Naming is not final, willing
> to change if people would prefer something else}, available in every
> FOR & FOREACH.

(Having originally handwaved a suggestion on IRC for this) - I imagined
somewhat shorter names, maybe ${^INDEX} and ${^COUNT}

An idea for a usecase could be

foreach my $row (@rows) {
say "<table>" if ${^INDEX} == 0;
say "<tr>" . $row->as_html . "</tr>";
say "</table>" if ${^INDEX} == ${^COUNT}-1;
}

This way, if there are no rows we don't print an empty table container.
It's a bit shorter than the alternative

if(@rows) {
say "<table>";
foreach my $row (@rows) { ... }
say "</table>";
}

> Here are the potential problems.
> Minor overhead added to all loops, though perhaps this could
> be a triggerable feature requiring a “use feature ‘infoloop’” or such.

I was thinking about this too. I think if you said they had dynamic
scope, then it would indeed add a performance hit everywhere.

*But* if they are lexically scoped, only taking effect inside a lexical
foreach loop, then at compiletime we know which loops need them and can
account for them. It means the cost isn't paid anywhere else.


But overall - yes I would like to see a PPC document on this at least
so we can discuss it in further detail.

--
Paul "LeoNerd" Evans

leo...@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/

Darren Duncan

unread,
Jan 7, 2024, 2:45:05 PMJan 7
to perl5-...@perl.org
On 2024-01-07 10:17 a.m., Tomasz Konojacki wrote:
> This works today:
>
> use builtin 'indexed';
>
> for my($i, $row) (indexed @rows) {
> say "<table>" if $i == 0;
> say "<tr>" . $row->as_html . "</tr>";
> say "</table>" if $i == $#rows;
> }

As far as knowing what the current index is in a foreach loop, I feel that
explicit loop variable declarations such as shown here is by far the best
approach, and as we see it is already implemented. I don't feel that adding
special implicit variables like the original proposal is an improvement, and per
other responses seem to have a lot of downsides. The problem is already solved
it seems. -- Darren Duncan

Dave Mitchell

unread,
Jan 9, 2024, 7:30:04 AMJan 9
to paul.g....@googlemail.com, perl5-...@perl.org

First, an observation.

In a perl for loop you can't, in general, pre-determine the number of
iterations there will be, nor can you tell whether you are currently on
the last iteration. For example:

@a = (1,2,3);

for (@a) {
push @a, 4 if $_ == 3;
print "$_\n";
}

which outputs 1..4. You can have similar weirdness by shortening @a
mid-iteration. Furthermore, @a may be tied.

And now onto some opinions.

We absolutely should not use global special variables, i.e. $^{...}. If a
variable is needed, it should be lexical (possibly implicitly so) and be
declared as part of the 'for' syntax, e.g.

for my $x :$i (...) { ... }
or
for my ($x, $y) ->$i (...) { ... }

(those aren't firm proposals, just examples)

Anything which requires extra work will slow down iteration. At a minimum,
it will require an extra flag check per iteration to decide whether it can
skip the increment code.

--
Red sky at night - gerroff my land!
Red sky at morning - gerroff my land!
-- old farmers' sayings #14
0 new messages