Collection.st
changeset 19697 d937d19b4871
parent 19680 3b41b44769e7
child 19707 8e312f358d84
child 19708 55a0426c569f
--- a/Collection.st	Wed May 04 07:49:44 2016 +0200
+++ b/Collection.st	Wed May 04 07:49:57 2016 +0200
@@ -762,7 +762,8 @@
 
 last
     "return the last element of the collection.
-     This should be redefined in subclasses."
+     This is a slow fallback implementation,    
+     and should be redefined in subclasses which can do indexed accesses."
 
     |theLastOne any|
 
@@ -875,6 +876,24 @@
     "
 !
 
+secondLast
+    "return the second last element of the collection.
+     This is a slow fallback implementation,    
+     and should be redefined in subclasses which can do indexed accesses."
+
+    |theSecondLastOne theLastOne cnt|
+
+    cnt := 0.
+    
+    self do:[:e | cnt := cnt+1. theSecondLastOne := theLastOne. theLastOne := e].
+    cnt > 1 ifTrue:[
+        ^ theSecondLastOne
+    ].
+
+    "error if collection did not enumerate at least 2 elements"
+    ^ self notEnoughElementsError
+!
+
 seventh
     "return the seventh element of the collection.
      For unordered collections, this simply returns the sixth
@@ -5883,6 +5902,7 @@
     ^ aVisitor visitCollection:self with:aParameter
 ! !
 
+
 !Collection class methodsFor:'documentation'!
 
 version