New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of List.min and List.max #123
Labels
Comments
From @bacekHello. There is implementation of List.min and List.max -- |
From @bacekminmax.diffdiff --git a/languages/perl6/src/classes/List.pir b/languages/perl6/src/classes/List.pir
index b9b5107..3b6fe2a 100644
--- a/languages/perl6/src/classes/List.pir
+++ b/languages/perl6/src/classes/List.pir
@@ -684,6 +686,107 @@
.end
+=item min()
+
+Return minimum element in list
+
+=cut
+
+.sub 'min' :method :multi('ResizablePMCArray')
+ .param pmc by :optional
+ .param int has_by :opt_flag
+ .local pmc elem, res, iter
+
+ if has_by goto start
+ by = get_hll_global 'infix:<=>'
+ start:
+ iter = self.'iterator'()
+ if iter goto do_work
+ res = new 'Failure'
+ goto done
+
+ do_work:
+ res = shift iter
+ loop:
+ unless iter goto done
+ elem = shift iter
+ $I0 = by(elem, res)
+ unless $I0 < 0 goto loop
+ res = elem
+ goto loop
+
+ done:
+ .return(res)
+.end
+
+.namespace []
+.sub 'min' :multi(Closure)
+ .param pmc by
+ .param pmc values :slurpy
+ $P0 = values.'min'(by)
+ .return ($P0)
+.end
+
+.sub 'min' :multi()
+ .param pmc values :slurpy
+ .local pmc by
+ by = get_hll_global 'infix:<=>'
+ $P0 = values.'min'(by)
+ .return ($P0)
+.end
+
+
+.namespace ['List']
+=item max()
+
+Return maximum element in list
+
+=cut
+
+.sub 'max' :method :multi('ResizablePMCArray')
+ .param pmc by :optional
+ .param int has_by :opt_flag
+ .local pmc elem, res, iter
+
+ if has_by goto start
+ by = get_hll_global 'infix:<=>'
+ start:
+ iter = self.'iterator'()
+ if iter goto do_work
+ res = new 'Failure'
+ goto done
+
+ do_work:
+ res = shift iter
+ loop:
+ unless iter goto done
+ elem = shift iter
+ $I0 = by(elem, res)
+ unless $I0 > 0 goto loop
+ res = elem
+ goto loop
+
+ done:
+ .return(res)
+.end
+
+.namespace []
+.sub 'max' :multi(Closure)
+ .param pmc by
+ .param pmc values :slurpy
+ $P0 = values.'max'(by)
+ .return ($P0)
+.end
+
+.sub 'max' :multi()
+ .param pmc values :slurpy
+ .local pmc by
+ by = get_hll_global 'infix:<=>'
+ $P0 = values.'max'(by)
+ .return ($P0)
+.end
+
+
=back
=head1 Functions
|
From @jnthnApplied in r28259, thanks! |
The RT System itself - Status changed from 'new' to 'open' |
@jnthn - Status changed from 'open' to 'resolved' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Migrated from rt.perl.org#55482 (status was 'resolved')
Searchable as RT55482$
The text was updated successfully, but these errors were encountered: