Cons.st
author Claus Gittinger <cg@exept.de>
Tue, 27 Sep 2011 11:42:46 +0200
changeset 2662 b3249d9d9134
parent 2660 2c8509afbfc0
child 2753 aaebdee8dba9
permissions -rw-r--r--
changed: #documentation
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:]
2662
b3249d9d9134 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2660
diff changeset
    49
        Claus Gittinger (Jun 2002)
1043
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
    "
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
    99
!
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   100
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   101
makeList:size
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   102
    |first prev this|
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   103
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   104
    size == 0 ifTrue:[^ nil].
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   105
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   106
    first := prev := self car:nil cdr:nil.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   107
    2 to:size do:[:n |
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   108
        this := self car:nil cdr:nil.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   109
        prev cdr:this.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   110
        prev := this
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   111
    ].
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   112
    ^ first
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   113
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   114
    "
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   115
     (self makeList:0) size    
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   116
     (self makeList:1) size    
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   117
     (self makeList:100) size
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   118
     (self makeList:1000) size 
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   119
     (self makeList:10000) size 
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   120
    "
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   121
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   122
    "Created: / 28-04-2011 / 00:37:11 / cg"
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   123
    "Modified: / 29-04-2011 / 10:38:11 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   126
!Cons class methodsFor:'sExpressions'!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   127
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   128
readLispFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   129
    aStream skipSeparators.
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   130
    aStream atEnd ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   131
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   132
    aStream peek ==$; ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   133
        "/ comment
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   134
        aStream skipLine.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   135
        thisContext restart
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   136
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   137
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   138
    aStream peek ==$( ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   139
        ^ self realLispListFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   140
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   141
    ^ self realLispAtomFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   142
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   143
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   144
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   145
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   146
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   147
    "Created: / 08-08-2010 / 17:07:49 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   148
!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   149
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   150
realLispAtomFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   151
    |atom|
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   152
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   153
    aStream skipSeparators.
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   154
    aStream atEnd ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   155
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   156
    aStream peek isDigit ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   157
        ^ Number readFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   158
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   159
    atom := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   160
    ^ atom asSymbol
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   161
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   162
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   163
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   164
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   165
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   166
    "Created: / 08-08-2010 / 17:15:18 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   167
!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   168
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   169
realLispListFrom:aStream
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   170
    |first this prev element|
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   171
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   172
    aStream next.   "/ the leading '('
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   173
    [
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   174
        aStream skipSeparators.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   175
        aStream peek ~= $)
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   176
    ] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   177
        element := self readLispFrom:aStream.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   178
        this := Cons car:element cdr:nil.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   179
        prev isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   180
            first := this
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   181
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   182
            prev cdr:this.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   183
        ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   184
        prev := this.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   185
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   186
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   187
    aStream next.   "/ the trailing ')'
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   188
    ^ first.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   189
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   190
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   191
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   192
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   193
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   194
    "Modified: / 08-08-2010 / 17:15:54 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   195
