# avoiding min and max

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted 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

## Re: avoiding min and max

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

## Re: avoiding min and max

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

## Re: avoiding min and max

Ben Morrow wrote:

\$x = min( \$x, \$y, \$z, ... );

\$x min= \$y, \$z, ...;

\$x->min( \$y, \$z, ...);

Of course you often have a running minimum:

\$x = min( \$x, \$min );

\$x min= \$min;

\$x->min( \$min );

--
Ruud

## Re: avoiding min and max

R>    \$x = min( \$x, \$y, \$z, ... );

R>    \$x min= \$y, \$z, ...;

R>    \$x->min( \$y, \$z, ...);

The second one is best.

But I think it's much better to treat this as a general stats problem.
_ caches the stat call, so why not cache list stats too if requested?

cache_stats(@list, qw/min avg median stdev/);

min(@list); # fast

push @list, min(@list) -1;

min(@list); # automatically updated

max(@list); # slow, not cached

This is not so good if you have lots of small updates, that's why it
should be optional.  I don't think it can be done in Perl 5.

Ted

## Re: avoiding min and max

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

## Re: avoiding min and max

wrote:

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