1043
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
"{ Package: 'stx:libbasic2' }"
|
|
14 |
|
|
15 |
SequenceableCollection subclass:#Cons
|
|
16 |
instanceVariableNames:'car cdr'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Collections-Linked'
|
|
20 |
!
|
|
21 |
|
|
22 |
!Cons class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
A pair as in lisp.
|
|
41 |
Create with:
|
1074
|
42 |
a !! b
|
|
43 |
or:
|
|
44 |
Cons car:a cdr:b
|
|
45 |
|
|
46 |
Conses are not heavily used by Smalltalk (actually: not at all).
|
|
47 |
Consider this mostly a demo class.
|
|
48 |
|
1043
|
49 |
|
|
50 |
[author:]
|
|
51 |
Claus Gittinger
|
|
52 |
|
|
53 |
[see also:]
|
|
54 |
|
|
55 |
"
|
|
56 |
! !
|
|
57 |
|
|
58 |
!Cons class methodsFor:'instance creation'!
|
|
59 |
|
|
60 |
car:carArg cdr:cdrArg
|
|
61 |
^ self basicNew car:carArg cdr:cdrArg
|
|
62 |
!
|
|
63 |
|
|
64 |
fromArray:anArray
|
|
65 |
|p last first|
|
|
66 |
|
|
67 |
anArray do:[:el |
|
|
68 |
p := self car:el cdr:nil.
|
|
69 |
first isNil ifTrue:[
|
|
70 |
first := p.
|
|
71 |
] ifFalse:[
|
|
72 |
last cdr:p.
|
|
73 |
].
|
|
74 |
last := p.
|
|
75 |
].
|
|
76 |
^ first.
|
|
77 |
|
|
78 |
"
|
1074
|
79 |
Cons fromArray:#(1 2 3 4)
|
|
80 |
Cons fromArray:#()
|
|
81 |
Cons fromArray:#(1)
|
1043
|
82 |
"
|
|
83 |
! !
|
|
84 |
|
|
85 |
!Cons methodsFor:'accessing'!
|
|
86 |
|
|
87 |
at:n
|
1074
|
88 |
"for collection compatibility:
|
|
89 |
a slow indexed accessor"
|
|
90 |
|
1043
|
91 |
^ (self nth:n) car
|
|
92 |
|
|
93 |
"
|
1074
|
94 |
(Cons fromArray:#(1)) at:1
|
|
95 |
(Cons fromArray:#(1 2 3 4)) at:1
|
|
96 |
(Cons fromArray:#(1 2 3 4)) at:3
|
|
97 |
(Cons fromArray:#(1 2 3 4)) at:4
|
|
98 |
(Cons fromArray:#(1 2 3 4)) at:5
|
1043
|
99 |
"
|
|
100 |
!
|
|
101 |
|
|
102 |
at:n put:newValue
|
1074
|
103 |
"for collection compatibility:
|
|
104 |
a slow indexed accessor"
|
|
105 |
|
1043
|
106 |
(self nth:n) car:newValue.
|
|
107 |
^ newValue.
|
|
108 |
|
|
109 |
"
|
|
110 |
|l|
|
|
111 |
|
1074
|
112 |
l := Cons fromArray:#(1 2 3 4).
|
1043
|
113 |
l at:1 put:'one'.
|
|
114 |
l at:3 put:'three'.
|
|
115 |
l
|
|
116 |
"
|
|
117 |
!
|
|
118 |
|
1237
|
119 |
head
|
|
120 |
"return the head, first or car - whatever you wonna call it"
|
|
121 |
|
|
122 |
^ self car
|
|
123 |
!
|
|
124 |
|
1043
|
125 |
last
|
1074
|
126 |
"for lispers:
|
|
127 |
return the last element of a list"
|
|
128 |
|
1043
|
129 |
|p rest|
|
|
130 |
|
|
131 |
p := self.
|
|
132 |
[(rest := p cdr) notNil] whileTrue:[
|
|
133 |
p := rest
|
|
134 |
].
|
1237
|
135 |
^ p car
|
1043
|
136 |
|
|
137 |
"
|
1074
|
138 |
(Cons fromArray:#(1)) last
|
|
139 |
(Cons fromArray:#(1 2 3 4)) last
|
1043
|
140 |
"
|
|
141 |
!
|
|
142 |
|
|
143 |
nth:n
|
1074
|
144 |
"for lispers:
|
|
145 |
return the nth element of a list"
|
|
146 |
|
1043
|
147 |
|cnt p|
|
|
148 |
|
|
149 |
cnt := n.
|
|
150 |
p := self.
|
|
151 |
[true] whileTrue:[
|
|
152 |
cnt := cnt - 1.
|
1237
|
153 |
cnt == 0 ifTrue:[^ p car].
|
1043
|
154 |
p := p cdr.
|
|
155 |
p isNil ifTrue:[
|
|
156 |
self error:'no such element' mayProceed:true.
|
|
157 |
^ nil
|
|
158 |
].
|
|
159 |
].
|
|
160 |
|
|
161 |
"
|
1074
|
162 |
(Cons fromArray:#(1)) nth:1
|
|
163 |
(Cons fromArray:#(1 2 3 4)) nth:1
|
|
164 |
(Cons fromArray:#(1 2 3 4)) nth:3
|
|
165 |
(Cons fromArray:#(1 2 3 4)) nth:4
|
|
166 |
(Cons fromArray:#(1 2 3 4)) nth:5
|
1043
|
167 |
"
|
1237
|
168 |
!
|
|
169 |
|
|
170 |
tail
|
|
171 |
"return the tail, second or cdr - whatever you wonna call it"
|
|
172 |
|
|
173 |
^ self cdr
|
1043
|
174 |
! !
|
|
175 |
|
|
176 |
!Cons methodsFor:'accessing - basic'!
|
|
177 |
|
|
178 |
car
|
1074
|
179 |
"return the head, first or car - whatever you wonna call it"
|
1043
|
180 |
|
|
181 |
^ car
|
|
182 |
!
|
|
183 |
|
|
184 |
car:something
|
1074
|
185 |
"set the head, first or car - whatever you wonna call it"
|
1043
|
186 |
|
|
187 |
car := something.
|
|
188 |
!
|
|
189 |
|
|
190 |
car:carArg cdr:cdrArg
|
1074
|
191 |
"set both car and cdr"
|
1043
|
192 |
|
|
193 |
car := carArg.
|
|
194 |
cdr := cdrArg.
|
|
195 |
!
|
|
196 |
|
|
197 |
cdr
|
1074
|
198 |
"return the tail, second or cdr - whatever you wonna call it"
|
1043
|
199 |
|
|
200 |
^ cdr
|
|
201 |
!
|
|
202 |
|
|
203 |
cdr:something
|
1074
|
204 |
"set the tail, second or cdr - whatever you wonna call it"
|
1043
|
205 |
|
|
206 |
cdr := something.
|
|
207 |
! !
|
|
208 |
|
|
209 |
!Cons methodsFor:'list processing'!
|
|
210 |
|
|
211 |
append:aCons
|
1074
|
212 |
"for lispers:
|
|
213 |
append the arg. Return a new list, where the 2nd part is shared"
|
|
214 |
|
1043
|
215 |
|p rest|
|
|
216 |
|
|
217 |
p := self.
|
|
218 |
[(rest := p cdr) notNil] whileTrue:[
|
|
219 |
p := rest
|
|
220 |
].
|
|
221 |
p cdr:aCons.
|
|
222 |
^ self
|
|
223 |
|
|
224 |
"
|
1074
|
225 |
(Cons fromArray:#(1 2 3 4))
|
|
226 |
append:(Cons fromArray:#(5 6 7 8))
|
|
227 |
"
|
|
228 |
|
|
229 |
"sharing demonstrated:
|
|
230 |
|
|
231 |
|a b ab|
|
|
232 |
|
|
233 |
a := Cons fromArray:#(1 2 3 4).
|
|
234 |
b := Cons fromArray:#(5 6 7 8).
|
|
235 |
ab := a append:b.
|
|
236 |
b car:'five'.
|
|
237 |
ab
|
1043
|
238 |
"
|
1237
|
239 |
!
|
|
240 |
|
|
241 |
take:n
|
|
242 |
"for lispers:
|
|
243 |
take n elements from the list; return a new list"
|
|
244 |
|
|
245 |
n > 0 ifTrue:[
|
|
246 |
^ Cons car:(self car) cdr:(self cdr take:n-1)
|
|
247 |
].
|
|
248 |
^ nil
|
|
249 |
|
|
250 |
"
|
|
251 |
(Cons fromArray:#(1 2 3 4)) take:3
|
|
252 |
(Cons fromArray:#(1)) take:0
|
|
253 |
(Cons fromArray:#()) take:3
|
|
254 |
"
|
1043
|
255 |
! !
|
|
256 |
|
|
257 |
!Cons methodsFor:'printing'!
|
|
258 |
|
|
259 |
displayString
|
|
260 |
^ self printString
|
|
261 |
!
|
|
262 |
|
|
263 |
printOn:aStream
|
|
264 |
(car isLazyValue not and:[ car isCons ]) ifTrue:[
|
|
265 |
aStream nextPutAll:'('.
|
|
266 |
car printOn:aStream.
|
|
267 |
aStream nextPutAll:')'.
|
|
268 |
] ifFalse:[
|
|
269 |
car printOn:aStream.
|
|
270 |
].
|
|
271 |
|
1073
|
272 |
aStream nextPutAll:'!!'.
|
1043
|
273 |
|
|
274 |
(cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
|
|
275 |
aStream nextPutAll:'('.
|
|
276 |
cdr printOn:aStream.
|
|
277 |
aStream nextPutAll:')'.
|
|
278 |
] ifFalse:[
|
|
279 |
cdr printOn:aStream.
|
|
280 |
].
|
|
281 |
! !
|
|
282 |
|
|
283 |
!Cons methodsFor:'queries'!
|
|
284 |
|
|
285 |
isCons
|
|
286 |
^ true
|
|
287 |
!
|
|
288 |
|
|
289 |
size
|
1074
|
290 |
"for smalltalkers: the lists length"
|
|
291 |
|
1043
|
292 |
|l p rest|
|
|
293 |
|
|
294 |
l := 1.
|
|
295 |
p := self.
|
|
296 |
[(rest := p cdr) notNil] whileTrue:[
|
|
297 |
l := l + 1.
|
|
298 |
p := rest
|
|
299 |
].
|
|
300 |
^ l
|
|
301 |
|
|
302 |
"
|
1074
|
303 |
(Cons fromArray:#( )) size
|
|
304 |
(Cons fromArray:#(1)) size
|
|
305 |
(Cons fromArray:#(1 2 3 4)) size
|
1043
|
306 |
"
|
|
307 |
! !
|
|
308 |
|
|
309 |
!Cons class methodsFor:'documentation'!
|
|
310 |
|
|
311 |
version
|
1237
|
312 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.4 2003-06-05 18:39:20 cg Exp $'
|
1043
|
313 |
! !
|