develooper Front page | perl.perl5.porters | Postings from December 2017

Re: [perl #132607] chained comparison

Thread Previous
From:
David Nicol
Date:
December 20, 2017 19:49
Subject:
Re: [perl #132607] chained comparison
Message ID:
CAFwScO-0fgpLsvkY1uuMgMEDaJ=fJjiF_CoonkN-1XsK9svM1g@mail.gmail.com
how would this be done without looking ahead more than one parse-time
element?

Perhaps a "Comparison Expression" type would be introduced to the grammar.
Except the fact of the current syntax error implies there is already a
place the new thing could go.

Expressions in the middle should only get evaluated once, and later ones
should short-circuit out:

        $min <= &lookup1 < &lookup2 <= $max

should be equivalent to

        do {  my $tmp1 = &lookup1;  $min <= $tmp1 and do {  my $tmp2 =
&lookup2; $tmp1 < $tmp2 and $tmp2 <= $max  }   }

rather than the double-calling

        ( $min <= &lookup1 and &lookup1 < &lookup2 and  &lookup2 <= $max )

for any expression &lookupN more complicated than an unblessed scalar.


I think I'm willing to write a test case for this if it would help.



On Wed, Dec 20, 2017 at 10:39 AM, Paul "LeoNerd" Evans <
leonerd@leonerd.org.uk> wrote:

> On Tue, 19 Dec 2017 09:10:57 +0100
> demerphq <demerphq@gmail.com> wrote:
>
> > I so look forward to this, both as a patch i can learn from and as a
> > feature I would enjoy. ++Zefram.
>
> +1
>
> I'd be happy to accept some new warnings, in exchange for being able to
> e.g.:
>
>   grep { $min <= $_ <= $max } @numbers
>
> --
> Paul "LeoNerd" Evans
>
> leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
> http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/
>



-- 
“no man should be compelled to do what the laws do not require; nor to
refrain from acts which the laws permit.” Calder v. Bull (U.S. 1798)

Thread Previous


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