eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'Iterator':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: Iterator


Inheritance:

   Object
   |
   +--Collection
      |
      +--Iterator

Package:
stx:libbasic2
Category:
Collections-Sequenceable
Version:
rev: 1.36 date: 2019/01/09 16:39:50
user: cg
file: Iterator.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Mario Wolczko miw@cs.man.ac.uk

Description:


Occasionally you may have a block that when evaluated can be
treated as a collection -- i.e. it takes another block as parameter,
then applies that to a sequence of values.

This goodie wraps the block into an object -- an iterator -- which is
part of the collection hierarchy, and therefore inherits a variety of
useful collection-related methods.

[info:]
    NAME            Iterator
    AUTHOR          miw@cs.man.ac.uk (Mario Wolczko)
    FUNCTION        a wrapper for blocks that iterate over collections
    ST-VERSION      4.0 4.1
    PREREQUISITES   
    CONFLICTS
    DISTRIBUTION    world
    VERSION         1
    DATE    18 Jun 1991
    SUMMARY

[organisation:]
    Dept. of Computer Science   Internet:      mario@cs.man.ac.uk
    The University              uucp:        uknet!!man.cs!!mario
    Manchester M13 9PL          JANET:         mario@uk.ac.man.cs
    U.K.                        Tel: +44-61-275 6146  (FAX: 6236)
    ______the mushroom project___________________________________


Class protocol:

instance creation
o  on: aBlock
|i b|

b := [:whatToDo |
1 to:10 do:[:i |
whatToDo value:i
]
].

i := Iterator on:b.
i do:[:j |
Transcript showCR:j
].

usage example(s):

     an iterator, simulating a collection of 100 random values:

     |i b|

     b := [:whatToDo |
               |rnd|

               rnd := Random new.
               1 to:100 do:[:i | 
                  whatToDo value:(rnd next)
               ] 
          ].

     i := Iterator on:b.
     i do:[:j |
        j printNL
     ].

o  on: collection msg: oneArgSelector
|i|

i := Iterator on:#(1 2 3 4 5 6 7) msg:#reverseDo:.
i do:[:j |
Transcript showCR:j
].


Instance protocol:

accessing
o  identityIndexOf: anElement
Answer the identity index of anElement within the receiver.
If the receiver does not contain anElement, answer 0.

o  identityIndexOf: anElement ifAbsent: exceptionBlock
Answer the identity index of anElement within the receiver.
If the receiver does not contain anElement, answer the result
of evaluating the exceptionBlock.

o  indexOf: anElement
Answer the index of anElement within the receiver. If the receiver does
not contain anElement, answer 0.

o  indexOf: anElement ifAbsent: exceptionBlock
Answer the index of anElement within the receiver. If the receiver does
not contain anElement, answer the result of evaluating the exceptionBlock.

adding & removing
o  add: anObject
raises an error: Iterators are read-only

o  remove: oldObject ifAbsent: anExceptionBlock
Iterators are read-only.

o  removeIdentical: oldObject ifAbsent: anExceptionBlock
Iterators are read-only.

converting
o  asOrderedCollection
Answer a new instance of OrderedCollection whose elements are the elements of
the receiver. The order in which elements are added depends on the order in
which the receiver enumerates its elements. In the case of unordered collections,
the ordering is not necessarily the same for multiple requests for the conversion.

enumerating
o  collect: aBlock as: aClass
Reimplemented here as Iterator does not support #size.
aClass must be a non-fixed collection.

usage example(s):

     (Iterator on:[:whatToDo| 'abcd' do:whatToDo]) 
        collect:[:element | element asUppercase] as:OrderedCollection

     (Iterator on:[:whatToDo| 'abcd' do:whatToDo]) 
        collect:[:element | element asUppercase] as:Array

     (Iterator on:[:whatToDo| '1234' do:whatToDo]) 
        collect:[:char | char digitValue] as:ByteArray

o  detectLast: aBlock ifNone: anExceptionValue
evaluate the argument, aBlock for each element in the receiver until
the block returns true; in this case return the element which caused
the true evaluation. The elements are processed in reverse order.
If none of the evaluations returns true, return the value of anExceptionValue

usage example(s):

     (Iterator on:[:whatToDo| #(1 2 3 4) do:whatToDo]) detectLast:[:n | n odd] ifNone:['sorry']    
     (Iterator on:[:whatToDo| #(2 4 6 8) do:whatToDo]) detectLast:[:n | n odd] ifNone:['sorry']    

o  do: aBlock

o  findFirst: aBlock ifNone: exceptionalValue
Answer the index of the first element of the receiver
for which aBlock evaluates as true.

o  findLast: aBlock
Answer the index of the last element of the receiver
for which aBlock evaluates as true.

o  keysAndValuesDo: aBlock
Evaluate aBlock with each of the receiver's key/value pairs
(e.g. indexes and elements) as the arguments.

o  reverseDo: aBlock
evaluate the argument, aBlock for each element in reverse order.

inspecting
o  inspectorValueStringInListFor: anInspector
( an extension from the stx:libtool package )
returns a string to be shown in the inspector's list.
Redefined to avoid calling the iterator

printing & storing
o  displayOn: aGCOrStream
what a kludge - Dolphin and Squeak mean: printOn: a stream;

o  printOn: aStream
(comment from inherited method)
append a user readable representation of the receiver to aStream.
The text appended is not meant to be read back for reconstruction of
the receiver. Also, this method limits the size of generated string.

private
o  block

o  block: aBlock

o  species
return the type of collection to be returned by collect, select etc.

queries
o  isEmpty
(comment from inherited method)
return true, if the receiver is empty

o  size
(comment from inherited method)
return the number of elements in the receiver.
This is usually redefined in subclasses for more performance.


Examples:


an iterator, simulating the collection of all classes in the system:
    |i b|

    b := [:whatToDo |
              Smalltalk allClassesDo:[:cls | 
                 whatToDo value:cls
              ] 
         ].

    i := Iterator on:b.
    i do:[:cls |
       Transcript showCR:cls name
    ].
much like above, one that simulates the collection of all methodNames starting with 'a':
    |i b|

    b := [:whatToDo |
              Smalltalk allClassesDo:[:cls |
                 cls methodDictionary keysAndValuesDo:[:nm :mthd |
                   (nm startsWith:$a) ifTrue:[
                      whatToDo value:nm
                    ]
                 ]
              ] 
         ].

    i := Iterator on:b.
    i do:[:nm |
       Transcript showCR:nm
    ].
an iterator, simulating a collection of 100 random values:
    |i b|

    b := [:whatToDo |
              |rnd|

              rnd := Random new.
              1 to:100 do:[:i | 
                 whatToDo value:(rnd next)
              ] 
         ].

    i := Iterator on:b.
    i do:[:j |
       j printNL
    ].
an iterator, simulating a collection of the lines in a file:
    |i b|

    b := [:whatToDo |
              |s line|

              s := 'smalltalk.rc' asFilename readStream.
              [s atEnd] whileFalse:[
                 line := s nextLine.
                 whatToDo value:line.
              ].
              s close
         ].

    i := Iterator on:b.
    i do:[:j |
       j printNL
    ].


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 19 Apr 2024 01:43:51 GMT