Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a simple versions of Any.min and Any.max, and use them to impleme…
…nt re-implement infix:<min> and infix:<max> as list associative operators.
  • Loading branch information
colomon committed Feb 4, 2010
1 parent 135ace1 commit 8aa3b7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/core/Any-list.pm
Expand Up @@ -45,6 +45,42 @@ augment class Any {
}
multi method end() { self.elems - 1; }
# CHEAT: this should take an ordering parameter
# And use the FIRST: phaser
multi method min() {
my $min = +Inf;
my $first-time = Bool::True;
for @.list {
if $first-time {
$min = $_;
$first-time = Bool::False;
next;
}
if $_ before $min {
$min = $_;
}
}
$min;
}

# CHEAT: this should take an ordering parameter
# And use the FIRST: phaser
multi method max() {
my $max = -Inf;
my $first-time = Bool::True;
for @.list {
if $first-time {
$max = $_;
$first-time = Bool::False;
next;
}
if $_ after $max {
$max = $_;
}
}
$max;
}
}

our proto sub join (Str $separator = '', *@values) { @values.join($separator); }
Expand Down
8 changes: 4 additions & 4 deletions src/core/operators.pm
Expand Up @@ -111,10 +111,10 @@ our multi infix:<?^>($a, $b) {
?pir::bxor__III($a, $b)
}

our multi infix:<min>($a, $b) {
$a before $b ?? $a !! $b;
our multi infix:<min>(*@args) {
@args.min;
}

our multi infix:<max>($a, $b) {
$a after $b ?? $a !! $b;
our multi infix:<max>(*@args) {
@args.max;
}

0 comments on commit 8aa3b7a

Please sign in to comment.