Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add naive implementation of Iterator.count-only
The idea being that if we have a situation like "foo".IO.lines.elems, we
can call "count-only" on the iterator, which would not bother pushing anything
to the target, but do a much faster low level count.  *AND* would allow any
consumer class to optimize that case even further (like now with the :count
adverb on IO::Handle.lines).
  • Loading branch information
lizmat committed Aug 16, 2015
1 parent ac9d315 commit 9c5d5da
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/Iterator.pm
Expand Up @@ -67,6 +67,17 @@ my role Iterator {
!! self.push-all($target)
}

# Does not push anything but consumes the iterator to find out the number
# items that were generated, and returns that number. Intended to be used
# in situations such as "foo".IO.lines.elems, where we're only interested
# in the number of lines in the file, rather than the contents of the
# lines.
method count-only($) {
my int $i = 0;
$i = $i + 1 until self.pull-one() =:= IterationEnd;
$i
}

# Consumes all of the values in the iterator for their side-effects only.
# May be overridden by iterators to either warn about use of things in
# sink context that should not be used that way, or to process things in
Expand Down

0 comments on commit 9c5d5da

Please sign in to comment.