Home > Uncategorized > Incomplete Ranges

Incomplete Ranges

Lately, some unhappiness has popped up about Range and it’s incomplete numericaliness. Having just one blogpost about it is clearly not enough, given how big Ranges can be.

say (-∞..∞).elems;
# Cannot .elems a lazy list
  in block <unit> at tmp/2021-03-08.raku line 2629

I don’t quite agree with Rakudo here. There are clearly ∞ elements in that lazy list. This could very well be special-cased.

The argument has been made, that many operators in Raku tell you what type the returned value will have. Is that so? (This question is always silly or unnecessary.)

say (1 + 2&3).WHAT;
# (Junction)

Granted, Junction is quite special. But so are Ranges. Yet, Raku covers the former everywhere but the latter feels uncompleted. Please consider the following code.

multi sub infix:<±>(Numeric \n, Numeric \variance --> Range) {
    (n - variance) .. (n + variance)
}

say 2.6 > 2 ± 0.5;
# True

my @heavy-or-light = 25.6, 50.3, 75.4, 88.8;

@heavy-or-light.map({ $_ ≤ 75 ± 0.5 ?? „$_ is light“ !! „$_ is heavy“ }).say;
# (25.6 is heavy 50.3 is heavy 75.4 is heavy 88.8 is heavy)

To me that looks like it should DWIM. It doesn’t, because &infix:«≤» defaults to coercing to Real and then comparing numerically.

This could easily be fixed by adding a few more multis and I don’t think it would break any production code. We already provide quite a few good tools for scientists. And those scientists do love their error bars — which are ranges. I would love for them to have another reason to use Raku over … that other language.

Categories: Uncategorized
  1. librasteve
    October 25, 2023 at 10:50

    Linking back to my somewhat related blog https://rakujourney.wordpress.com/2023/10/24/raku-home-on-the-range/

    My sense is that these improvements could be made initially in a raku module [Math::Range|Math::Interval]? that exports all the overload operators and then rolled into core around 6.f time. I would like to be able to complete the wikipedia BMI demo (ie. Range * Range) and similar.

    What do you think?

    • October 25, 2023 at 18:42

      I think it needs a lot more thinking. :) The odd thing about ranges is that they can be Numeric or Stringy, and the call is made at runtime. I’m quite sure Raku allows for a solution. A nice solution would be preferable.

  1. October 30, 2023 at 12:54

Leave a comment