1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  
     | 
    
      doc ///
  Key
    Iterator
  Headline
    class for iterators
  Description
    Text
      This is a class designed to simplify writing @TO iterator@ methods.
      Each instance is a nullary @TO FunctionClosure@ that serves as the
      @TO next@ method for the iterator.
    Example
      iter = iterator {1, 2, 3}
      code iter
    Text
      Each call of @TT "next iter"@ is equivalent to @TT "iter()"@.
    Example
      next iter
      iter()
      next iter
      iter()
    Text
      Every @TT "Iterator"@ object is an iterator for itself.
    Example
      primes = Iterator (
	  p := 2;
	  () -> (
	      r := p;
	      p = nextPrime(p + 1);
	      r));
      iterator primes === primes
    Text
      However, we cannot "rewind" an @TT "Iterator"@ object.  Every time that
      it is iterated upon using @TO "for"@, @TO scan@, etc., iteration will
      resume where it left off previously.
    Example
      for p in primes list if p > 20 then break else p
      for p in primes list if p > 20 then break else p
    Text
      Contrast this with most other classes with the @TO iterator@ method
      installed, like, strings, for which a new @TT "Iterator"@ object is
      created every time it is iterated upon, and so iteration starts over
      from the beginning
    Example
      s = "Hello, world!"
      for c in s list c
      for c in s list c
  SeeAlso
    iterator
    next
    StopIteration
///
doc ///
  Key
    iterator
    (iterator, Iterator)
    (iterator, String)
    (iterator, VisibleList)
  Headline
    get an iterator
  Usage
    iterator x
  Inputs
    x:Thing
  Outputs
    :Thing -- likely an @TO Iterator@
  Description
    Text
      An iterator is an object that is used to traverse through @TT "x"@.
      Usually, but not necessarily, this will be an instance of the
      @TO Iterator@ class.
    Example
      iter = iterator {1, 2, 3}
    Text
      The class of an iterator should have a @TO next@ method installed that
      gets the next element of @TT "x"@.
    Example
      next iter
      next iter
      next iter
    Text
      If @TT "x"@ contains only a finite number of elements, then @TO next@
      should return the symbol @TO StopIteration@ after exhausting them all.
    Example
      next iter
    Text
      Instances of classes with this method installed can be used like lists
      in @TO "for"@ loops and @TO scan@.
    Example
      lookup(iterator, String)
      for i in "foo" list i
      scan("foo", print)
    Text
      They can also be passed to @TO apply@ and @TO select@.  In each case, an
      @TO Iterator@ object will be returned that is an iterator for itself.
    Example
      apply("foo", toUpper)
      for i in oo list i
      select("foo", i -> i == "o")
      for i in oo list i
  SeeAlso
    Iterator
    next
    StopIteration
///
doc ///
  Key
    next
    (next, Iterator)
  Headline
    get the next object from an iterator
  Usage
    next x
  Inputs
    x:Thing -- an iterator
  Outputs
    :Thing -- the next object from @TT "x"@
  Description
    Text
      This gets the next object from an iterator, i.e., something returned by
      @TO iterator@.
    Example
      iter = iterator {1, 2, 3}
      next iter
      next iter
      next iter
    Text
      If the iterator is exhausted, then the symbol @TO StopIteration@ is
      returned.
    Example
      next iter
  SeeAlso
    Iterator
    iterator
    StopIteration
///
doc ///
  Key
    StopIteration
  Headline
    stop iteration
  Description
    Text
      This symbol is returned by @TO next@ to signal that iteration is complete.
    Example
      iter = iterator {1, 2, 3}
      next iter
      next iter
      next iter
      next iter
  SeeAlso
    Iterator
    iterator
    next
///
 
     |