! !
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   196
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
!Cons methodsFor:'accessing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
at:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   200
    "for collection compatibility:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   201
     a slow indexed accessor"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   202
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   203
    ^ (self nth:n)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   206
     (Cons fromArray:#(1))       at:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   207
     (Cons fromArray:#(1 2 3 4)) at:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   208
     (Cons fromArray:#(1 2 3 4)) at:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   209
     (Cons fromArray:#(1 2 3 4)) at:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   210
     (Cons fromArray:#(1 2 3 4)) at:5  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
at:n put:newValue
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   215
    "destructive: 
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   216
     for collection compatibility: a slow indexed accessor"
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   217
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   218
    (self nthPair:n) car:newValue.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
    ^ newValue.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
     |l|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   224
     l := Cons fromArray:#(1 2 3 4).
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
     l at:1 put:'one'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
     l at:3 put:'three'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
     l       
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   231
first
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   232
    "return the head, first or car - whatever you wonna call it"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   233
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   234
    ^ car
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   235
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   236
    "Modified: / 08-08-2010 / 17:04:23 / cg"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   237
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   238
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   239
head
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   240
    "return the head, first or car - whatever you wonna call it"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   241
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   242
    ^ car
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   243
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   244
    "Modified: / 08-08-2010 / 17:04:20 / cg"
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   245
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   246
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
last
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   248
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   249
     return the last element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   250
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
    ].
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   257
    ^ p car
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
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   260
     (Cons fromArray:#(1))       last     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   261
     (Cons fromArray:#(1 2 3 4)) last    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
nth:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   266
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   267
     return the nth element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   268
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   269
    ^ (self nthPair:n) car
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   272
     (Cons fromArray:#(1))       nth:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   273
     (Cons fromArray:#(1 2 3 4)) nth:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   274
     (Cons fromArray:#(1 2 3 4)) nth:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   275
     (Cons fromArray:#(1 2 3 4)) nth:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   276
     (Cons fromArray:#(1 2 3 4)) nth:5  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   277
     (Cons fromArray:#( ))       nth:1  -> error    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   279
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   280
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   281
rest
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   282
    "return the tail, rest or cdr - whatever you wonna call it"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   283
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   284
    ^ cdr
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   285
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   286
    "Modified: / 08-08-2010 / 17:04:48 / cg"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   287
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   288
1243
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   289
reversed
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   290
    "for lispers:
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   291
     return a new list with the cars in reverse order"
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   292
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   293
"/ 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
   294
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   295
"/    |rev|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   296
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   297
"/    rev := [:lst :acc |
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   298
"/                lst isNil ifTrue:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   299
"/                    acc
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   300
"/                ] ifFalse:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   301
"/                    rev value:(lst tail)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   302
"/                        value:(Cons car:(lst head) cdr:acc)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   303
"/                ]
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   304
"/           ].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   305
"/    ^ rev value:self value:nil
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   306
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   307
    | lst acc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   308
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   309
    lst := self.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   310
    acc := nil.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   311
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   312
    [
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   313
        |nLst nAcc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   314
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   315
        lst isNil ifTrue:[ ^ acc].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   316
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   317
        nLst := lst tail.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   318
        nAcc := Cons car:(lst head) cdr:acc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   319
        lst := nLst.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   320
        acc := nAcc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   321
   ] loop
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   322
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   323
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   324
     (Cons fromArray:#(1))       reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   325
     (Cons fromArray:#(1 2))     reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   326
     (Cons fromArray:#(1 2 3 4)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   327
     (Cons fromArray:(1 to:10000)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   328
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   329
!
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   330
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   331
tail
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   332
    "return the tail, rest or cdr - whatever you wonna call it"
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   333
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   334
    ^ cdr
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   335
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   336
    "Modified: / 08-08-2010 / 17:04:59 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
!Cons methodsFor:'accessing - basic'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   341
cadddr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   342
    "return the fourth element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   343
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   344
    ^ cdr cdr cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   345
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   346
    "Created: / 08-08-2010 / 17:29:40 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   347
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   348
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   349
caddr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   350
    "return the third element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   351
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   352
    ^ cdr cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   353
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   354
    "Created: / 08-08-2010 / 17:29:32 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   355
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   356
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   357
cadr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   358
    "return the second element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   359
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   360
    ^ cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   361
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   362
    "Created: / 08-08-2010 / 17:29:25 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   363
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   364
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
car
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   366
    "return the head, first or car - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
    ^ car
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
car:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   372
    "set the head, first or car - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
    car := something.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
car:carArg cdr:cdrArg 
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   378
    "set both car and cdr"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
    car := carArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
    cdr := cdrArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
2472
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   384
cddr
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   385
    "return the rest after the second element"
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   386
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   387
    ^ cdr cdr
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   388
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   389
    "Created: / 08-08-2010 / 17:47:11 / cg"
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   390
!
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   391
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
cdr
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   393
    "return the tail, second or cdr - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
    ^ cdr
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
cdr:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   399
    "set the tail, second or cdr - whatever you wonna call it"
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
    cdr := something.
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   402
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   403
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   404
first:carArg rest:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   405
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   406
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   407
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   408
    cdr := cdrArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   409
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   410
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   411
head:carArg tail:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   412
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   413
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   414
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   415
    cdr := cdrArg.
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   416
!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   417
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   418
nthPair:n
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   419
    "a helper:
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   420
     return the nth pair of a list"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   421
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   422
    |cnt p|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   423
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   424
    cnt := n.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   425
    p := self.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   426
    [
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   427
        cnt := cnt - 1.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   428
        cnt == 0 ifTrue:[^ p].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   429
        p := p cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   430
        p isNil ifTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   431
            self error:'no such element' mayProceed:true.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   432
            ^ nil
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   433
        ].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   434
    ] loop.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   435
! !
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   436
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   437
!Cons methodsFor:'comparing'!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   438
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   439
= aCons
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   440
    ^ aCons class == self class
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   441
    and:[ car = aCons car
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   442
    and:[ cdr = aCons cdr ]]
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   443
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   444
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   445
hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   446
    ^ car hash bitXor: cdr hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   447
! !
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   448
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   449
!Cons methodsFor:'enumerating'!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   450
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   451
do:aBlock
2471
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   452
    "evaluate aBlock for each car"
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   453
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   454
    |ptr|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   455
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   456
    aBlock value:car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   457
    ptr := cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   458
    [ ptr notNil ] whileTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   459
        aBlock value:ptr car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   460
        ptr := ptr cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   461
    ].
2471
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   462
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   463
    "Modified: / 08-08-2010 / 17:33:44 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
!Cons methodsFor:'list processing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   467
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
append:aCons
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   469
    "for lispers:
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   470
     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
   471
     Destructive: the receivers last cdr is modified."
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   472
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
    p cdr:aCons.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
    ^ self
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   483
     (Cons fromArray:#(1 2 3 4)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   484
        append:(Cons fromArray:#(5 6 7 8)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   485
    "
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   486
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   487
    "sharing demonstrated:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   488
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   489
     |a b ab|
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   490
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   491
     a := Cons fromArray:#(1 2 3 4).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   492
     b := Cons fromArray:#(5 6 7 8).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   493
     ab := a append:b.
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   494
     b car:'five'.
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   495
     ab      
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   496
    "
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   497
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   498
    "destruction demonstrated:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   499
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   500
     |a b ab|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   501
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   502
     a := Cons fromArray:#(1 2 3 4).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   503
     b := Cons fromArray:#(5 6 7 8).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   504
     ab := a append:b.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   505
     a  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   506
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   507
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   508
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   509
take:nTaken
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   510
    "for lispers:
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   511
     take n elements from the list; return a new list"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   512
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   513
    |nRemain l rslt lastCons cons|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   514
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   515
    nTaken > 0 ifTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   516
        "/ avoiding recursion here...
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   517
        "/ instead of:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   518
        "/        ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   519
        "/ we do:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   520
        nRemain := nTaken.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   521
        l := self.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   522
        rslt := lastCons := Cons car:(l car) cdr:nil.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   523
        [nRemain > 1] whileTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   524
            l := l cdr.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   525
            cons := Cons car:(l car) cdr:nil.    
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   526
            lastCons cdr:cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   527
            lastCons := cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   528
            nRemain := nRemain - 1.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   529
        ].
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   530
        ^ rslt.
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   531
    ].
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   532
    ^ nil
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   533
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   534
    "
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   535
     (Cons fromArray:#(1 2 3 4)) take:3  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   536
     (Cons fromArray:#(1)) take:0  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   537
     (Cons fromArray:#()) take:3  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   538
     (Cons fromArray:(1 to: 1000)) take:999  
2656
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   539
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   540
     |gen allNumbers|
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   541
     gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   542
     allNumbers := gen value:1. 
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   543
     allNumbers take:10
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   544
    "
2656
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   545
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   546
    "Modified (comment): / 27-09-2011 / 11:31:38 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   547
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   548
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   549
!Cons methodsFor:'printing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   550
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
displayString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
    ^ self printString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
printOn:aStream
2448
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   556
    thisContext isRecursive ifTrue:[ 
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   557
        'Cons [error]: printOn: of self referencing collection.' errorPrintCR.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   558
        aStream nextPutAll:'#("recursive")'.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   559
        ^ self.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   560
    ].
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   561
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
    (car isLazyValue not and:[ car isCons ]) ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
        aStream nextPutAll:'('.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
        car printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
        aStream nextPutAll:')'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
    ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
        car printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
1073
1b0886e40a5a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1043
diff changeset
   570
    aStream nextPutAll:'!!'.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
    (cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
        aStream nextPutAll:'('.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
        cdr printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
        aStream nextPutAll:')'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
    ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
        cdr printOn:aStream.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
    ].
2448
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   579
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   580
    "Modified: / 18-05-2010 / 10:25:49 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
!Cons methodsFor:'queries'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
isCons
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
    ^ true
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
size
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   590
    "for smalltalkers: the lists length"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   591
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
    |l p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
    l := 1.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
        l := l + 1.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
    ^ l
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   603
     (Cons fromArray:#( )) size    
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   604
     (Cons fromArray:#(1)) size     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   605
     (Cons fromArray:#(1 2 3 4)) size    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   609
!Cons methodsFor:'streaming'!
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   610
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   611
readStream
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   612
    ^ ConsStream on:self.
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   613
! !
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   614
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
version
2662
b3249d9d9134 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2660
diff changeset
   618
    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.20 2011-09-27 09:42:46 cg Exp $'
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   619
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   620
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   621
version_CVS
2662
b3249d9d9134 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2660
diff changeset
   622
    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.20 2011-09-27 09:42:46 cg Exp $'
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   623
! !