Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created March 22, 2017 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/e6dd979e9291eb6be6d041e53700cfb4 to your computer and use it in GitHub Desktop.
Save zoffixznet/e6dd979e9291eb6be6d041e53700cfb4 to your computer and use it in GitHub Desktop.
use MONKEY;
augment class List {
multi method maxpairs3(Any:D:) {
my @found;
for self.pairs {
my $value := .value;
state $max = $value;
nqp::if(
nqp::iseq_i( (my $cmp := $value cmp $max), 1 ),
nqp::stmts((@found = $_), ($max = $value)),
nqp::if(
nqp::iseq_i($cmp, 0),
@found.push: $_
)
)
}
Seq.new(@found.iterator)
}
method maxpairs2 {
my @found;
for self.pairs {
my $value := .value;
state $max = $value;
nqp::if(
nqp::iseq_i( (my $cmp := $value cmp $max), 1 ),
nqp::stmts((@found = $_), ($max = $value)),
nqp::if(
nqp::iseq_i($cmp, 0),
@found.push: $_;
)
)
}
Seq.new(@found.iterator)
}
}
dd (<a b c>, 4, 5, 6).maxpairs2;
dd (<a b c>, 4, 5, 6).maxpairs3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment