develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC: Multiple-alias syntax for for

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
June 9, 2021 06:42
Subject:
Re: RFC: Multiple-alias syntax for for
Message ID:
20210609064200.GR16703@etla.org
On Tue, Jun 08, 2021 at 03:50:07PM -0400, Dan Book wrote:
> On Tue, Jun 8, 2021 at 3:35 PM Paul "LeoNerd" Evans <leonerd@leonerd.org.uk>
> wrote:
> 
> > On Tue, 8 Jun 2021 14:23:26 -0500
> > David Nicol <davidnicol@gmail.com> wrote:
> >
> > > Perl already has this feature.

Your statement "has this feature" is not accurate. What you show below
is using existing functionality (features, *plural*) to implement the
behaviour.

What you mean is "Perl does not need a special feature for this because it
can be implemented with existing features".

Which in itself is a sensible argument against adding a feature.

Trouble is, that Perl is a Turing complete language. Meaning that *any*
possible new feature that could be proposed can already be implemented with
existing functionality.

So arguing against a new feature because it is already possible is, well,
tautological.


So, I assume that what you are really trying to argue is:

  Perl does not need a special feature for this because it can be *easily*
  be implemented with existing features.


Which gets us to:

> > > @triples % 3 and die "TRIPLES ARRAY NOT MULTIPLE OF 3 IN LENGTH";
> > > while (@triples){
> > >     my ($foo, $bar, $baz) = splice @triples, 0, 3;
> > >     ...
> > > }
> >
> > We do, but it's not very nice for two reasons:
> >
> >  1) DRY failure - you've written `@triples` once in the looping
> >     condition and then a second time in the `splice`
> >
> >  2) It's destructive on the array
> >
> > Combined, that means it's impossible to run it on any non-variable
> > expression, such as the result of a function call. Whereas, calling a
> > function is just as possible with the multivariabled foreach:
> >
> >   foreach my ($red, $green, $blue) (get_palette()) {
> >     ...
> >   }
> >
> 
> Also it doesn't work on hashes.

Or efficiently on things like ranges. The syntax (1..9999999) doesn't
actually generate a temporary list of 9,999,999 items:

$ ./perl -Ilib -MO=Concise,-exec -e 'for my $c (1..9999999) { ... } '
1  <0> enter
2  <;> nextstate(main 1 -e:1) v:{
3  <0> pushmark s
4  <$> const(IV 1) s
5  <$> const(IV 9999999) s
6  <{> enteriter(next->b last->e redo->7)[$c:2,5] vKS/LVINTRO
c  <0> iter s
d  <|> and(other->7) vK/1
7      <;> nextstate(main 4 -e:1) v
8      <0> pushmark s
9      <$> const(PV "Unimplemented") s
a      <@> die vK/1
b      <0> unstack v
           goto c
e  <2> leaveloop vK/2
f  <@> leave[1 ref] vKP/REFC
-e syntax OK


The approach your suggesting would require creating a temporary array to
hold the contents of the list (or hash, or array you don't want to destroy)
- this is a bunch of copying that the direct syntax avoids.

I *think* that one could use a loop like this to avoid the destructive splice:

   for (my $i = 0; $i < @array; $i += 3) {
       my ($foo, $bar, $baz) = @array[$i .. $i + 2];
       ...
   }

but I've deliberately *not* tested that, as my point is that there are
several things that I know that I might get wrong here (boundary cases in
the for loop, boundary cases in the list slice) - it's "no obvious bugs"

Whereas the proposed syntax is

   for my ($foo, $bar, $baz) (@array) {
       ...
   }

"obviously no bugs" (famous last words)


Finally, foreach *aliases* its iterator - if the loop actually wants to
modify the values in-place, any "copying" approach isn't going to solve the
programmer's problem easily.


This gets back to the non-tautological argument:

    Perl does not need a special feature for this because it can be *easily*
    be implemented with existing features.


I don't think that this is true.

The existing approaches are more verbose, and have many ways to make
mistakes.

Nicholas Clark

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