Cons.st
author Claus Gittinger <cg@exept.de>
Fri, 26 Mar 2010 00:18:37 +0100
changeset 2437 58c3242dc76d
parent 1627 5bc113e1f2f3
child 2448 f21170d2e545
permissions -rw-r--r--
added: #= #hash
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
"{ Package: 'stx:libbasic2' }"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
SequenceableCollection subclass:#Cons
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:'car cdr'
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	category:'Collections-Linked'
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
copyright
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
 COPYRIGHT (c) 2002 by eXept Software AG
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
              All Rights Reserved
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 This software is furnished under a license and may be used
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 hereby transferred.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
"
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
documentation
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
    A pair as in lisp.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
    Create with:
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    41
        a !! b
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    42
    or:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    43
        Cons car:a cdr:b
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    44
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    45
    Conses are not heavily used by Smalltalk (actually: not at all).
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
    46
    Consider this a demo class.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
    [author:]
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
        Claus Gittinger
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    [see also:]
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
"
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    54
!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    55
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    56
examples
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    57
"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    58
                                                                        [exBegin]
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    59
    |p1 p2 p3|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    60
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    61
    p3 := Cons car:3 cdr:nil.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    62
    p2 := Cons car:2 cdr:p3.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    63
    p1 := Cons car:1 cdr:p2.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    64
    p1 head.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    65
    p1 tail.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    66
    p1 size.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    67
    p1 do:[:each | Transcript showCR:each].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    68
    p1 at:2
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    69
                                                                        [exEnd]
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    70
"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!Cons class methodsFor:'instance creation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
car:carArg cdr:cdrArg
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    ^ self basicNew car:carArg cdr:cdrArg
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
fromArray:anArray
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    |p last first|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    anArray do:[:el |
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
        p := self car:el cdr:nil.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        first isNil ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
            first := p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
        ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
            last cdr:p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
        ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        last := p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    ^ first.
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 2 3 4)   
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    95
     Cons fromArray:#()    
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    96
     Cons fromArray:#(1)    
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
    97
     Cons fromArray:(1 to:10000)    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    "
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
!Cons methodsFor:'accessing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
at:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   104
    "for collection compatibility:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   105
     a slow indexed accessor"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   106
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   107
    ^ (self nth:n)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   110
     (Cons fromArray:#(1))       at:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   111
     (Cons fromArray:#(1 2 3 4)) at:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   112
     (Cons fromArray:#(1 2 3 4)) at:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   113
     (Cons fromArray:#(1 2 3 4)) at:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   114
     (Cons fromArray:#(1 2 3 4)) at:5  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    "
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
at:n put:newValue
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   119
    "destructive: 
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   120
     for collection compatibility: a slow indexed accessor"
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   121
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   122
    (self nthPair:n) car:newValue.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
    ^ newValue.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
     |l|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   128
     l := Cons fromArray:#(1 2 3 4).
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
     l at:1 put:'one'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
     l at:3 put:'three'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
     l       
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   135
first
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   136
    "return the head, first or car - whatever you wonna call it"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   137
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   138
    ^ self car
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   139
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   140
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   141
head
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   142
    "return the head, first or car - whatever you wonna call it"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   143
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   144
    ^ self car
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   145
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   146
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
last
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   148
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   149
     return the last element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   150
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
    ].
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   157
    ^ p car
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   160
     (Cons fromArray:#(1))       last     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   161
     (Cons fromArray:#(1 2 3 4)) last    
1043
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
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
nth:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   166
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   167
     return the nth element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   168
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   169
    ^ (self nthPair:n) car
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   172
     (Cons fromArray:#(1))       nth:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   173
     (Cons fromArray:#(1 2 3 4)) nth:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   174
     (Cons fromArray:#(1 2 3 4)) nth:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   175
     (Cons fromArray:#(1 2 3 4)) nth:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   176
     (Cons fromArray:#(1 2 3 4)) nth:5  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   177
     (Cons fromArray:#( ))       nth:1  -> error    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   179
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   180
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   181
rest
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   182
    "return the head, first or car - whatever you wonna call it"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   183
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   184
    ^ self cdr
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   185
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   186
1243
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   187
reversed
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   188
    "for lispers:
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   189
     return a new list with the cars in reverse order"
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   190
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   191
"/ for now, tail recursion is not yet optimized by the st/x jitter...
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   192
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   193
"/    |rev|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   194
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   195
"/    rev := [:lst :acc |
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   196
"/                lst isNil ifTrue:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   197
"/                    acc
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   198
"/                ] ifFalse:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   199
"/                    rev value:(lst tail)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   200
"/                        value:(Cons car:(lst head) cdr:acc)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   201
"/                ]
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   202
"/           ].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   203
"/    ^ rev value:self value:nil
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   204
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   205
    | lst acc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   206
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   207
    lst := self.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   208
    acc := nil.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   209
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   210
    [
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   211
        |nLst nAcc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   212
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   213
        lst isNil ifTrue:[ ^ acc].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   214
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   215
        nLst := lst tail.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   216
        nAcc := Cons car:(lst head) cdr:acc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   217
        lst := nLst.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   218
        acc := nAcc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   219
   ] loop
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   220
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   221
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   222
     (Cons fromArray:#(1))       reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   223
     (Cons fromArray:#(1 2))     reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   224
     (Cons fromArray:#(1 2 3 4)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   225
     (Cons fromArray:(1 to:10000)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   226
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   227
!
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   228
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   229
tail
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   230
    "return the tail, second or cdr - whatever you wonna call it"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   231
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   232
    ^ self cdr
1043
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
!Cons methodsFor:'accessing - basic'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
car
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   238
    "return the head, first or car - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
    ^ car
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
car:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   244
    "set the head, first or car - whatever you wonna call it"
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
    car := something.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
car:carArg cdr:cdrArg 
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   250
    "set both car and cdr"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
    car := carArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
    cdr := cdrArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
cdr
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   257
    "return the tail, second or cdr - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
    ^ cdr
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
cdr:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   263
    "set the tail, second or cdr - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
    cdr := something.
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   266
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   267
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   268
first:carArg rest:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   269
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   270
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   271
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   272
    cdr := cdrArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   273
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   274
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   275
head:carArg tail:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   276
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   277
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   278
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   279
    cdr := cdrArg.
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   280
!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   281
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   282
nthPair:n
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   283
    "a helper:
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   284
     return the nth pair of a list"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   285
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   286
    |cnt p|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   287
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   288
    cnt := n.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   289
    p := self.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   290
    [
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   291
        cnt := cnt - 1.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   292
        cnt == 0 ifTrue:[^ p].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   293
        p := p cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   294
        p isNil ifTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   295
            self error:'no such element' mayProceed:true.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   296
            ^ nil
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   297
        ].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   298
    ] loop.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   299
! !
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   300
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   301
!Cons methodsFor:'comparing'!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   302
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   303
= aCons
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   304
    ^ aCons class == self class
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   305
    and:[ car = aCons car
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   306
    and:[ cdr = aCons cdr ]]
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   307
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   308
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   309
hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   310
    ^ car hash bitXor: cdr hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   311
! !
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   312
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   313
!Cons methodsFor:'enumerating'!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   314
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   315
do:aBlock
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   316
    |ptr|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   317
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   318
    aBlock value:car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   319
    ptr := cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   320
    [ ptr notNil ] whileTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   321
        aBlock value:ptr car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   322
        ptr := ptr cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   323
    ].
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
!Cons methodsFor:'list processing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
append:aCons
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   329
    "for lispers:
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   330
     append the arg. Return a new list, where the 2nd part is shared.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   331
     Destructive: the receivers last cdr is modified."
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   332
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
    p cdr:aCons.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
    ^ self
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   343
     (Cons fromArray:#(1 2 3 4)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   344
        append:(Cons fromArray:#(5 6 7 8)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   345
    "
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   346
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   347
    "sharing demonstrated:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   348
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   349
     |a b ab|
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   350
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   351
     a := Cons fromArray:#(1 2 3 4).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   352
     b := Cons fromArray:#(5 6 7 8).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   353
     ab := a append:b.
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   354
     b car:'five'.
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   355
     ab      
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   356
    "
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   357
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   358
    "destruction demonstrated:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   359
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   360
     |a b ab|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   361
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   362
     a := Cons fromArray:#(1 2 3 4).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   363
     b := Cons fromArray:#(5 6 7 8).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   364
     ab := a append:b.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   365
     a  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   367
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   368
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   369
take:nTaken
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   370
    "for lispers:
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   371
     take n elements from the list; return a new list"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   372
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   373
    |nRemain l rslt lastCons cons|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   374
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   375
    nTaken > 0 ifTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   376
        "/ avoiding recursion here...
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   377
        "/ instead of:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   378
        "/        ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   379
        "/ we do:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   380
        nRemain := nTaken.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   381
        l := self.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   382
        rslt := lastCons := Cons car:(l car) cdr:nil.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   383
        [nRemain > 1] whileTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   384
            l := l cdr.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   385
            cons := Cons car:(l car) cdr:nil.    
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   386
            lastCons cdr:cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   387
            lastCons := cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   388
            nRemain := nRemain - 1.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   389
        ].
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   390
        ^ rslt.
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   391
    ].
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   392
    ^ nil
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   393
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   394
    "
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   395
     (Cons fromArray:#(1 2 3 4)) take:3  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   396
     (Cons fromArray:#(1)) take:0  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   397
     (Cons fromArray:#()) take:3  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   398
     (Cons fromArray:(1 to: 1000)) take:999  
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   399
    "
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
!Cons methodsFor:'printing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
displayString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
    ^ self printString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
printOn:aStream
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
    (car isLazyValue not and:[ car isCons ]) ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
        aStream nextPutAll:'('.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
        car printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
        aStream nextPutAll:')'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
    ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
        car printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
1073
1b0886e40a5a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1043
diff changeset
   417
    aStream nextPutAll:'!!'.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
    (cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
        aStream nextPutAll:'('.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
        cdr printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
        aStream nextPutAll:')'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
    ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
        cdr printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
!Cons methodsFor:'queries'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
isCons
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   431
    ^ true
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
size
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   435
    "for smalltalkers: the lists length"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   436
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
    |l p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
    l := 1.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
        l := l + 1.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
    ^ l
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   448
     (Cons fromArray:#( )) size    
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   449
     (Cons fromArray:#(1)) size     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   450
     (Cons fromArray:#(1 2 3 4)) size    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   454
!Cons methodsFor:'streaming'!
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   455
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   456
readStream
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   457
    ^ ConsStream on:self.
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   458
! !
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   459
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
version
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   463
    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.10 2010-03-25 23:18:37 cg Exp $'
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   464
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   465
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   466
version_CVS
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   467
    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.10 2010-03-25 23:18:37 cg Exp $'
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
! !