Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow shifting and scaling of Range values
Since it's actually kind of difficult to do correctly otherwise and
maintain endpoint exclusions, we now allow basic arithmetic operations
on a Range to clone the Range and modify the endpoints in order to shift
an interval arithmetically or expand/contract it geometrically.
  • Loading branch information
TimToady committed Sep 28, 2015
1 parent a41b719 commit 59b5a4b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/core/Range.pm
Expand Up @@ -284,6 +284,13 @@ my class Range is Cool does Iterable does Positional {
my $floor := $diff.floor;
$floor + 1 - ($floor == $diff ?? $.excludes-max !! 0);
}

method clone-with-op(&op, $value) {
my \SELF = self.clone;
nqp::bindattr(SELF, Range, '$!min', $!min [&op] $value);
nqp::bindattr(SELF, Range, '$!max', $!max [&op] $value);
SELF;
}
}

sub infix:<..>($min, $max) is pure {
Expand All @@ -309,4 +316,11 @@ multi sub infix:<eqv>(Range:D \a, Range:D \b) {
&& a.excludes-max eqv b.excludes-max
}

multi sub infix:<+>(Range:D \a, Real:D \b) { a.clone-with-op(&[+], b) }
multi sub infix:<+>(Real:D \a, Range:D \b) { b.clone-with-op(&[+], a) }
multi sub infix:<->(Range:D \a, Real:D \b) { a.clone-with-op(&[-], b) }
multi sub infix:<*>(Range:D \a, Real:D \b) { a.clone-with-op(&[*], b) }
multi sub infix:<*>(Real:D \a, Range:D \b) { b.clone-with-op(&[*], a) }
multi sub infix:</>(Range:D \a, Real:D \b) { a.clone-with-op(&[/], b) }

# vim: ft=perl6 expandtab sw=4

0 comments on commit 59b5a4b

Please sign in to comment.