Cons.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4932 571ac375933a
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4672
8cc550f23f56 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4537
diff changeset
     1
"{ Encoding: utf8 }"
8cc550f23f56 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4537
diff changeset
     2
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
 COPYRIGHT (c) 2002 by eXept Software AG
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
              All Rights Reserved
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 This software is furnished under a license and may be used
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
 hereby transferred.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
4109
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
    16
"{ NameSpace: Smalltalk }"
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
    17
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
SequenceableCollection subclass:#Cons
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	instanceVariableNames:'car cdr'
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	classVariableNames:''
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
	category:'Collections-Linked'
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
copyright
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 COPYRIGHT (c) 2002 by eXept Software AG
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
              All Rights Reserved
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 This software is furnished under a license and may be used
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 only in accordance with the terms of that license and with the
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 be provided or otherwise made available to, or used by, any
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 other person.  No title to or ownership of the software is
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
 hereby transferred.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
documentation
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
"
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    A pair as in lisp.
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    44
        Cons car:a cdr:b
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    45
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    46
    Conses are not heavily used by Smalltalk (actually: not at all).
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
    47
    Consider this a demo class.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
    [author:]
2662
b3249d9d9134 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2660
diff changeset
    50
        Claus Gittinger (Jun 2002)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
    [see also:]
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
"
1627
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
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    57
examples
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    58
"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    59
                                                                        [exBegin]
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    60
    |p1 p2 p3|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    61
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    62
    p3 := Cons car:3 cdr:nil.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    63
    p2 := Cons car:2 cdr:p3.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    64
    p1 := Cons car:1 cdr:p2.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    65
    p1 head.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    66
    p1 tail.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    67
    p1 size.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    68
    p1 do:[:each | Transcript showCR:each].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    69
    p1 at:2
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    70
                                                                        [exEnd]
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
    71
"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
!Cons class methodsFor:'instance creation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
car:carArg cdr:cdrArg
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    ^ self basicNew car:carArg cdr:cdrArg
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
fromArray:anArray
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    |p last first|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    anArray do:[:el |
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        p := self car:el cdr:nil.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
        first isNil ifTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
            first := p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
        ] ifFalse:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
            last cdr:p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
        last := p.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    ^ first.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    95
     Cons fromArray:#(1 2 3 4)   
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    96
     Cons fromArray:#()    
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
    97
     Cons fromArray:#(1)    
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
    98
     Cons fromArray:(1 to:10000)    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
    "
2556
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
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   102
makeList:size
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   103
    |first prev this|
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   104
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   105
    size == 0 ifTrue:[^ nil].
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   106
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   107
    first := prev := self car:nil cdr:nil.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   108
    2 to:size do:[:n |
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   109
        this := self car:nil cdr:nil.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   110
        prev cdr:this.
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   111
        prev := this
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   112
    ].
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   113
    ^ first
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   114
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   115
    "
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   116
     (self makeList:0) size    
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   117
     (self makeList:1) size    
2556
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   118
     (self makeList:100) size
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   119
     (self makeList:1000) size 
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   120
     (self makeList:10000) size 
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
bdc21dece784 added: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2472
diff changeset
   123
    "Created: / 28-04-2011 / 00:37:11 / cg"
2557
35bc2d58019d comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents: 2556
diff changeset
   124
    "Modified: / 29-04-2011 / 10:38:11 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   127
!Cons class methodsFor:'sExpressions'!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   128
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   129
readLispAtomFrom:aStream
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   130
    |chars n|
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   131
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   132
    aStream skipSeparators.
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   133
    aStream atEnd ifTrue:[^ nil].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   134
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   135
    chars := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   136
    (n := Number readFromString:chars onError:nil) notNil ifTrue:[
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   137
        "/ smalltalk allows +n; scheme does not
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   138
        (chars startsWith:'+') ifFalse:[
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   139
            ^ n
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   140
        ].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   141
    ].
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   142
    ^ chars asSymbol
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   143
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   144
    "
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   145
     self readLispFrom:('(cons 1 2)' readStream).
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   146
     self readLispFrom:('(cons 1+ 2)' readStream).  
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   147
     self readLispFrom:('(cons +1 2)' readStream).
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   148
    "
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   149
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   150
    "Created: / 08-08-2010 / 17:15:18 / cg"
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   151
!
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   152
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   153
readLispFrom:aStream
4109
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   154
    [
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   155
        aStream skipSeparators.
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   156
        aStream atEnd ifTrue:[^ nil].
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   157
4109
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   158
        aStream peek ==$; ifFalse:[
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   159
            aStream peek ==$( ifTrue:[
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   160
                ^ self readLispListFrom:aStream
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   161
            ].
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   162
            ^ self readLispAtomFrom:aStream
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   163
        ].
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   164
        "/ EOL comment
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   165
        aStream skipLine.
4109
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   166
    ] loop
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   167
    
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   168
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   169
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   170
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   171
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   172
    "Created: / 08-08-2010 / 17:07:49 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   173
!
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   174
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   175
readLispListFrom:aStream
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   176
    |first this prev element|
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   177
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   178
    aStream next.   "/ the leading '('
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   179
    [
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   180
        aStream skipSeparators.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   181
        aStream peek ~= $)
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   182
    ] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   183
        element := self readLispFrom:aStream.
