Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
You want an iterator? Just ask for one!
There were many places in the code doing a ternary checking for
iterability, and if not, do .list.iterator instead of just .iterator .
By putting an iterator method in Any, all of these ternaries can
go, leading to less code to execute and better inlineability.  Of
course, since Nil is no longer Any, it needed its own iterator method.
  • Loading branch information
lizmat committed Nov 10, 2015
1 parent ed10656 commit 326a744
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 67 deletions.
41 changes: 18 additions & 23 deletions src/core/Any-iterable-methods.pm
Expand Up @@ -9,23 +9,18 @@ class X::Cannot::Lazy { ... }
# work on by doing a .list coercion.
use MONKEY-TYPING;
augment class Any {
sub as-iterator(\iterablish) is raw {
iterablish.DEFINITE && nqp::istype(iterablish, Iterable)
?? iterablish.iterator
!! iterablish.list.iterator
}

proto method map(|) is nodal { * }

multi method map(\SELF: █; :$label, :$item) {
sequential-map(as-iterator($item ?? (SELF,) !! SELF), &block, :$label);
sequential-map(($item ?? (SELF,) !! SELF).iterator, &block, :$label);
}

multi method map(HyperIterable:D: █; :$label) {
# For now we only know how to parallelize when we've only one input
# value needed per block. For the rest, fall back to sequential.
if &block.count != 1 {
sequential-map(as-iterator(self), &block, :$label)
sequential-map(self.iterator, &block, :$label)
}
else {
HyperSeq.new(class :: does HyperIterator {
Expand Down Expand Up @@ -309,7 +304,7 @@ augment class Any {
has Mu $!test;
has int $!index;
method BUILD(\list,Mu \test) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!test := test;
$!index = -1;
self
Expand Down Expand Up @@ -351,7 +346,7 @@ augment class Any {
has int $!index;
has Mu $!value;
method BUILD(\list,Mu \test) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!test := test;
$!index = -1;
self
Expand Down Expand Up @@ -420,7 +415,7 @@ augment class Any {
has Mu $!test;
has int $!index;
method BUILD(\list,Mu \test) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!test := test;
$!index = -1;
self
Expand Down Expand Up @@ -460,7 +455,7 @@ augment class Any {
has Mu $!iter;
has Mu $!test;
method BUILD(\list,Mu \test) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!test := test;
self
}
Expand Down Expand Up @@ -709,7 +704,7 @@ augment class Any {
Nil
}
else {
my $iter := as-iterator(self);
my $iter := self.iterator;
my int $index;
$index = $index + 1
until ($_ := $iter.pull-one) =:= IterationEnd || .match($test);
Expand All @@ -730,7 +725,7 @@ augment class Any {
Nil
}
else {
my $iter := as-iterator(self);
my $iter := self.iterator;
my int $index;
$index = $index + 1
until ($_ := $iter.pull-one) =:= IterationEnd || $test($_);
Expand All @@ -751,7 +746,7 @@ augment class Any {
Nil
}
else {
my $iter := as-iterator(self);
my $iter := self.iterator;
my int $index;
$index = $index + 1
until (($_ := $iter.pull-one) =:= IterationEnd) || $test.ACCEPTS($_);
Expand Down Expand Up @@ -901,7 +896,7 @@ augment class Any {

method sort(&by = &infix:<cmp>) is nodal {
# Obtain all the things to sort.
my \iter = as-iterator(self);
my \iter = self.iterator;
my \sort-buffer = IterationBuffer.new;
unless iter.push-until-lazy(sort-buffer) =:= IterationEnd {
fail X::Cannot::Lazy.new(:action<sort>);
Expand Down Expand Up @@ -964,7 +959,7 @@ augment class Any {
has Mu $!iter;
has $!seen;
method BUILD(\list) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!seen := nqp::hash();
self
}
Expand Down Expand Up @@ -1030,7 +1025,7 @@ augment class Any {
has &!as;
has $!seen;
method BUILD(\list, &!as) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!seen := nqp::hash();
self
}
Expand Down Expand Up @@ -1099,7 +1094,7 @@ augment class Any {
has Mu $!iter;
has $!seen;
method BUILD(\list) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!seen := nqp::hash();
self
}
Expand Down Expand Up @@ -1164,7 +1159,7 @@ augment class Any {
has &!as;
has $!seen;
method BUILD(\list, &!as) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!seen := nqp::hash();
self
}
Expand Down Expand Up @@ -1235,7 +1230,7 @@ augment class Any {
has $!last;
has int $!first;
method BUILD(\list, &!as, &!with) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!first = 1;
self
}
Expand Down Expand Up @@ -1288,7 +1283,7 @@ augment class Any {
has Mu $!last;
has int $!first;
method BUILD(\list, &!with) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!first = 1;
self
}
Expand Down Expand Up @@ -1362,7 +1357,7 @@ augment class Any {
has Mu $!iter;
has int $!todo;
method BUILD(\list,\todo) {
$!iter = as-iterator(list);
$!iter = list.iterator;
$!todo = todo;
self
}
Expand All @@ -1384,7 +1379,7 @@ augment class Any {
has int $!todo;
has int $!index;
method BUILD(\list,\size) {
$!iter = as-iterator(list);
$!iter = list.iterator;
X::Cannot::Lazy.new(:action<tail>).throw if $!iter.is-lazy;

$!lastn := nqp::list();
Expand Down
3 changes: 3 additions & 0 deletions src/core/Any.pm
Expand Up @@ -110,6 +110,9 @@ my class Any { # declared in BOOTSTRAP
multi method roll() { self.list.roll }
multi method roll($n) { self.list.roll($n) }

proto method iterator(|) { * }
multi method iterator(Any:D:) { self.list.iterator }

proto method classify(|) is nodal { * }
multi method classify() {
die "Must specify something to classify with, a Callable, Hash or List";
Expand Down
8 changes: 2 additions & 6 deletions src/core/List.pm
Expand Up @@ -1152,11 +1152,7 @@ multi sub infix:<Z>(+lol) {
eager my @l = (^$arity).map: -> $i {
my \elem = lol[$i];
$laze = False unless elem.is-lazy;
Rakudo::Internals::WhateverIterator.new(
nqp::istype(elem, Iterable)
?? elem.iterator
!! elem.list.iterator
)
Rakudo::Internals::WhateverIterator.new(elem.iterator)
};

gather {
Expand All @@ -1176,7 +1172,7 @@ multi sub infix:<Z>(+lol) {
my &zip := &infix:<Z>;

sub roundrobin(**@lol) {
my @iters = @lol.map: { nqp::istype($_, Iterable) ?? .iterator !! .list.iterator };
my @iters = @lol.map: *.iterator;
my $laze = so any(@lol).is-lazy;
gather {
while @iters {
Expand Down
4 changes: 1 addition & 3 deletions src/core/Map.pm
Expand Up @@ -182,9 +182,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
}

method STORE(\to_store) {
my \iter = nqp::istype(to_store, Iterable)
?? to_store.iterator
!! to_store.list.iterator;
my \iter = to_store.iterator;
$!storage := nqp::hash();
until (my Mu $x := iter.pull-one) =:= IterationEnd {
if nqp::istype($x,Pair) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/Nil.pm
Expand Up @@ -19,6 +19,8 @@ my class Nil is Cool { # declared in BOOTSTRAP
method unshift(*@) is nodal { die "Attempted to unshift to {self.gist}." }
method prepend(*@) is nodal { die "Attempted to prepend to {self.gist}." }
method FALLBACK(*@) { Nil }

method iterator() { self.list.iterator }
}

# vim: ft=perl6 expandtab sw=4
27 changes: 6 additions & 21 deletions src/core/metaops.pm
Expand Up @@ -25,10 +25,7 @@ sub METAOP_CROSS(\op, &reduce) {
my $laze = False;
my @loi = eager for lol -> \elem {
$laze = True if elem.is-lazy;
nqp::iscont(elem) ?? (elem,).iterator
!! nqp::istype(elem, Iterable)
?? elem.iterator
!! elem.list.iterator;
nqp::iscont(elem) ?? (elem,).iterator !! elem.iterator;
}
my Mu $cache := nqp::list();
my int $i = 0;
Expand Down Expand Up @@ -78,11 +75,7 @@ sub METAOP_ZIP(\op, &reduce) {
my $laze = True;
my @loi = eager for lol -> \elem {
$laze = False unless elem.is-lazy;
Rakudo::Internals::WhateverIterator.new(
nqp::istype(elem, Iterable)
?? elem.iterator
!! elem.list.iterator
)
Rakudo::Internals::WhateverIterator.new(elem.iterator)
}
gather {
loop {
Expand All @@ -108,9 +101,7 @@ multi sub METAOP_REDUCE_LEFT(\op, \triangle) {
my $ :=
#?endif
sub (+values) {
my \source = nqp::istype(values, Iterable)
?? values.iterator
!! values.list.iterator;
my \source = values.iterator;

my \first = source.pull-one;
return () if first =:= IterationEnd;
Expand All @@ -135,9 +126,7 @@ multi sub METAOP_REDUCE_LEFT(\op, \triangle) {
my $ :=
#?endif
sub (+values) {
my \source = nqp::istype(values, Iterable)
?? values.iterator
!! values.list.iterator;
my \source = values.iterator;

my \first = source.pull-one;
return () if first =:= IterationEnd;
Expand All @@ -160,9 +149,7 @@ multi sub METAOP_REDUCE_LEFT(\op) {
my $ :=
#?endif
sub (+values) {
my \iter = nqp::istype(values, Iterable)
?? values.iterator
!! values.list.iterator;
my \iter = values.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;

Expand All @@ -185,9 +172,7 @@ multi sub METAOP_REDUCE_LEFT(\op) {
my $ :=
#?endif
sub (+values) {
my \iter = nqp::istype(values, Iterable)
?? values.iterator
!! values.list.iterator;
my \iter = values.iterator;
my \first = iter.pull-one;
return op.() if first =:= IterationEnd;

Expand Down
18 changes: 4 additions & 14 deletions src/core/operators.pm
Expand Up @@ -104,7 +104,7 @@ multi sub infix:<but>(Mu:U \obj, **@roles) {

sub SEQUENCE(\left, Mu \right, :$exclude_end) {
my \righti := nqp::iscont(right)
?? nqp::istype(right, Iterable) ?? right.iterator !! right.list.iterator
?? right.iterator
!! [right].iterator;
my $endpoint := righti.pull-one;
X::Cannot::Empty.new(:action('get sequence endpoint'), :what('list (use * or :!elems instead?)')).throw
Expand Down Expand Up @@ -162,7 +162,7 @@ sub SEQUENCE(\left, Mu \right, :$exclude_end) {
}

my \gathered = GATHER({
my \lefti := nqp::istype(left, Iterable) ?? left.iterator !! left.list.iterator;
my \lefti := left.iterator;
my $value;
my $code;
my $stop;
Expand Down Expand Up @@ -442,21 +442,11 @@ multi sub infix:<...>(|lol) {
my int $i = 0;
my int $m = +@lol - 1;
while $i <= $m {
if @lol[$i] ~~ Iterable {
@seq[$i] := @lol[$i].iterator;
}
else {
@seq[$i] := @lol[$i].list.iterator;
}
@seq[$i] := @lol[$i].iterator;
if $i {
@end[$i-1] := @seq[$i].pull-one;
if @end[$i-1] ~~ Numeric | Stringy {
if @lol[$i] ~~ Iterable {
@seq[$i] := @lol[$i].iterator;
}
else {
@seq[$i] := @lol[$i].list.iterator;
}
@seq[$i] := @lol[$i].iterator;
@excl[$i-1] = True;
}
}
Expand Down

0 comments on commit 326a744

Please sign in to comment.