Navigation Menu

Skip to content

Commit

Permalink
A few more Range updates for Whatever, .from, .to, .bounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Aug 15, 2010
1 parent 8455499 commit e8f2c6b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/core/Range.pm
Expand Up @@ -22,18 +22,28 @@ class Range is Iterable does Positional {
self.bless(*, :min($min.Numeric), :$max, :$excludes_min, :$excludes_max);
}

multi method new(Whatever $min, Real $max,
multi method new(Whatever $min, $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.new(-Inf, $max, :$excludes_min, :$excludes_max);
}

multi method new(Real $min, Whatever $max,
multi method new($min, Whatever $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.new($min, +Inf, :$excludes_min, :$excludes_max);
}

multi method new(Whatever $min, Whatever $max,
Bool :$excludes_min = Bool::False,
Bool :$excludes_max = Bool::False) {
self.new(-Inf, +Inf, :$excludes_min, :$excludes_max);
}

multi method bounds() { ($.min, $.max) }
multi method from() { $.min; }
multi method to() { $.max; }

multi method iterator() {
RangeIter.new(:value($!excludes_min ?? $!min.succ !! $!min),
:$!max, :$!excludes_max);
Expand All @@ -48,11 +58,27 @@ class Range is Iterable does Positional {
).join('');
}

our Bool multi method ACCEPTS(Range $topic) {
?(($.min eqv $topic.min)
&& ($.max eqv $topic.max)
&& ($.excludes_min == $topic.excludes_min)
&& ($.excludes_max == $topic.excludes_min));
}

our Bool multi method ACCEPTS($topic) {
?(self!min_test($topic) && self!max_test($topic))
}

multi method postcircumfix:<[ ]>(\$parcel) { self.Seq[$parcel]; }

my Bool multi method !max_test($topic) {
$topic before $.max || (!$.excludes_max && !($topic after $.max));
}

my Bool multi method !min_test($topic) {
$.min before $topic || (!$.excludes_min && !($.min after $topic));
}

}


Expand Down
2 changes: 2 additions & 0 deletions src/core/RangeIter.pm
Expand Up @@ -4,6 +4,8 @@ class RangeIter is Iterator {
has $!excludes_max;
has $!reify;

method infinite() { $!max eqv Inf }

method reify() {
Q:PIR {
.local pmc self, reify, value, max, excl_max
Expand Down

0 comments on commit e8f2c6b

Please sign in to comment.