5260
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5256
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2019 by Claus Gittinger
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
PeekableStream subclass:#IteratorStream
|
|
19 |
instanceVariableNames:'idx iterator contentsSpecies'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Streams'
|
|
23 |
!
|
|
24 |
|
|
25 |
!IteratorStream class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2019 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
This is a pseudo stream, which generates values by using an iterator action;
|
|
44 |
this is a block which is called to generate the value to read.
|
|
45 |
The block gets the index (1..) as argument.
|
|
46 |
|
|
47 |
[author:]
|
|
48 |
Claus Gittinger
|
|
49 |
|
|
50 |
[instance variables:]
|
|
51 |
|
|
52 |
[class variables:]
|
|
53 |
|
|
54 |
[see also:]
|
|
55 |
Iterator
|
|
56 |
"
|
|
57 |
!
|
|
58 |
|
|
59 |
example
|
|
60 |
"
|
|
61 |
|s|
|
|
62 |
|
|
63 |
s := IteratorStream on:[:i | i].
|
|
64 |
s next:10.
|
|
65 |
s next:10.
|
|
66 |
|
|
67 |
s := IteratorStream on:[:i | i*2].
|
|
68 |
s next:10.
|
|
69 |
|
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
|
73 |
!IteratorStream class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
on:aBlock
|
|
76 |
^ self basicNew initialize iterator:aBlock
|
|
77 |
! !
|
|
78 |
|
|
79 |
!IteratorStream methodsFor:'accessing'!
|
|
80 |
|
5260
|
81 |
atEnd
|
|
82 |
^ false
|
|
83 |
|
|
84 |
!
|
|
85 |
|
5256
|
86 |
contentsSpecies
|
|
87 |
"return a class of which instances will be returned, when
|
|
88 |
parts of the collection are asked for.
|
|
89 |
(see upTo-kind of methods in Stream)"
|
|
90 |
|
|
91 |
^ contentsSpecies ? Array
|
|
92 |
!
|
|
93 |
|
|
94 |
contentsSpecies:aCollectionClass
|
|
95 |
contentsSpecies := aCollectionClass.
|
|
96 |
!
|
|
97 |
|
|
98 |
iterator:aOneArgBlock
|
|
99 |
iterator := aOneArgBlock.
|
|
100 |
!
|
|
101 |
|
|
102 |
position
|
|
103 |
^ idx
|
|
104 |
!
|
|
105 |
|
|
106 |
position:newPosition
|
|
107 |
idx := newPosition
|
|
108 |
! !
|
|
109 |
|
|
110 |
!IteratorStream methodsFor:'initialization'!
|
|
111 |
|
|
112 |
initialize
|
|
113 |
super initialize.
|
|
114 |
idx := 0.
|
|
115 |
! !
|
|
116 |
|
|
117 |
!IteratorStream methodsFor:'reading'!
|
|
118 |
|
|
119 |
next
|
|
120 |
"return the next element from the stream by evaluating the nextBlock"
|
|
121 |
|
|
122 |
iterator notNil ifTrue:[
|
|
123 |
idx := idx + 1.
|
|
124 |
^ iterator value:idx
|
|
125 |
].
|
|
126 |
self error:'iterator is undefined'
|
|
127 |
! !
|
|
128 |
|
|
129 |
!IteratorStream class methodsFor:'documentation'!
|
|
130 |
|
|
131 |
version_CVS
|
|
132 |
^ '$Header$'
|
|
133 |
! !
|
|
134 |
|