Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created March 15, 2015 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnthn/aabc5e604dbb75fd0d69 to your computer and use it in GitHub Desktop.
Save jnthn/aabc5e604dbb75fd0d69 to your computer and use it in GitHub Desktop.
class MultiRange {
has Range @.ranges;
method new(**@ranges) {
self.bless(:@ranges);
}
multi method ACCEPTS(MultiRange:D: $value) {
$value ~~ any(@!ranges)
}
method min() {
@!ranges>>.min.min
}
method max() {
@!ranges>>.max.max
}
method list() {
# There's a more efficient way to do this... :-)
gather {
for $.min .. $.max -> $maybe {
take $maybe if $maybe ~~ self;
}
}
}
}
{
use Test;
my $mr = MultiRange.new(1..5, 2..4, 3..8, 10..Inf);
nok 0 ~~ $mr;
for 1..8 -> $test {
ok $test ~~ $mr;
}
nok 9 ~~ $mr;
ok 10 ~~ $mr;
ok 11 ~~ $mr;
ok 42 ~~ $mr;
is $mr.min, 1;
is $mr.max, Inf;
my @l := $mr.list;
is @l[^11], (1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment