You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Marty Neal (JIRA)" <ji...@apache.org> on 2017/07/27 11:50:00 UTC

[jira] [Created] (GROOVY-8271) Take/TakeRight methods on iterator needlessly calls hasNext for one too many elements

Marty Neal created GROOVY-8271:
----------------------------------

             Summary: Take/TakeRight methods on iterator needlessly calls hasNext for one too many elements
                 Key: GROOVY-8271
                 URL: https://issues.apache.org/jira/browse/GROOVY-8271
             Project: Groovy
          Issue Type: Bug
          Components: groovy-jdk
    Affects Versions: 2.4.12
            Reporter: Marty Neal
            Priority: Minor


I expect the {{TakeIterator}} to only call the {{hasNext()}} method for the delegate a maximum of {{N}} times where {{N}} is the number of elements to take.  If the iterator represents some expensive operations, or an unexhausted infinite stream that has exactly {{N}} elements available, the {{TakeIterator}} will block waiting for the {{Nth+1}} element only to return {{N}} of them.

In short, change the {{TakeIterator}}'s {{hasNext}} function from

            {{return delegate.hasNext() && num > 0;}}
to

            {{return num > 0 && delegate.hasNext();}}




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)