|
Class: Cons
Object
|
+--Collection
|
+--SequenceableCollection
|
+--Cons
|
+--LazyCons
- Package:
- stx:libbasic2
- Category:
- Collections-Linked
- Version:
- rev:
1.32
date: 2019/04/03 06:33:16
- user: cg
- file: Cons.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger (Jun 2002)
A pair as in lisp.
Cons car:a cdr:b
Conses are not heavily used by Smalltalk (actually: not at all).
Consider this a demo class.
instance creation
-
car: carArg cdr: cdrArg
-
-
fromArray: anArray
-
Cons fromArray:#(1 2 3 4)
Cons fromArray:#()
Cons fromArray:#(1)
Cons fromArray:(1 to:10000)
-
makeList: size
-
(self makeList:0) size
(self makeList:1) size
(self makeList:100) size
(self makeList:1000) size
(self makeList:10000) size
sExpressions
-
readLispAtomFrom: aStream
-
smalltalk allows +n; scheme does not
-
readLispFrom: aStream
-
EOL comment
usage example(s):
self readLispFrom:('(cons 1 2)' readStream).
|
-
readLispListFrom: aStream
-
the leading '('
accessing
-
at: n
-
for collection compatibility:
a slow indexed accessor
usage example(s):
(Cons fromArray:#(1)) at:1
(Cons fromArray:#(1 2 3 4)) at:1
(Cons fromArray:#(1 2 3 4)) at:3
(Cons fromArray:#(1 2 3 4)) at:4
(Cons fromArray:#(1 2 3 4)) at:5
|
-
at: n put: newValue
-
destructive:
for collection compatibility: a slow indexed accessor
-
first
-
return the head, first or car - whatever you wonna call it
-
head
-
return the head, first or car - whatever you wonna call it
-
last
-
for lispers:
return the last element of a list
usage example(s):
(Cons fromArray:#(1)) last
(Cons fromArray:#(1 2 3 4)) last
|
-
nth: n
-
for lispers:
return the nth element of a list
usage example(s):
(Cons fromArray:#(1)) nth:1
(Cons fromArray:#(1 2 3 4)) nth:1
(Cons fromArray:#(1 2 3 4)) nth:3
(Cons fromArray:#(1 2 3 4)) nth:4
(Cons fromArray:#(1 2 3 4)) nth:5
(Cons fromArray:#( )) nth:1 -> error
|
-
rest
-
return the tail, rest or cdr - whatever you wonna call it
-
reversed
-
for lispers:
return a new list with the cars in reverse order
usage example(s):
(Cons fromArray:#(1)) reversed
(Cons fromArray:#(1 2)) reversed
(Cons fromArray:#(1 2 3 4)) reversed
(Cons fromArray:(1 to:10000)) reversed
|
-
tail
-
return the tail, rest or cdr - whatever you wonna call it
accessing-basic
-
cadddr
-
return the fourth element
-
caddr
-
return the third element
-
cadr
-
return the second element
-
car
-
return the head, first or car - whatever you wonna call it
-
car: something
-
set the head, first or car - whatever you wonna call it
-
car: carArg cdr: cdrArg
-
set both car and cdr
-
cddr
-
return the rest after the second element
-
cdr
-
return the tail, second or cdr - whatever you wonna call it
-
cdr: something
-
set the tail, second or cdr - whatever you wonna call it
-
first: carArg rest: cdrArg
-
set both car and cdr
-
head: carArg tail: cdrArg
-
set both car and cdr
-
nthPair: n
-
a helper:
return the nth pair of a list
comparing
-
= aCons
-
-
hash
-
enumerating
-
do: aBlock
-
evaluate aBlock for each car
list processing
-
append: aCons
-
for lispers:
append the arg. Return a new list, where the 2nd part is shared.
Destructive: the receiver's last cdr is modified.
usage example(s):
(Cons fromArray:#(1 2 3 4))
append:(Cons fromArray:#(5 6 7 8))
|
-
take: nTaken
-
for lispers:
take n elements from the list; return a new list
usage example(s):
^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
|
usage example(s):
(Cons fromArray:#(1 2 3 4)) take:3
(Cons fromArray:#(1)) take:0
(Cons fromArray:#()) take:3
(Cons fromArray:(1 to: 1000)) take:999
|gen allNumbers|
gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
allNumbers := gen value:1.
allNumbers take:10
|
printing
-
displayString
-
-
printOn: aStream
-
(comment from inherited method)
append a user readable representation of the receiver to aStream.
The text appended is not meant to be read back for reconstruction of
the receiver. Also, this method limits the size of generated string.
-
printRestOn: aStream
-
queries
-
beginAndSizeOfCycle
-
return the begin and size of a cycle, if the list contains one.
Nil otherwise.
Floyd's cycle-finding algorithm
-
isCyclic
-
true if the list contains a cycle
-
size
-
the list's length
usage example(s):
(Cons fromArray:#(1)) size
(Cons fromArray:#(1 2 3 4)) size
(Cons car:1 cdr:2) size --> error (degenerated list)
|
streaming
-
readStream
-
testing
-
isCons
-
-
isLazy
-
|p1 p2 p3|
p3 := Cons car:3 cdr:nil.
p2 := Cons car:2 cdr:p3.
p1 := Cons car:1 cdr:p2.
p1 head.
p1 tail.
p1 size.
p1 do:[:each | Transcript showCR:each].
p1 at:2
|
|