Skip to content

Commit

Permalink
Add .infinite as a BUILD-time attribute
Browse files Browse the repository at this point in the history
Also fixes the (-Inf..42) case (previously, this was not infinite)
  • Loading branch information
lizmat committed Apr 16, 2015
1 parent 54fbdfe commit 67df04e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/core/Range.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ my class Range is Iterable is Cool does Positional {
has $.max;
has $.excludes-min;
has $.excludes-max;
has $.infinite;

proto method new(|) { * }
# The order of "method new" declarations matters here, to ensure
Expand All @@ -17,28 +18,35 @@ my class Range is Iterable is Cool does Positional {
X::Range::InvalidArg.new(:got($max)).throw;
}
multi method new(Whatever $min, Whatever $max, :$excludes-min, :$excludes-max) {
nqp::create(self).BUILD(-Inf, Inf, $excludes-min, $excludes-max)
nqp::create(self).BUILD(-Inf, Inf, $excludes-min, $excludes-max, True);
}
multi method new(Whatever $min, $max, :$excludes-min, :$excludes-max) {
nqp::create(self).BUILD(-Inf, $max, $excludes-min, $excludes-max)
nqp::create(self).BUILD(-Inf, $max, $excludes-min, $excludes-max, True);
}
multi method new($min, Whatever $max, :$excludes-min, :$excludes-max) {
nqp::create(self).BUILD($min, Inf, $excludes-min, $excludes-max)
nqp::create(self).BUILD($min, Inf, $excludes-min, $excludes-max, True);
}
multi method new(Real $min, $max, :$excludes-min, :$excludes-max) {
nqp::create(self).BUILD($min, $max.Real, $excludes-min, $excludes-max)
multi method new(Real $min, Real() $max, :$excludes-min, :$excludes-max) {
nqp::create(self).BUILD(
$min,
$max,
$excludes-min,
$excludes-max,
$max == Inf || $min == -Inf,
);
}
multi method new($min is copy, $max, :$excludes-min, :$excludes-max) {
$min = +$min
if nqp::istype($min,List) || nqp::istype($min,Match) || nqp::istype($min,Parcel);
nqp::create(self).BUILD($min, $max, $excludes-min, $excludes-max)
nqp::create(self).BUILD($min, $max, $excludes-min, $excludes-max);
}

submethod BUILD(
$!min,
$!max,
Bool() $!excludes-min,
Bool() $!excludes-max,
Bool $!infinite = False,
) {
self;
}
Expand All @@ -52,10 +60,6 @@ my class Range is Iterable is Cool does Positional {
~ $!max;
}

multi method infinite(Range:D:) {
nqp::p6bool(nqp::istype($!max, Num)) && $!max eq 'Inf';
}

method flat() { nqp::p6list(nqp::list(self), List, Bool::True) }
method iterator() { self }
method list() { self.flat }
Expand Down

0 comments on commit 67df04e

Please sign in to comment.