develooper Front page | perl.perl6.language | Postings from June 2005

Re: reduce metaoperator on an empty list

Thread Previous | Thread Next
From:
John Macdonald
Date:
June 9, 2005 12:14
Subject:
Re: reduce metaoperator on an empty list
Message ID:
20050609192120.GB14501@lupus.perlwolf.com
On Thu, Jun 09, 2005 at 06:41:55PM +0200, "TSa (Thomas SandlaƟ)" wrote:
> Edward Cherlin wrote:
> >That means that we have to straighten out the functions that can 
> >return either a Boolean or an item of the argument type. 
> >Comparison functions < > <= >= = != should return only Booleans,
> 
> I'm not sure but Perl6 could do better or at least trickier ;)
> Let's assume that < > <= >= when chained return an accumulated
> boolean and the least or greatest value where the condition was
> true. E.g.
> 
>   0 < 2 < 3   returns  0 but true
> 
>   1 < 2 < 1   returns  1 but false
> 
>   4 < 5 < 2   returns  2 but false
> 
> Then the reduce versions [<] and [<=] naturally come out as min
> and strict min respectively.
> 
> Is it correct that [min] won't parse unless min is declared
> as an infix op, which looks a bit strange?
> 
> if 3 min 4 { ... }

The natural method of implementation would imply that the
final is returned:

    0 < 2 < 3   returns  3 but true
  
    1 < 2 < 1   returns  1 but false
  
    4 < 5 < 2   returns  2 but false

The application of each stage of the chain has to remember
the right hand value (for the next stage of the comparison)
as well as the accumulated boolean result.  When the boolean
result is true, that has < and <= returning the max, and > and
>= returning the min - the opposite of what you asked above.
When the numbers are not in the desired order, it would be
nice to shirtcircuit and not continue on with the meaningless
comparisons as soon as one fails - which means that the max
or min value could not be known.

Whatever is chosen, though, still has to make sense for other
chained comparisons:

    $v != $w < $x > $z == $z

cannot sensibly return either the max or the min (which would
it choose?).

I'd be inclined to have the result be val but true/false
where val is the right hand operand of the final comparison
actually tested.  When a consistant set of operators is used
(a mixture of <, <=, and ==; or a mixture of >, >=, and ==)
- then a true boolean result also provides the max (or min
respectively) value, while a false boolean result provides
the value of the first element that was out of order.

-- 

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