Skip to content

Commit

Permalink
re-implement List.min in Perl 6; Patch courtesy by bacek++
Browse files Browse the repository at this point in the history
Closes RT #63712
  • Loading branch information
moritz committed Mar 8, 2009
1 parent a6bd3a0 commit a7214ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 47 deletions.
47 changes: 0 additions & 47 deletions src/builtins/any-list.pir
Expand Up @@ -168,53 +168,6 @@ Return a List with the keys of the invocant.
.return (result)
.end

=item min

=cut

.namespace []
.sub 'min' :multi()
.param pmc values :slurpy
.local pmc by
by = get_hll_global 'infix:cmp'
unless values goto have_by
$P0 = values[0]
$I0 = isa $P0, 'Sub'
unless $I0 goto have_by
by = shift values
have_by:
.tailcall values.'min'(by)
.end


.namespace ['Any']
.sub 'min' :method :multi(_)
.param pmc by :optional
.param int has_by :opt_flag
if has_by goto have_by
by = get_hll_global 'infix:cmp'
have_by:

.local pmc it, result
$P0 = self.'list'()
it = $P0.'iterator'()
unless it goto fail
result = shift it
loop:
unless it goto done
$P0 = shift it
$I0 = by($P0, result)
unless $I0 < 0 goto loop
result = $P0
goto loop
fail:
.local num failres
failres = "+Inf"
.return (failres)
done:
.return (result)
.end


.namespace []
.sub 'max' :multi()
Expand Down
19 changes: 19 additions & 0 deletions src/setting/Any-list.pm
Expand Up @@ -22,6 +22,20 @@ class Any is also {
($value,).map: &expr
}

# RT #63700 - parse failed on &infix:<cmp>
our Array multi method min( $values: Code $by = sub { $^a cmp $^b } ) {
my @list = $values.list;
return +Inf unless @list.elems;
my $res = @list.shift;
for @list -> $x {
if (&$by($res, $x) > 0) {
$res = $x;
}
}
$res;
};


our List multi method pairs(@values: *@indices) {
gather {
for (@values.keys Z @values) -> $key, $val is rw {
Expand All @@ -44,4 +58,9 @@ our List multi pairs(@values, *@indices) {
@values.pairs(@indices)
}

our List multi min(*@values) {
my $by = @values[0] ~~ Code ?? shift @values !! sub { $^a cmp $^b };
@values.min($by);
}

# vim: ft=perl6

0 comments on commit a7214ac

Please sign in to comment.