Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum of empty Seq is infinite, Why?

Tags:

raku

I'm working on this weeks PerlWChallenge.

You are given an array of integers @A. Write a script to create an array that represents the smaller element to the left of each corresponding index. If none found then use 0.

Here's my approach:

my @A = (7, 8, 3, 12, 10);
my $L = @A.elems - 1;

say gather for 1 .. $L -> $i { take @A[ 0..$i-1 ].grep( * < @A[$i] ).min };

Which kinda works and outputs:

(7 Inf 3 3)

The Infinity obviously comes from the empty grep. Checking:

> raku -e "().min.say"
Inf

But why is the minimum of an empty Seq Infinity? If anything it should be -Infinity. Or zero?

It's probably a good idea to test for the empty sequence anyway.

I ended up using

take .min with @A[ 0..$i-1 ].grep( * < @A[$i] ) or 0

or

take ( @A[ 0..$i-1 ].grep( * < @A[$i] ) or 0 ).min
like image 696
Holli Avatar asked Aug 10 '20 09:08

Holli


2 Answers

Generally, Inf works out quite well in the face of further operations. For example, consider a case where we have a list of lists, and we want to find the minimum across all of them. We can do this:

my @a = [3,1,3], [], [-5,10];
say @a>>.min.min

And it will just work, since (1, Inf, -5).min comes out as -5. Were min to instead have -Inf as its value, then it'd get this wrong. It will also behave reasonably in comparisons, e.g. if @a.min > @b.min { }; by contrast, an undefined value will warn.

like image 106
Jonathan Worthington Avatar answered Oct 21 '22 14:10

Jonathan Worthington


TL;DR say min displays Inf.


min is, or at least behaves like, a reduction.


Per the doc for reduction of a List:

When the list contains no elements, an exception is thrown, unless &with is an operator with a known identity value (e.g., the identity value of infix:<+> is 0).


Per the doc for min:

a comparison Callable can be specified with the named argument :by

by is min's spelling of with.


To easily see the "identity value" of an operator/function, call it without any arguments:

say min # Inf

Imo the underlying issue here is one of many unsolved wide challenges of documenting Raku. Perhaps comments here in this SO about doc would best focus on the narrow topic of solving the problem just for min (and maybe max and minmax).

like image 44
raiph Avatar answered Oct 21 '22 14:10

raiph