Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement general Range iterator using .pred and .succ
Spec says the Range iterator should just the .succ method to get the next
element. So that is what we do if we have no specialized iterator.
  • Loading branch information
niner committed Aug 21, 2015
1 parent 0b1722b commit 05e46f5
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/core/Range.pm
Expand Up @@ -165,9 +165,26 @@ my class Range is Cool does Iterable does Positional {
}.new($value)
}

# XXX do other cases here
# General case according to spec
else {
nqp::die('Range iterator NYI')
class :: does Iterator {
has $!i;
has $!e;

method new($i is copy, $e is copy) {
my \iter = self.CREATE;
nqp::bindattr(iter, self, '$!i', $i);
nqp::bindattr(iter, self, '$!e', $e);
iter
}

method pull-one() {
$!i.=succ;
$!i <= $!e ?? $!i !! IterationEnd
}

method is-lazy() { True }
}.new($value, $!excludes-max ?? $!max.pred !! $!max)
}
}
multi method list(Range:D:) { List.from-iterator(self.iterator) }
Expand Down

0 comments on commit 05e46f5

Please sign in to comment.