Cons.st
changeset 2469 67e364844a2b
parent 2448 f21170d2e545
child 2470 0b735c9bc90e
equal deleted inserted replaced
2468:36c571b96063 2469:67e364844a2b
    96      Cons fromArray:#(1)    
    96      Cons fromArray:#(1)    
    97      Cons fromArray:(1 to:10000)    
    97      Cons fromArray:(1 to:10000)    
    98     "
    98     "
    99 ! !
    99 ! !
   100 
   100 
       
   101 !Cons class methodsFor:'sExpressions'!
       
   102 
       
   103 readLispFrom:aStream
       
   104     aStream skipSeparators.
       
   105     aStream peek ==$; ifTrue:[
       
   106         "/ comment
       
   107         aStream skipLine.
       
   108         thisContext restart
       
   109     ].
       
   110 
       
   111     aStream peek ==$( ifTrue:[
       
   112         ^ self realLispListFrom:aStream
       
   113     ].
       
   114     ^ self realLispAtomFrom:aStream
       
   115 
       
   116     "
       
   117      self readLispFrom:('(cons 1 2)' readStream).
       
   118     "
       
   119 
       
   120     "Created: / 08-08-2010 / 17:07:49 / cg"
       
   121 !
       
   122 
       
   123 realLispAtomFrom:aStream
       
   124     |atom|
       
   125 
       
   126     aStream skipSeparators.
       
   127     aStream peek isDigit ifTrue:[
       
   128         ^ Number readFrom:aStream
       
   129     ].
       
   130     atom := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]].
       
   131     ^ atom asSymbol
       
   132 
       
   133     "
       
   134      self readLispFrom:('(cons 1 2)' readStream).
       
   135     "
       
   136 
       
   137     "Created: / 08-08-2010 / 17:15:18 / cg"
       
   138 !
       
   139 
       
   140 realLispListFrom:aStream
       
   141     |first this prev element|
       
   142 
       
   143     aStream next.   "/ the leading '('
       
   144     [
       
   145         aStream skipSeparators.
       
   146         aStream peek ~= $)
       
   147     ] whileTrue:[
       
   148         element := self readLispFrom:aStream.
       
   149         this := Cons car:element cdr:nil.
       
   150         prev isNil ifTrue:[
       
   151             first := this
       
   152         ] ifFalse:[
       
   153             prev cdr:this.
       
   154         ].
       
   155         prev := this.
       
   156     ].
       
   157 
       
   158     aStream next.   "/ the trailing ')'
       
   159     ^ first.
       
   160 
       
   161     "
       
   162      self readLispFrom:('(cons 1 2)' readStream).
       
   163     "
       
   164 
       
   165     "Modified: / 08-08-2010 / 17:15:54 / cg"
       
   166 ! !
       
   167 
   101 !Cons methodsFor:'accessing'!
   168 !Cons methodsFor:'accessing'!
   102 
   169 
   103 at:n
   170 at:n
   104     "for collection compatibility:
   171     "for collection compatibility:
   105      a slow indexed accessor"
   172      a slow indexed accessor"
   133 !
   200 !
   134 
   201 
   135 first
   202 first
   136     "return the head, first or car - whatever you wonna call it"
   203     "return the head, first or car - whatever you wonna call it"
   137 
   204 
   138     ^ self car
   205     ^ car
       
   206 
       
   207     "Modified: / 08-08-2010 / 17:04:23 / cg"
   139 !
   208 !
   140 
   209 
   141 head
   210 head
   142     "return the head, first or car - whatever you wonna call it"
   211     "return the head, first or car - whatever you wonna call it"
   143 
   212 
   144     ^ self car
   213     ^ car
       
   214 
       
   215     "Modified: / 08-08-2010 / 17:04:20 / cg"
   145 !
   216 !
   146 
   217 
   147 last
   218 last
   148     "for lispers:
   219     "for lispers:
   149      return the last element of a list"
   220      return the last element of a list"
   177      (Cons fromArray:#( ))       nth:1  -> error    
   248      (Cons fromArray:#( ))       nth:1  -> error    
   178     "
   249     "
   179 !
   250 !
   180 
   251 
   181 rest
   252 rest
   182     "return the head, first or car - whatever you wonna call it"
   253     "return the tail, rest or cdr - whatever you wonna call it"
   183 
   254 
   184     ^ self cdr
   255     ^ cdr
       
   256 
       
   257     "Modified: / 08-08-2010 / 17:04:48 / cg"
   185 !
   258 !
   186 
   259 
   187 reversed
   260 reversed
   188     "for lispers:
   261     "for lispers:
   189      return a new list with the cars in reverse order"
   262      return a new list with the cars in reverse order"
   225      (Cons fromArray:(1 to:10000)) reversed    
   298      (Cons fromArray:(1 to:10000)) reversed    
   226     "
   299     "
   227 !
   300 !
   228 
   301 
   229 tail
   302 tail
   230     "return the tail, second or cdr - whatever you wonna call it"
   303     "return the tail, rest or cdr - whatever you wonna call it"
   231 
   304 
   232     ^ self cdr
   305     ^ cdr
       
   306 
       
   307     "Modified: / 08-08-2010 / 17:04:59 / cg"
   233 ! !
   308 ! !
   234 
   309 
   235 !Cons methodsFor:'accessing - basic'!
   310 !Cons methodsFor:'accessing - basic'!
   236 
   311 
   237 car
   312 car
   466 ! !
   541 ! !
   467 
   542 
   468 !Cons class methodsFor:'documentation'!
   543 !Cons class methodsFor:'documentation'!
   469 
   544 
   470 version
   545 version
   471     ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.11 2010-05-18 08:25:55 cg Exp $'
   546     ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.12 2010-08-08 15:16:23 cg Exp $'
   472 !
   547 !
   473 
   548 
   474 version_CVS
   549 version_CVS
   475     ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.11 2010-05-18 08:25:55 cg Exp $'
   550     ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.12 2010-08-08 15:16:23 cg Exp $'
   476 ! !
   551 ! !