From: bugbear on
After careful thought I have concluded that:
$x < $aa && $x < $bb
is an equivalent condition to
$x < min($aa, $bb)

I cannot presently see how this generalises
for all the comparators (< <=, == !=, >=, >) , and "max" as well
as "min".

Can anyone give me a general rule for replacing

$x <comparator> <min|max($aa, $bb)

My reasons for wanting to avoid min/max
are fairly theoretical and unimportant :-)

NugBear
From: Willem on
bugbear wrote:
) After careful thought I have concluded that:
) $x < $aa && $x < $bb
) is an equivalent condition to
) $x < min($aa, $bb)
)
) I cannot presently see how this generalises
) for all the comparators (< <=, == !=, >=, >) , and "max" as well
) as "min".

It doesn't generalise. There are three separate cases.
The first case, you already mentioned.

The second case:

$x < max($aa, $bb)
is equivalent to
$x < $aa || $x < $bb

But the third case (equality) is more complicated:

$x == max($aa, $bb)
is equivalent to
($x == $aa && $aa <= $bb) || ($x == $bb && $bb <= $aa)
which can't be simplified further.


PS: Yes, theoretically the last case is general for all combinations,
but in the other cases it can be simplified.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
From: Ted Zlatanov on
On Fri, 30 Jul 2010 11:16:11 +0100 bugbear <bugbear(a)trim_papermule.co.uk_trim> wrote:

b> After careful thought I have concluded that:
b> $x < $aa && $x < $bb
b> is an equivalent condition to
b> $x < min($aa, $bb)

b> I cannot presently see how this generalises
b> for all the comparators (< <=, == !=, >=, >) , and "max" as well
b> as "min".

b> Can anyone give me a general rule for replacing

b> $x <comparator> <min|max($aa, $bb)

You should look at Quantum::Superpositions (Perl 6 has built-in support
for the same concepts). min() and max() simply collapse the
superposition. So, for example, your conclusion is equivalent to

$x < all($aa, $bb) === $x < min($aa, $bb)

Similarly:

$x > any($aa, $bb) === $x > min($aa, $bb)

Ted
From: Ben Morrow on

Quoth Willem <willem(a)turtle.stack.nl>:
> bugbear wrote:
> ) After careful thought I have concluded that:
> ) $x < $aa && $x < $bb
> ) is an equivalent condition to
> ) $x < min($aa, $bb)
> )
> ) I cannot presently see how this generalises
> ) for all the comparators (< <=, == !=, >=, >) , and "max" as well
> ) as "min".
>
> It doesn't generalise. There are three separate cases.
> The first case, you already mentioned.
>
> The second case:
>
> $x < max($aa, $bb)
> is equivalent to
> $x < $aa || $x < $bb
>
> But the third case (equality) is more complicated:
>
> $x == max($aa, $bb)
> is equivalent to
> ($x == $aa && $aa <= $bb) || ($x == $bb && $bb <= $aa)
> which can't be simplified further.

Or to

$x == ($aa > $bb ? $aa : $bb)

which generalises like

$x == ($aa > $bb ? $aa : $bb > $cc ? $bb : $cc);

and is just a written-out max().

To the OP: max() is just

sub max {
my $rv = shift;
$_ > $rv and $rv = $_ for @_;
return $rv;
}

so there's really no need to make your code ugly to avoid it even if you
think you can't use List::Util.

(Now, of course, I'm wondering about a '<=' operator, which isn't the
usual 'less-than-or-equal' operator but instead a compare-and-assign
operator like ||=... It's hard to see what it might sensibly be called.)

Ben

From: sln on
On Fri, 30 Jul 2010 11:16:11 +0100, bugbear <bugbear(a)trim_papermule.co.uk_trim> wrote:

>After careful thought I have concluded that:
> $x < $aa && $x < $bb
>is an equivalent condition to
> $x < min($aa, $bb)
>
>I cannot presently see how this generalises
>for all the comparators (< <=, == !=, >=, >) , and "max" as well
>as "min".
>
>Can anyone give me a general rule for replacing
>
>$x <comparator> <min|max($aa, $bb)
>
>My reasons for wanting to avoid min/max
>are fairly theoretical and unimportant :-)
>
> NugBear

Its hard to fathom that min/max are not functions
where its body could be stripped out and integrated
as an "equivalent condition" within a surrounding
boolean expression.

Boolean expressions say what you want, not what you
don't. Thinking this way about min/max will lead you
down the wrong path, that is a "math" path, which
doesen't really coincide with a programming "logic"
path. Code doesen't look like math, and visa versa.
Warning, pretty soon you will be disecting transendentals
for the equivalent condition.

-sln