|
Class: Cons
Object
|
+--Collection
|
+--SequenceableCollection
|
+--Cons
|
+--ImmutableCons
|
+--LazyCons
- Package:
- stx:libbasic2
- Category:
- Collections-Linked
- Version:
- rev:
1.48
date: 2023/12/08 09:13:11
- user: cg
- file: Cons.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
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 (it is used by the embedded Scheme interpreter, though).
copyrightCOPYRIGHT (c) 2002 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
instance creation
-
car: carArg
-
return an cons with given car and NIL as cdr
-
car: carArg cdr: cdrArg
-
return an cons with given car and cdr
-
fromArray: anArray
-
return a Lisp-like cons-chain with elements from anArray
Usage example(s):
Cons fromArray:#(1 2 3 4)
Cons fromArray:#()
Cons fromArray:#(1)
Cons fromArray:(1 to:10000)
|
-
makeList: sizeArg
-
return a Lisp-like cons-chain with size elements.
Each car is nil
Usage example(s):
(self makeList:0) size
(self makeList:1) size
(self makeList:10) size
(self makeList:100) size
(self makeList:1000) size
(self makeList:10000) size
|
sExpressions
-
readLispAtomFrom: aStream
-
read a number or symbol given in lisp syntax
-
readLispFrom: aStream
-
read a cons given in lisp syntax;
cares for EOL comments (introduced by ';')
Usage example(s):
self readLispFrom:('(cons 1 2)' readStream).
self readLispFrom:(c'(cons 1 ; foo \n 2)' readStream).
|
-
readLispListFrom: aStream
-
read a list in Lisp syntax
-
readLispStringFrom: aStream
-
a string in Scheme syntax; starts with double-quote,
may have embedded C-escapes.
accessing
-
at: n
-
for collection compatibility:
a very 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 very slow indexed accessor
Usage example(s):
|l|
l := Cons fromArray:#(1 2 3 4).
l at:1 put:'one'.
l at:3 put:'three'.
l
|
-
first
-
return the head, first or car - whatever you wonna call it
-
fourth
-
return the 3rd element
Usage example(s):
(BorgReader readFromString:'(10 20 30 40)') first
(BorgReader readFromString:'(10 20 30 40)') second
(BorgReader readFromString:'(10 20 30 40)') third
(BorgReader readFromString:'(10 20 30 40)') fourth
(BorgReader readFromString:'(10 20 30 40)') nth:1
(BorgReader readFromString:'(10 20 30 40)') nth:3
(BorgReader readFromString:'(10 20 30 40)') last
|
-
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
|
-
second
-
return the 2nd element
-
tail
-
return the tail, rest or cdr - whatever you wonna call it
-
third
-
return the 3rd element
Usage example(s):
(BorgReader readFromString:'(10 20 30 40)') first
(BorgReader readFromString:'(10 20 30 40)') second
(BorgReader readFromString:'(10 20 30 40)') third
(BorgReader readFromString:'(10 20 30 40)') nth:1
(BorgReader readFromString:'(10 20 30 40)') nth:3
(BorgReader readFromString:'(10 20 30 40)') last
|
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: newCar
-
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: newCdr
-
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
-
setCar: newCar
-
set the head, first or car - whatever you wonna call it
-
setCar: carArg setCdr: cdrArg
-
set both car and cdr
-
setCdr: newCdr
-
set the tail, second or cdr - whatever you wonna call it
comparing
-
= aCons
-
warning: this might be expensive for long lists
-
hash
-
warning: this might be expensive for long lists
copying
-
, aCollection
-
return a new list by concatenating elements from the receiver and the argument
Usage example(s):
|l1 l2|
l1 := Cons fromArray:#(1 2 3 4).
l2 := Cons fromArray:#(10 20 30 40).
l1 , l2
|
Usage example(s):
|l1 l2|
l1 := Cons fromArray:#(1 2 3 4).
l1 , #(10 20 30 40)
|
-
copyEmpty: size
-
(comment from inherited method)
return a copy of the receiver with no elements, but space for
size elements. This is used by copying and enumeration methods
to get a new instance which is similar to the receiver.
This method should be redefined in subclasses with instance
variables, which should be put into the copy too.
For example, SortedCollection has to copy its sortBlock into the
new collection.
-
copyEmptyAndGrow: size
-
(comment from inherited method)
return a copy of the receiver with size nil elements.
This is used by copying and enumeration methods
to get a new instance which is similar to the receiver.
enumerating
-
do: aBlock
-
evaluate aBlock for each car;
raises an error if the receiver is a degenerated list (i.e. last cdr notNil)
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 front of 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
-
displayOn: aStream
-
(comment from inherited method)
print a representation of the receiver on aGCOrStream for display in inspectors etc.
-
displayString
-
(comment from inherited method)
return a string used when displaying the receiver in a view;
for example an Inspector. This is usually the same as printString,
but sometimes redefined for a better look.
Notice: displayString and displayOn: are for developers, debugging and inspectors,
whereas printString and printOn: are for the program to print data.
Note: the base method (used by the inspector) is #displayOn:.
So you should implement #displayOn: instead of #displayString in subclasses.
-
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
Usage example(s):
|n1 n2 n3 n4 n5|
n1 := Cons new car:1.
n2 := Cons new car:2.
n3 := Cons new car:3.
n4 := Cons new car:4.
n5 := Cons new car:5.
n1 cdr: n2.
n2 cdr: n3.
n3 cdr: n4.
n4 cdr: n5.
n1 beginAndSizeOfCycle.
n5 cdr: n3.
n1 beginAndSizeOfCycle.
|
-
isCyclic
-
true if the list contains a cycle
Usage example(s):
|n1 n2 n3 n4 n5|
n1 := Cons new car:1.
n2 := Cons new car:2.
n3 := Cons new car:3.
n4 := Cons new car:4.
n5 := Cons new car:5.
n1 cdr: n2.
n2 cdr: n3.
n3 cdr: n4.
n4 cdr: n5.
n1 isCyclic.
n5 cdr: n3.
n1 isCyclic.
|
-
length
-
the list's length
Usage example(s):
(Cons fromArray:#(1)) length
(Cons fromArray:#(1 2 3 4)) length
(Cons car:1 cdr:2) length --> error (degenerated list)
|
-
size
-
the list's length
streaming
-
readStream
-
(comment from inherited method)
return a stream for reading from the receiver
testing
-
isCons
-
-
isImmutable
-
(comment from inherited method)
experimental - not yet usable; do not use.
For now the #isImmutable flag prohibits only #become*.
-
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
|
|