Created
March 15, 2015 10:53
-
-
Save jnthn/aabc5e604dbb75fd0d69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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