Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#203 Please supply an \"Iterator\" for any collection

v3.2.0
closed
nobody
Classes (154)
5
2012-08-14
2007-07-07
No

As pointed out in the "Cursor" RFE I have come up with an implemenation of a class "Iterator" that can be used with any collection. Its methods and logic duplicates as much of Supplier as possible (it adds positioning).

An iterator object should work on a snapshot of the colletction and supply the following methods:

  • init(collection): constructor expecting a collection
  • available: returns .true if an index/item pair is available
  • next: positions on the next index/item pair
  • previous: positions on the previous index/item pair
  • first: positions on the first index/item pair
  • last: positions on the last index/item pair
  • position: attribute which stores the number of the index/item pair the iterator is currently addressing
  • index: returns the index value
  • item: returns the item value

The class is uploaded with this RFE.

A possible program using that class, followed by its output:

------------- cut here -------------
arr=.array~of("a", "b", "c")

say "Iterating using 'next':"
it=.Iterator~new(arr) -- create an ierator
do while it~available
say it~position"/"it~items":" "index="it~index"," "item="it~item
it~next
end
say "---"

say "Positioning using 'last', ierating using 'previous':"
it~last -- position on last index/item pair
do while it~available
say it~position"/"it~items":" "index="it~index"," "item="it~item
it~previous
end
say "---"

say "Positioning on second index/item pair:"
it~position=2
say it~position"/"it~items":" "index="it~index"," "item="it~item
say "---"

::requires "Iterator.cls" -- get the "Iterator" class
------------- cut here -------------

Output of previous program, if run with the uploaded "Iterator.cls" definition:

------------- cut here -------------
Iterating using 'next':
1/3: index=1, item=a
2/3: index=2, item=b
3/3: index=3, item=c
---
Positioning using 'last', ierating using 'previous':
3/3: index=3, item=c
2/3: index=2, item=b
1/3: index=1, item=a
---
Positioning on second index/item pair:
2/3: index=2, item=b
---
------------- cut here -------------

Discussion

  • Logged In: YES
    user_id=662126
    Originator: YES

    Sorry, forgot the "items" method which returns the number of index/item pairs in the iterator (this method is available in the Iterator class implementation).

     
  • Rick McGuire
    Rick McGuire
    2007-10-03

    Logged In: YES
    user_id=1125291
    Originator: NO

    The more I look at this, the less I see that there's any need for this. It doesn't really provide any new function above what's already available with the collection classes, and will generally create confusion between iterator, supplier, and the various methods for traversal.

     
  • Rick McGuire
    Rick McGuire
    2007-10-03

    Logged In: YES
    user_id=1125291
    Originator: NO

    Notice: This RFE is slated to be rejected.

    Reason: See the RFE item in the SourceForge Tracker system for the rejection reason.

    To appeal this rejection please contact the Appeals Committee via Mr. Chip Davis

    oorexx-rfe-appeals@oorexx.org

    All further correspondence on this RFE should be directed to the Appeals Committee and MUST include this RFE number.

    The decision of the Appeals Committee is final.

     


Anonymous


Cancel   Add attachments