Cons.st
changeset 3310 d33825f03485
parent 3309 fca930a90261
child 4109 f76b70ec4c1d
--- a/Cons.st	Wed Jun 25 19:08:56 2014 +0200
+++ b/Cons.st	Wed Jun 25 19:19:00 2014 +0200
@@ -431,6 +431,7 @@
     [
         cnt := cnt - 1.
         cnt == 0 ifTrue:[^ p].
+
         p := p cdr.
         p isNil ifTrue:[
             self error:'no such element' mayProceed:true.
@@ -588,7 +589,8 @@
 !Cons methodsFor:'queries'!
 
 beginAndSizeOfCycle
-    "true if the list contains a cycle.
+    "return the begin and size of a cycle, if the list contains one.
+     Nil otherwise.
      Floyd's cycle-finding algorithm"
 
     |t h i loopStartIndex loopSize|
@@ -643,10 +645,6 @@
     "Created: / 27-07-2012 / 00:00:36 / cg"
 !
 
-isCons
-    ^ true
-!
-
 isCyclic
     "true if the list contains a cycle"
 
@@ -672,37 +670,27 @@
     "Created: / 26-07-2012 / 23:32:52 / cg"
 !
 
-length
+size
     "the list's length"
 
     |len p rest|
 
     len := 1.
     p := self.
-    [(rest := p cdr) notNil] whileTrue:[
-        rest isCons ifFalse:[^ len].
 
+    [
+        p isLazyValue not
+        and:[ (rest := p cdr) notNil ]
+    ] whileTrue:[
         len := len + 1.
         p := rest
     ].
     ^ len
 
     "
-     (Cons fromArray:#(1)) length     
-     (Cons fromArray:#(1 2 3 4)) length    
-     (Cons car:1 cdr:2) length    
-    "
-!
-
-size
-    "for smalltalkers: the list's length"
-
-    ^ self length
-
-    "
-     (Cons fromArray:#( )) size    
-     (Cons fromArray:#(1)) size     
+     (Cons fromArray:#(1)) size       
      (Cons fromArray:#(1 2 3 4)) size    
+     (Cons car:1 cdr:2) size          --> error (degenerated list)
     "
 ! !
 
@@ -712,13 +700,23 @@
     ^ ConsStream on:self.
 ! !
 
+!Cons methodsFor:'testing'!
+
+isCons
+    ^ true
+!
+
+isLazy
+    ^ false
+! !
+
 !Cons class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.24 2014-06-25 17:08:56 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.25 2014-06-25 17:19:00 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.24 2014-06-25 17:08:56 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.25 2014-06-25 17:19:00 cg Exp $'
 ! !