Skip to content

Commit

Permalink
Add Any.minmax.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Apr 26, 2010
1 parent f6ec0aa commit 8098bf9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/Any-list.pm
Expand Up @@ -82,6 +82,29 @@ augment class Any {
$max;
}

# CHEAT: this should take an ordering parameter
# And use the FIRST: phaser
multi method minmax($by = { $^a cmp $^b}) {
my $min = +Inf;
my $max = -Inf;
my $first-time = Bool::True;
for @.list {
if $first-time {
$min = $_;
$max = $_;
$first-time = Bool::False;
next;
}
if $by($_, $min) == -1 {
$min = $_;
}
if $by($_, $max) == 1 {
$max = $_;
}
}
($min, $max);
}

#CHEAT: Simplified version which we can hopefully sneak by ng.
multi method pick() {
my @l = @.list.Seq;
Expand Down Expand Up @@ -199,6 +222,7 @@ proto sub grep(Mu $test, *@values) { @values.grep($test); }
proto sub first($test, @values) { @values.first($test); }
proto sub min($by, *@values) { @values.min($by); }
proto sub max($by, *@values) { @values.max($by); }
proto sub minmax($by, *@values) { @values.minmax($by); }
proto sub uniq(@values) { @values.uniq; }
proto sub pick ($num, :$replace, *@values) { @values.pick($num, :$replace); }
proto sub map(&mapper, @values) { @values.map(&mapper); }
Expand Down

0 comments on commit 8098bf9

Please sign in to comment.