Skip to main content
Retitle stating purpose rather than mechanism
Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

User defined Range check operator in Raku

Source Link
hsmyers
  • 153
  • 1
  • 5

User defined operator in Raku

I'd like to improve this operator code if I can. I don't know enough Raku to write idiomatically (or any other way for that matter), but suggestions along those lines would be good also.

use v6d;

sub circumfix:<α ω>( ( $a, $b, $c ) ) {
    if ( $a.WHAT ~~ (Int) ) {
        so ( $a >= $b & $a <= $c );
    }
    else {
        my @b;
        for $a { @b.push( ( $_ >= $b & $_ <= $c ).so ) };
        so (False) ∈ @b;
    }
};

if (α 5, 0, 10 ω) {
    "Pass".say;
}
else {
    "Fail".say;
}
if (α ( 5, 7, 11 ), 0, 10 ω) {
    "Pass".say;
}
else {
    "Fail".say;
}

This does a range check on a variable or variables, between/bookended by $b (lower) and $c (upper). I wonder about using some sort of signature check paired with multi as a cleaner approach?