Collection.st
branchjv
changeset 19707 8e312f358d84
parent 19691 5e613f6255d9
parent 19697 d937d19b4871
child 19811 65fec19facb0
--- a/Collection.st	Wed May 04 06:50:56 2016 +0200
+++ b/Collection.st	Thu May 05 06:48:19 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