Skip to content

Commit

Permalink
Introducing PredictiveIterator role
Browse files Browse the repository at this point in the history
After some discussion with Jonathan after the SPW, he felt it was better to
move the "count-only" and "bool-only" functionality into a separate role,
which could then be used to smartmatch / MMD on, rather than having to see
whether it is possible to call a method before calling it.

This commit creates that role, with "bool-only" defaulting to booleanizing
the result of "count-only".
  • Loading branch information
lizmat committed Sep 9, 2018
1 parent 18a6968 commit 3ffacb9
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/core/Iterator.pm6
Expand Up @@ -96,18 +96,6 @@ my role Iterator {
)
}

# The optional "count-only" method in an Iterator class returns the number
# of elements that the iterator would be able to still generate but
# *without* actually generating any values. This can e.g. be the case
# when an iterator for all the characters in a string, of which the number
# elements is already known and the number of values generated as well.
# method count-only(--> Int:D) { ... }

# The optional "bool-only" method in an Iterator class returns a Bool
# to indicate whether the generator is (still) able to generate *any*
# value, *without* actually generating any value.
# method bool-only(--> Bool:D) { ... }

# 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 All @@ -126,4 +114,21 @@ my role Iterator {
method is-lazy(--> False) { }
}

# The PredictiveIterator role is a refinement of the Iterator role for those
# cases when the number of values to be generated (still) can be determined
# *without* actually generating those values.
my role PredictiveIterator does Iterator {
# The "count-only" method in a PredictiveIterator class returns the number
# of elements that the iterator would still be able to generate but
# *without* actually generating any values. This can e.g. be the case
# when an iterator for all the characters in a string, of which the number
# elements is already known and the number of values generated as well.
method count-only(--> Int:D) { ... }

# The "bool-only" method in a PredictiveIterator class returns a Bool
# to indicate whether the generator is (still) able to generate at least
# one value, *without* actually generating that value.
method bool-only(--> Bool:D) { nqp::hllbool(self.count-only) }
}

# vim: ft=perl6 expandtab sw=4

0 comments on commit 3ffacb9

Please sign in to comment.