*** empty log message ***
authorClaus Gittinger <cg@exept.de>
Thu, 13 Apr 2006 17:21:03 +0200
changeset 1627 5bc113e1f2f3
parent 1626 8a0e7e7151b9
child 1628 0c7d951c6f99
*** empty log message ***
Cons.st
--- a/Cons.st	Fri Apr 07 18:29:59 2006 +0200
+++ b/Cons.st	Thu Apr 13 17:21:03 2006 +0200
@@ -52,6 +52,23 @@
     [see also:]
 
 "
+!
+
+examples
+"
+                                                                        [exBegin]
+    |p1 p2 p3|
+
+    p3 := Cons car:3 cdr:nil.
+    p2 := Cons car:2 cdr:p3.
+    p1 := Cons car:1 cdr:p2.
+    p1 head.
+    p1 tail.
+    p1 size.
+    p1 do:[:each | Transcript showCR:each].
+    p1 at:2
+                                                                        [exEnd]
+"
 ! !
 
 !Cons class methodsFor:'instance creation'!
@@ -88,7 +105,7 @@
     "for collection compatibility:
      a slow indexed accessor"
 
-    ^ (self nth:n) car
+    ^ (self nth:n)
 
     "
      (Cons fromArray:#(1))       at:1     
@@ -100,10 +117,10 @@
 !
 
 at:n put:newValue
-    "for collection compatibility:
-     a slow indexed accessor"
+    "destructive: 
+     for collection compatibility: a slow indexed accessor"
 
-    (self nth:n) car:newValue.
+    (self nthPair:n) car:newValue.
     ^ newValue.
 
     "
@@ -150,19 +167,7 @@
     "for lispers:
      return the nth element of a list"
 
-    |cnt p|
-
-    cnt := n.
-    p := self.
-    [
-        cnt := cnt - 1.
-        cnt == 0 ifTrue:[^ p car].
-        p := p cdr.
-        p isNil ifTrue:[
-            self error:'no such element' mayProceed:true.
-            ^ nil
-        ].
-    ] loop.
+    ^ (self nthPair:n) car
 
     "
      (Cons fromArray:#(1))       nth:1     
@@ -273,6 +278,38 @@
 
     car := carArg.
     cdr := cdrArg.
+!
+
+nthPair:n
+    "a helper:
+     return the nth pair of a list"
+
+    |cnt p|
+
+    cnt := n.
+    p := self.
+    [
+        cnt := cnt - 1.
+        cnt == 0 ifTrue:[^ p].
+        p := p cdr.
+        p isNil ifTrue:[
+            self error:'no such element' mayProceed:true.
+            ^ nil
+        ].
+    ] loop.
+! !
+
+!Cons methodsFor:'enumerating'!
+
+do:aBlock
+    |ptr|
+
+    aBlock value:car.
+    ptr := cdr.
+    [ ptr notNil ] whileTrue:[
+        aBlock value:ptr car.
+        ptr := ptr cdr.
+    ].
 ! !
 
 !Cons methodsFor:'list processing'!
@@ -412,5 +449,5 @@
 !Cons class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.8 2006-03-31 17:54:24 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.9 2006-04-13 15:21:03 cg Exp $'
 ! !