Skip to content

Commit

Permalink
Simplify the Map.kv iterator
Browse files Browse the repository at this point in the history
Using the now existing roles/classes/iterators
  • Loading branch information
lizmat committed Sep 8, 2018
1 parent 3b61d4f commit 6977973
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/core/Map.pm6
Expand Up @@ -211,39 +211,24 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
multi method keys(Map:D:) { Seq.new(Rakudo::Iterator.Mappy-keys(self)) } multi method keys(Map:D:) { Seq.new(Rakudo::Iterator.Mappy-keys(self)) }
multi method values(Map:D:) { Seq.new(Rakudo::Iterator.Mappy-values(self)) } multi method values(Map:D:) { Seq.new(Rakudo::Iterator.Mappy-values(self)) }


my class KV does Rakudo::Iterator::Mappy { my class KV does Rakudo::Iterator::Mappy-kv-from-pairs {
has int $!on-value;

method pull-one() is raw { method pull-one() is raw {
nqp::if( nqp::if(
$!on-value, $!on,
nqp::stmts( nqp::stmts(
($!on-value = 0), ($!on= 0),
nqp::iterval($!iter) nqp::iterval($!iter)
), ),
nqp::if( nqp::if(
$!iter, $!iter,
nqp::stmts( nqp::stmts(
($!on-value = 1), ($!on= 1),
nqp::iterkey_s(nqp::shift($!iter)) nqp::iterkey_s(nqp::shift($!iter))
), ),
IterationEnd IterationEnd
) )
) )
} }
method skip-one() {
nqp::if(
$!on-value,
nqp::not_i($!on-value = 0), # skipped a value
nqp::if(
$!iter, # if false, we didn't skip
nqp::stmts( # skipped a key
nqp::shift($!iter),
($!on-value = 1)
)
)
)
}
method push-all($target --> IterationEnd) { method push-all($target --> IterationEnd) {
nqp::while( # doesn't sink nqp::while( # doesn't sink
$!iter, $!iter,
Expand Down

0 comments on commit 6977973

Please sign in to comment.