Iterator.st
changeset 62 a759b5c72c98
child 290 f4fbe0881e1b
equal deleted inserted replaced
61:13d2435bc955 62:a759b5c72c98
       
     1 "       NAME            Iterator
       
     2         AUTHOR          miw@cs.man.ac.uk (Mario Wolczko)
       
     3         FUNCTION        a wrapper for blocks that iterate over collections
       
     4         ST-VERSION      4.0 4.1
       
     5         PREREQUISITES   
       
     6         CONFLICTS
       
     7         DISTRIBUTION    world
       
     8         VERSION         1
       
     9         DATE    18 Jun 1991
       
    10         SUMMARY
       
    11  Occasionally you may have a block that when evaluated can be
       
    12 treated as a collection -- ie it takes another block as parameter,
       
    13 then applies that to a sequence of values.
       
    14 
       
    15 This goodie wraps the block into an object -- an iterator -- which is
       
    16 part of the collection hierarchy, and therefore inherits a variety of
       
    17 useful collection-related methods.
       
    18 
       
    19 Mario Wolczko
       
    20 
       
    21 Dept. of Computer Science   Internet:      mario@cs.man.ac.uk
       
    22 The University              uucp:        uknet!!man.cs!!mario
       
    23 Manchester M13 9PL          JANET:         mario@uk.ac.man.cs
       
    24 U.K.                        Tel: +44-61-275 6146  (FAX: 6236)
       
    25 ______the mushroom project___________________________________
       
    26 
       
    27 "
       
    28 'From Objectworks(r)\Smalltalk, Release 4 of 25 October 1990 on 18 June 1991 at 7:48:59 pm'!
       
    29 
       
    30 Collection subclass: #Iterator
       
    31         instanceVariableNames: 'block '
       
    32         classVariableNames: ''
       
    33         poolDictionaries: ''
       
    34         category: 'Collections-Sequenceable'
       
    35 !
       
    36 
       
    37 Iterator comment:
       
    38 'An Iterator is a read-only collection that evaluates a block to yield the elements
       
    39  of the collection.'
       
    40 !
       
    41 
       
    42 !Iterator methodsFor: 'removing'!
       
    43 
       
    44 remove: oldObject ifAbsent: anExceptionBlock 
       
    45         "Iterators are read-only."
       
    46         self shouldNotImplement
       
    47 ! !
       
    48 
       
    49 !Iterator methodsFor: 'adding'!
       
    50 
       
    51 add: anObject
       
    52         "Iterators are read-only"
       
    53         self shouldNotImplement
       
    54 ! !
       
    55 
       
    56 !Iterator methodsFor: 'enumerating'!
       
    57 
       
    58 do: aBlock
       
    59         block value: aBlock
       
    60 !
       
    61 
       
    62 findFirst: aBlock
       
    63         "Answer the index of the first element of the receiver
       
    64         for which aBlock evaluates as true."
       
    65 
       
    66         | index |
       
    67         index := 1.
       
    68         self do: [ :el | (aBlock value: el) ifTrue: [^index].  index := index + 1].
       
    69 
       
    70         ^0
       
    71 !
       
    72 
       
    73 findLast: aBlock
       
    74         "Answer the index of the last element of the receiver
       
    75         for which aBlock evaluates as true."
       
    76 
       
    77         | index last |
       
    78         index := 1.
       
    79         last := 0.
       
    80         self do: [ :el | (aBlock value: el) ifTrue: [last := index].  index := index + 1].
       
    81         ^last
       
    82 !
       
    83 
       
    84 keysAndValuesDo: aBlock  
       
    85         "Evaluate aBlock with each of the receiver's key/value pairs
       
    86         (e.g. indexes and elements) as the arguments."
       
    87 
       
    88         | index |
       
    89         index := 1.
       
    90         self do: [:el | aBlock value: index value: el.  index := index + 1]
       
    91 ! !
       
    92 
       
    93 !Iterator methodsFor: 'accessing'!
       
    94 
       
    95 identityIndexOf: anElement 
       
    96         "Answer the identity index of anElement within the receiver.  
       
    97          If the receiver does not contain anElement, answer 0."
       
    98 
       
    99         ^self identityIndexOf: anElement ifAbsent: [0]
       
   100 !
       
   101 
       
   102 identityIndexOf: anElement ifAbsent: exceptionBlock 
       
   103         "Answer the identity index of anElement within the receiver.  
       
   104          If the receiver does not contain anElement, answer the result 
       
   105          of evaluating the exceptionBlock."
       
   106 
       
   107         | index |
       
   108         index := 1.
       
   109         self do: [ :el | el == anElement ifTrue: [^index].  index := index + 1].
       
   110         ^exceptionBlock value
       
   111 !
       
   112 
       
   113 indexOf: anElement 
       
   114         "Answer the index of anElement within the receiver.  If the receiver does
       
   115         not contain anElement, answer 0."
       
   116 
       
   117         ^self indexOf: anElement ifAbsent: [0]
       
   118 !
       
   119 
       
   120 indexOf: anElement ifAbsent: exceptionBlock 
       
   121         "Answer the index of anElement within the receiver.  If the receiver does
       
   122         not contain anElement, answer the result of evaluating the exceptionBlock."
       
   123 
       
   124 
       
   125         | index |
       
   126         index := 1.
       
   127         self do: [ :el | el = anElement ifTrue: [^index].  index := index + 1].
       
   128         ^exceptionBlock value
       
   129 ! !
       
   130 
       
   131 !Iterator methodsFor: 'private'!
       
   132 
       
   133 block: aBlock
       
   134         block := aBlock
       
   135 !
       
   136 
       
   137 species
       
   138         ^OrderedCollection
       
   139 ! !
       
   140 
       
   141 !Iterator methodsFor: 'converting'!
       
   142 
       
   143 asOrderedCollection
       
   144         "Answer a new instance of OrderedCollection whose elements are the elements of
       
   145         the receiver.  The order in which elements are added depends on the order in
       
   146         which the receiver enumerates its elements.  In the case of unordered collections,
       
   147         the ordering is not necessarily the same for multiple requests for the conversion."
       
   148 
       
   149 
       
   150         | anOrderedCollection |
       
   151         anOrderedCollection := OrderedCollection new.
       
   152         self do: [:each | anOrderedCollection addLast: each].
       
   153         ^anOrderedCollection
       
   154 ! !
       
   155 
       
   156 !Iterator class methodsFor: 'instance creation'!
       
   157 
       
   158 on: aBlock
       
   159         ^self new block: aBlock
       
   160 !
       
   161 
       
   162 on: collection msg: msg
       
   163         ^self new block: [ :aBlock | collection perform: msg with: aBlock]
       
   164 ! !
       
   165 
       
   166 "COPYRIGHT.
       
   167  The above file is a Manchester Goodie protected by copyright.
       
   168  These conditions are imposed on the whole Goodie, and on any significant
       
   169  part of it which is separately transmitted or stored:
       
   170         * You must ensure that every copy includes this notice, and that
       
   171           source and author(s) of the material are acknowledged.
       
   172         * These conditions must be imposed on anyone who receives a copy.
       
   173         * The material shall not be used for commercial gain without the prior
       
   174           written consent of the author(s).
       
   175  Further information on the copyright conditions may be obtained by
       
   176  sending electronic mail:
       
   177         To: goodies-lib@cs.man.ac.uk
       
   178         Subject: copyright
       
   179  or by writing to The Smalltalk Goodies Library Manager, Dept of
       
   180  Computer Science, The University, Manchester M13 9PL, UK
       
   181 
       
   182  (C) Copyright 1992 University of Manchester
       
   183  For more information about the Manchester Goodies Library (from which 
       
   184  this file was distributed) send e-mail:
       
   185         To: goodies-lib@cs.man.ac.uk
       
   186         Subject: help 
       
   187 "!