root/trunk/iterators/iterators.html

Revision 13, 5.8 kB (checked in by BCS, 2 years ago)

added Witold Baryluk's iterators

Line 
1 <html><head>
2     <META http-equiv="content-type" content="text/html; charset=utf-8">
3     <title>iterators</title>
4     </head><body>
5     <h1>iterators</h1>
6     <!-- Generated by Ddoc from iterators.d -->
7 Python-like (yield) <u>iterators</u> for Digital Mars D
8 <br><br>
9 <b>Version:</b><br>
10 1.0
11 <br><br>
12 <b>Author:</b><br>
13 Witold Baryluk <baryluk@smp.if.uj.edu.pl>
14  Copyright 2007
15 <br><br>
16 <b>Licence:</b><br>
17 BSD
18 <br><br>
19
20 This package may be redistributed under the terms of the UCB BSD
21 <br><br>
22 <b>License:</b><br>
23 Copyright (c) Witold Baryluk
24 All rights reserved.
25 <br><br>
26
27 Redistribution and use in source and binary forms, with or without
28 modification, are permitted provided that the following conditions
29 are met:
30 1. Redistributions of source code must retain the above copyright
31    notice, this list of conditions and the following disclaimer.
32 2. Redistributions in binary form must reproduce the above copyright
33    notice, this list of conditions and the following disclaimer in the
34    documentation and/or other materials provided with the distribution.
35 4. Neither the name of the Witold Baryluk nor the names of its contributors
36    may be used to endorse or promote products derived from this software
37    without specific prior written permission.
38 <br><br>
39
40 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 SUCH DAMAGE.
51 <br><br>
52
53  
54 <br><br>
55
56
57 <br><br>
58 <b>TODO:</b><br>
59 - Index variables shouldn't be inout.
60      - Shorter syntax (s.squers(), insted s.squers.ic())
61      - Neseting?
62    - Remove unnacasary types: (mixin mainiter!(iter),
63      insted mixin mainiter!(int, iter));
64    - Reuse mainiter procedures in inneriter, becouse
65      they are very similar.
66    - Mayby void iter() , and return 0; is not needed really
67  
68 <br><br>
69
70 <dl><dt><big>class <u>IterBreak</u>: object.Exception;
71 </big></dt>
72 <dd>Exception rised when user defined function in
73   foreach breaks, which cancel iteration
74     process, catched in opApply.
75 <br><br>
76
77 <dl></dl>
78 </dd>
79 <dt><big>class <u>IterLast</u>: object.Exception;
80 </big></dt>
81 <dd>Similar to IterBreak, used to make last iteration.
82 <br><br>
83
84 <dl></dl>
85 </dd>
86 <dt><big>template <u>mainiter</u>(V,alias f)</big></dt>
87 <dd>Mixin this template to use your class as
88  iterator.
89 <br><br>
90 <b>Params:</b><br>
91 <table><tr><td>V</td>
92 <td>type of iterator (ie. double)</td></tr>
93 <tr><td>f</td>
94 <td>name of virtual method implementing iteration.</td></tr>
95 </table><br>
96 <b>Example:</b><br>
97 <pre class="d_code">  <font color=blue>class</font> Naturals(<font color=blue>int</font> n) {
98     <font color=blue>int</font> iter() {
99         <font color=blue>for</font> (<font color=blue>int</font> i = 1; i &lt;= n; i++) yield(i); <font color=green>// use yield to process next item
100 </font>         <font color=blue>return</font> 0;        <font color=green>// indicate correct end of iterator
101 </font>    }
102     <font color=blue>mixin</font> <u>mainiter</u>!(<font color=blue>int</font>, iter);   <font color=green>// mixin iterator's stuff
103 </font>  }
104   ...
105   <font color=blue>foreach</font> (<font color=blue>int</font> x; <font color=blue>new</font> Naturals!(10)()) {
106       ...
107   }
108 </pre>
109  
110 <br><br>
111
112 <dl><dt><big>void <u>yield</u>(V <i>v</i>);
113 </big></dt>
114 <dd>Used to process next iteration.
115 <br><br>
116
117 </dd>
118 <dt><big>void <u>last</u>(V <i>v</i>);
119 </big></dt>
120 <dd>Used to process <u>last</u> iteration.
121 <br><br>
122
123 </dd>
124 </dl>
125 </dd>
126 <dt><big>class <u>Iterator</u>(V);
127 </big></dt>
128 <dd>V type <u>Iterator</u> for method metoda0
129 <br><br>
130
131 <dl><dt><big>this(int delegate(Iterator!(V)) metoda0);
132 </big></dt>
133 <dd><br><br>
134 </dd>
135 <dt><big>void <u>yield</u>(V <i>v</i>);
136 </big></dt>
137 <dd>Used to process next iteration.
138 <br><br>
139
140 </dd>
141 <dt><big>void <u>last</u>(V <i>v</i>);
142 </big></dt>
143 <dd>Used to process <u>last</u> iteration.
144 <br><br>
145
146 </dd>
147 </dl>
148 </dd>
149 <dt><big>template <u>inneriter</u>(V,alias metoda)</big></dt>
150 <dd>Mixin this template to use subclass as iterator.
151  Use this when you want multiple iterators.
152 <br><br>
153 <b>Example:</b><br>
154 <pre class="d_code">  <font color=blue>class</font> MNaturals(<font color=blue>int</font> n) {
155    <font color=blue>int</font> iter2(Iterator!(<font color=blue>double</font>) x) {
156        <font color=blue>for</font> (<font color=blue>int</font> i = 1; i &lt;= n; i++) x.yield(i*i);
157        <font color=blue>return</font> 0;
158    }
159    <font color=blue>mixin</font> <u>inneriter</u>!(<font color=blue>double</font>, iter2) squers;
160    <font color=blue>int</font> iter3(Iterator!(<font color=blue>double</font>) x) {
161        <font color=blue>for</font> (<font color=blue>int</font> i = 1; i &lt;= n; i++) x.yield(i*i*i);
162        <font color=blue>return</font> 0;
163    }
164    <font color=blue>mixin</font> <u>inneriter</u>!(<font color=blue>double</font>, iter3) qubes;
165  }
166   ...
167   <font color=blue>auto</font> s = <font color=blue>new</font> MNaturals!(10)();
168   <font color=blue>foreach</font> (<font color=blue>int</font> x; s.squers.ic()) {
169       ...
170   }
171   <font color=blue>foreach</font> (<font color=blue>int</font> x; s.qubes.ic()) {
172       ...
173   }
174 </pre>
175  
176 <br><br>
177
178 <dl></dl>
179 </dd>
180 </dl>
181
182     <hr><small>Page generated by <a href="http://www.digitalmars.com/d/ddoc.html">Ddoc</a>. </small>
183     </body></html>
Note: See TracBrowser for help on using the browser.