4247
11f26c6f2d17 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4152
diff changeset
   184
        this := self car:element cdr:nil.
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   185
        prev isNil ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   186
            first := this
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   187
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   188
            prev cdr:this.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   189
        ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   190
        prev := this.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   191
    ].
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   192
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   193
    aStream next.   "/ the trailing ')'
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   194
    ^ first.
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   195
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   196
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   197
     self readLispFrom:('(cons 1 2)' readStream).
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   198
    "
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   199
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   200
    "Modified: / 08-08-2010 / 17:15:54 / cg"
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   201
! !
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   202
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
!Cons methodsFor:'accessing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
at:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   206
    "for collection compatibility:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   207
     a slow indexed accessor"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   208
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   209
    ^ (self nth:n)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   212
     (Cons fromArray:#(1))       at:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   213
     (Cons fromArray:#(1 2 3 4)) at:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   214
     (Cons fromArray:#(1 2 3 4)) at:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   215
     (Cons fromArray:#(1 2 3 4)) at:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   216
     (Cons fromArray:#(1 2 3 4)) at:5  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
at:n put:newValue
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   221
    "destructive: 
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   222
     for collection compatibility: a slow indexed accessor"
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   223
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   224
    (self nthPair:n) car:newValue.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
    ^ newValue.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
     |l|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   230
     l := Cons fromArray:#(1 2 3 4).
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
     l at:1 put:'one'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
     l at:3 put:'three'.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
     l       
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   237
first
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   238
    "return the head, first or car - whatever you wonna call it"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   239
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   240
    ^ car
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   241
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   242
    "Modified: / 08-08-2010 / 17:04:23 / cg"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   243
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   244
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   245
head
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   246
    "return the head, first or car - whatever you wonna call it"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   247
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   248
    ^ car
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   249
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   250
    "Modified: / 08-08-2010 / 17:04:20 / cg"
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   251
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   252
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
last
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   254
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   255
     return the last element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   256
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    ].
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   263
    ^ p car
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
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   266
     (Cons fromArray:#(1))       last     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   267
     (Cons fromArray:#(1 2 3 4)) last    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
    "
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
nth:n
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   272
    "for lispers:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   273
     return the nth element of a list"
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   274
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   275
    ^ (self nthPair:n) car
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   278
     (Cons fromArray:#(1))       nth:1     
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   279
     (Cons fromArray:#(1 2 3 4)) nth:1 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   280
     (Cons fromArray:#(1 2 3 4)) nth:3  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   281
     (Cons fromArray:#(1 2 3 4)) nth:4  
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   282
     (Cons fromArray:#(1 2 3 4)) nth:5  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   283
     (Cons fromArray:#( ))       nth:1  -> error    
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   285
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   286
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   287
rest
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   288
    "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
   289
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   290
    ^ cdr
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   291
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   292
    "Modified: / 08-08-2010 / 17:04:48 / cg"
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   293
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   294
1243
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   295
reversed
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   296
    "for lispers:
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   297
     return a new list with the cars in reverse order"
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   298
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   299
"/ 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
   300
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   301
"/    |rev|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   302
"/
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   303
"/    rev := [:lst :acc |
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   304
"/                lst isNil ifTrue:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   305
"/                    acc
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   306
"/                ] ifFalse:[
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   307
"/                    rev value:(lst tail)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   308
"/                        value:(Cons car:(lst head) cdr:acc)
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   309
"/                ]
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   310
"/           ].
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   311
"/    ^ rev value:self value:nil
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
    | lst acc|
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 := self.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   316
    acc := nil.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   317
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   318
    [
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   319
        |nLst nAcc|
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   320
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   321
        lst isNil ifTrue:[ ^ acc].
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
        nLst := lst tail.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   324
        nAcc := Cons car:(lst head) cdr:acc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   325
        lst := nLst.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   326
        acc := nAcc.
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   327
   ] loop
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
     (Cons fromArray:#(1))       reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   331
     (Cons fromArray:#(1 2))     reversed     
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   332
     (Cons fromArray:#(1 2 3 4)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   333
     (Cons fromArray:(1 to:10000)) reversed    
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   334
    "
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   335
!
15058ebc321b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1237
diff changeset
   336
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   337
tail
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   338
    "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
   339
2469
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   340
    ^ cdr
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   341
Claus Gittinger <cg@exept.de>
parents: 2448
diff changeset
   342
    "Modified: / 08-08-2010 / 17:04:59 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
4537
e93d043153d3 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4526
diff changeset
   345
!Cons methodsFor:'accessing-basic'!
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
2470
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   347
cadddr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   348
    "return the fourth element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   349
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   350
    ^ cdr cdr cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   351
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   352
    "Created: / 08-08-2010 / 17:29:40 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   353
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   354
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   355
caddr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   356
    "return the third element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   357
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   358
    ^ cdr cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   359
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   360
    "Created: / 08-08-2010 / 17:29:32 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   361
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   362
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   363
cadr
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   364
    "return the second element"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   365
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   366
    ^ cdr car
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   367
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   368
    "Created: / 08-08-2010 / 17:29:25 / cg"
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   369
!
Claus Gittinger <cg@exept.de>
parents: 2469
diff changeset
   370
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
car
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   372
    "return 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
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:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   378
    "set the head, first or car - whatever you wonna call it"
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 := something.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
car:carArg cdr:cdrArg 
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   384
    "set both car and cdr"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
    car := carArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
    cdr := cdrArg.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
2472
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   390
cddr
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   391
    "return the rest after the second element"
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   392
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   393
    ^ cdr cdr
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   394
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   395
    "Created: / 08-08-2010 / 17:47:11 / cg"
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   396
!
bdfac3129628 added: #cddr
Claus Gittinger <cg@exept.de>
parents: 2471
diff changeset
   397
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
cdr
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   399
    "return 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
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
cdr:something
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   405
    "set the tail, second or cdr - whatever you wonna call it"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
    cdr := something.
1623
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   408
!
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
first:carArg rest:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   411
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   412
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   413
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   414
    cdr := cdrArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   415
!
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   416
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   417
head:carArg tail:cdrArg 
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   418
    "set both car and cdr"
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   419
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   420
    car := carArg.
d93980e41a6a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1377
diff changeset
   421
    cdr := cdrArg.
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   422
!
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
nthPair:n
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   425
    "a helper:
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   426
     return the nth pair of a list"
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   427
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   428
    |cnt p|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   429
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   430
    cnt := n.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   431
    p := self.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   432
    [
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   433
        cnt := cnt - 1.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   434
        cnt == 0 ifTrue:[^ p].
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   435
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   436
        p := p cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   437
        p isNil ifTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   438
            self error:'no such element' mayProceed:true.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   439
            ^ nil
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   440
        ].
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   441
    ] loop.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   442
! !
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   443
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   444
!Cons methodsFor:'comparing'!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   445
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   446
= aCons
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   447
    ^ aCons class == self class
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   448
    and:[ car = aCons car
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   449
    and:[ cdr = aCons cdr ]]
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   450
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   451
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   452
hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   453
    ^ car hash bitXor: cdr hash
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   454
! !
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   455
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   456
!Cons methodsFor:'enumerating'!
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   457
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   458
do:aBlock
2471
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   459
    "evaluate aBlock for each car"
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   460
1627
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   461
    |ptr|
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   462
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   463
    aBlock value:car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   464
    ptr := cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   465
    [ ptr notNil ] whileTrue:[
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   466
        aBlock value:ptr car.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   467
        ptr := ptr cdr.
5bc113e1f2f3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1623
diff changeset
   468
    ].
2471
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   469
d3561a593169 changed: #do:
Claus Gittinger <cg@exept.de>
parents: 2470
diff changeset
   470
    "Modified: / 08-08-2010 / 17:33:44 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
!Cons methodsFor:'list processing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
append:aCons
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   476
    "for lispers:
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   477
     append the arg. Return a new list, where the 2nd part is shared.
4672
8cc550f23f56 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4537
diff changeset
   478
     Destructive: the receiver's last cdr is modified."
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   479
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
    |p rest|
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
    p := self.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
    [(rest := p cdr) notNil] whileTrue:[
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
    ].
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
    p cdr:aCons.
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
    ^ self
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
    "
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   490
     (Cons fromArray:#(1 2 3 4)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   491
        append:(Cons fromArray:#(5 6 7 8)) 
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   492
    "
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   493
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   494
    "sharing demonstrated:
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   495
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   496
     |a b ab|
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   497
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   498
     a := Cons fromArray:#(1 2 3 4).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   499
     b := Cons fromArray:#(5 6 7 8).
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   500
     ab := a append:b.
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   501
     b car:'five'.
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   502
     ab      
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   503
    "
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   504
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   505
    "destruction demonstrated:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   506
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   507
     |a b ab|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   508
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   509
     a := Cons fromArray:#(1 2 3 4).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   510
     b := Cons fromArray:#(5 6 7 8).
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   511
     ab := a append:b.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   512
     a  
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
    "
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   514
!
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   515
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   516
take:nTaken
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   517
    "for lispers:
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   518
     take n elements from the list; return a new list"
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   519
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   520
    |nRemain l rslt lastCons cons|
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   521
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   522
    nTaken > 0 ifTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   523
        "/ avoiding recursion here...
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   524
        "/ instead of:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   525
        "/        ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   526
        "/ we do:
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   527
        nRemain := nTaken.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   528
        l := self.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   529
        rslt := lastCons := Cons car:(l car) cdr:nil.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   530
        [nRemain > 1] whileTrue:[
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   531
            l := l cdr.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   532
            cons := Cons car:(l car) cdr:nil.    
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   533
            lastCons cdr:cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   534
            lastCons := cons.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   535
            nRemain := nRemain - 1.
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   536
        ].
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   537
        ^ rslt.
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   538
    ].
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   539
    ^ nil
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   540
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   541
    "
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   542
     (Cons fromArray:#(1 2 3 4)) take:3  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   543
     (Cons fromArray:#(1)) take:0  
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   544
     (Cons fromArray:#()) take:3  
1248
2d0493cd8d3c *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1243
diff changeset
   545
     (Cons fromArray:(1 to: 1000)) take:999  
2656
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   546
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   547
     |gen allNumbers|
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   548
     gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   549
     allNumbers := gen value:1. 
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   550
     allNumbers take:10
1237
3814a1f983a5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1074
diff changeset
   551
    "
2656
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   552
0d2ca6713ca7 comment/format in: #take:
Claus Gittinger <cg@exept.de>
parents: 2557
diff changeset
   553
    "Modified (comment): / 27-09-2011 / 11:31:38 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
!Cons methodsFor:'printing'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
displayString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
    ^ self printString
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
printOn:aStream
2448
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   563
    thisContext isRecursive ifTrue:[ 
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   564
        'Cons [error]: printOn: of self referencing collection.' errorPrintCR.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   565
        aStream nextPutAll:'#("recursive")'.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   566
        ^ self.
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   567
    ].
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   568
4932
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   569
    aStream nextPutAll:'('.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   570
    self printRestOn:aStream.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   571
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   572
    "Modified: / 18-05-2010 / 10:25:49 / cg"
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   573
    "Modified: / 02-04-2019 / 23:59:15 / Claus Gittinger"
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   574
!
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   575
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   576
printRestOn:aStream
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   577
    thisContext isRecursive ifTrue:[ 
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   578
        'Cons [error]: printOn: of self referencing collection.' errorPrintCR.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   579
        aStream nextPutAll:'#("recursive")'.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   580
        ^ self.
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
4932
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   583
    car printOn:aStream.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   584
    cdr isNil ifTrue:[
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
        aStream nextPutAll:')'.
4932
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   586
        ^ self.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   587
    ].
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   588
    (cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   589
        aStream nextPutAll:' '.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   590
        cdr printRestOn:aStream.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   591
        ^ self.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
    ].
4932
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   593
    
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   594
    aStream nextPutAll:' . '.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   595
    cdr printOn:aStream.
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   596
    aStream nextPutAll:')'.
2448
f21170d2e545 changed: #printOn:
Claus Gittinger <cg@exept.de>
parents: 2437
diff changeset
   597
4932
571ac375933a #UI_ENHANCEMENT by cg
Claus Gittinger <cg@exept.de>
parents: 4672
diff changeset
   598
    "Created: / 02-04-2019 / 23:55:05 / Claus Gittinger"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
!Cons methodsFor:'queries'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
2753
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   603
beginAndSizeOfCycle
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   604
    "return the begin and size of a cycle, if the list contains one.
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   605
     Nil otherwise.
2753
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   606
     Floyd's cycle-finding algorithm"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   607
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   608
    |t h i loopStartIndex loopSize|
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   609
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   610
    t := self cdr. t isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   611
    h := t cdr. h isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   612
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   613
    [t ~~ h] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   614
        t := t cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   615
        h := h cdr. h isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   616
        h := h cdr. h isNil ifTrue:[^ nil].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   617
    ].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   618
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   619
    "/ both h and t are now inside the cycle,
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   620
    "/ equidistant from the start of the cycle
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   621
    t := self.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   622
    i := 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   623
    [t ~~ h] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   624
        t := t cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   625
        h := h cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   626
        i := i + 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   627
    ].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   628
    loopStartIndex := i.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   629
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   630
    loopSize := 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   631
    h := t cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   632
    [t ~~ h] whileTrue:[
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   633
        h := h cdr.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   634
        i := i + 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   635
        loopSize := loopSize + 1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   636
    ].
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   637
    
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   638
    ^ { loopStartIndex. loopSize }
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   639
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   640
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   641
     |n1 n2 n3 n4 n5|
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   642
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   643
     n1 := Cons new car:1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   644
     n2 := Cons new car:2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   645
     n3 := Cons new car:3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   646
     n4 := Cons new car:4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   647
     n5 := Cons new car:5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   648
     n1 cdr: n2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   649
     n2 cdr: n3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   650
     n3 cdr: n4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   651
     n4 cdr: n5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   652
     n1 beginAndSizeOfCycle.      
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   653
     n5 cdr: n3.    
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   654
     n1 beginAndSizeOfCycle.             
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   655
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   656
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   657
    "Created: / 27-07-2012 / 00:00:36 / cg"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   658
!
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   659
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   660
isCyclic
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   661
    "true if the list contains a cycle"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   662
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   663
    ^ self beginAndSizeOfCycle notNil
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   664
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   665
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   666
     |n1 n2 n3 n4 n5|
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   667
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   668
     n1 := Cons new car:1.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   669
     n2 := Cons new car:2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   670
     n3 := Cons new car:3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   671
     n4 := Cons new car:4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   672
     n5 := Cons new car:5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   673
     n1 cdr: n2.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   674
     n2 cdr: n3.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   675
     n3 cdr: n4.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   676
     n4 cdr: n5.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   677
     n1 isCyclic.
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   678
     n5 cdr: n3.    
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   679
     n1 isCyclic.   
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   680
    "
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   681
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   682
    "Created: / 26-07-2012 / 23:32:52 / cg"
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   683
!
Claus Gittinger <cg@exept.de>
parents: 2662
diff changeset
   684
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   685
size
3307
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   686
    "the list's length"
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   687
3307
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   688
    |len p rest|
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
3307
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   690
    len := 1.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
    p := self.
3309
fca930a90261 class: Cons
Claus Gittinger <cg@exept.de>
parents: 3307
diff changeset
   692
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   693
    [
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   694
        p isLazyValue not
4526
5ca205fde08f #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4247
diff changeset
   695
        and:[ (rest := p cdr) isCons ]
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   696
    ] whileTrue:[
3307
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   697
        len := len + 1.
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
        p := rest
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
    ].
3307
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   700
    ^ len
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   701
4298986dd5de class: Cons
Claus Gittinger <cg@exept.de>
parents: 3269
diff changeset
   702
    "
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   703
     (Cons fromArray:#(1)) size       
1074
cb147ae2e03c comment
Claus Gittinger <cg@exept.de>
parents: 1073
diff changeset
   704
     (Cons fromArray:#(1 2 3 4)) size    
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   705
     (Cons car:1 cdr:2) size          --> error (degenerated list)
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   706
    "
4526
5ca205fde08f #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4247
diff changeset
   707
5ca205fde08f #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4247
diff changeset
   708
    "Modified: / 25-10-2017 / 12:43:38 / cg"
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
! !
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   710
1377
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   711
!Cons methodsFor:'streaming'!
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   712
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   713
readStream
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   714
    ^ ConsStream on:self.
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   715
! !
150000bb9d20 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1248
diff changeset
   716
3310
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   717
!Cons methodsFor:'testing'!
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   718
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   719
isCons
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   720
    ^ true
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   721
!
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   722
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   723
isLazy
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   724
    ^ false
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   725
! !
d33825f03485 class: Explainer
Claus Gittinger <cg@exept.de>
parents: 3309
diff changeset
   726
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   727
!Cons class methodsFor:'documentation'!
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   728
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   729
version
4109
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   730
    ^ '$Header$'
2437
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   731
!
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   732
Claus Gittinger <cg@exept.de>
parents: 1627
diff changeset
   733
version_CVS
4109
f76b70ec4c1d #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3310
diff changeset
   734
    ^ '$Header$'
1043
544a0829a59b initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   735
! !
3269
3f964738277d class: Cons
Claus Gittinger <cg@exept.de>
parents: 2753
diff changeset
   736