Skip to content

Commit

Permalink
Rename Range.minmax to Range.bounds. Change Any.minmax to return a Ra…
Browse files Browse the repository at this point in the history
…nge, and take Ranges as arguments, too. (Though note that in many conditions the Range is expanded to a list going in, which muddles things.) Add minmax operator which uses Any.minmax internally.
  • Loading branch information
colomon committed Apr 27, 2010
1 parent a27dfef commit 0a04ef3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Perl6/Grammar.pm
Expand Up @@ -1687,6 +1687,7 @@ token infix:sym<^^> { <sym> <O('%tight_or, :pasttype<xor>')> }
token infix:sym<//> { <sym> <O('%tight_or, :assoc<left>, :pasttype<def_or>')> }
token infix:sym<min> { <sym> <O('%tight_or')> }
token infix:sym<max> { <sym> <O('%tight_or')> }
token infix:sym<minmax> { <sym> <O('%tight_or')> }

token infix:sym<?? !!> {
'??'
Expand Down
31 changes: 30 additions & 1 deletion src/core/Any-list.pm
@@ -1,3 +1,5 @@
class Range { ... }

augment class Any {

our Str multi method join($separator = '') {
Expand Down Expand Up @@ -87,8 +89,30 @@ augment class Any {
multi method minmax($by = { $^a cmp $^b}) {
my $min = +Inf;
my $max = -Inf;
my $excludes_min = Bool::False;
my $excludes_max = Bool::False;

my $first-time = Bool::True;
for @.list {
when Range {
if $first-time {
$min = $_.min;
$max = $_.max;
$excludes_min = $_.excludes_min;
$excludes_max = $_.excludes_max;
$first-time = Bool::False;
next;
}
if $by($_.min, $min) == -1 {
$min = $_;
$excludes_min = $_.excludes_min;
}
if $by($_.max, $max) == 1 {
$max = $_;
$excludes_max = $_.excludes_max;
}
}

if $first-time {
$min = $_;
$max = $_;
Expand All @@ -97,12 +121,17 @@ augment class Any {
}
if $by($_, $min) == -1 {
$min = $_;
$excludes_min = Bool::False;
}
if $by($_, $max) == 1 {
$max = $_;
$excludes_max = Bool::False;
}
}
($min, $max);
Range.new($min,
$max,
:excludes_min($excludes_min),
:excludes_max($excludes_max));
}

#CHEAT: Simplified version which we can hopefully sneak by ng.
Expand Down
2 changes: 1 addition & 1 deletion src/core/Range.pm
Expand Up @@ -38,7 +38,7 @@ class Range is Iterable {
?(self!min_test($topic) && self!max_test($topic))
}

multi method minmax() {
multi method bounds() {
($.min, $.max)
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/operators.pm
Expand Up @@ -172,6 +172,10 @@ our multi infix:<max>(*@args) {
@args.max;
}

our multi infix:<minmax>(*@args) {
@args.minmax;
}

our multi infix=>»($key, Mu $value) {
Pair.new(key => $key, value => $value);
}
Expand Down

0 comments on commit 0a04ef3

Please sign in to comment.