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