SequenceableCollection.st
changeset 18712 8e39130d65c8
parent 18711 81ebc1f3389d
child 18713 f777ab47baa5
--- a/SequenceableCollection.st	Sun Aug 23 11:08:05 2015 +0200
+++ b/SequenceableCollection.st	Sun Aug 23 11:30:28 2015 +0200
@@ -405,7 +405,8 @@
 
 allButFirst
     "Return a copy of the receiver containing all but the first element.
-     Returns an empty collection if there are not at least two elements."
+     Returns an empty collection if there are not at least two elements.
+     Differs from #copyButFirst in its error behavior."
 
     ^ self copyFrom:2
 
@@ -414,12 +415,15 @@
      'ab' allButFirst
      'a' allButFirst
      '' allButFirst
+
+     '' copyButFirst
     "
 !
 
 allButFirst:n
     "Return a copy of the receiver containing all but the first n elements.
-     Returns a short (or empty) collection if there are not enough elements."
+     Returns a short (or empty) collection if there are not enough elements.
+     Differs from #copyButFirst: in its error behavior."
 
     ^ self copyFrom:n+1
 
@@ -428,12 +432,15 @@
      '123456' allButFirst:5       
      '12345' allButFirst:5       
      '1234' allButFirst:5       
+
+     '1234' copyButFirst:5       
     "
 !
 
 allButLast
     "Return a copy of the receiver containing all but the last element.
-     Returns an empty collection if there are not at least two elements."
+     Returns an empty collection if there are not at least two elements.
+     Differs from #copyButFirst: in its error behavior."
 
     ^ self allButLast:1
 
@@ -442,6 +449,8 @@
      'ab' allButLast
      '0' allButLast
      '' allButLast
+
+     '' copyButLast
     "
 
     "Modified: / 13.11.2001 / 13:36:36 / cg"
@@ -449,7 +458,8 @@
 
 allButLast:n
     "Return a copy of the receiver containing all but the last n elements.
-     Returns a short (or empty) collection if there are not enough elements."
+     Returns a short (or empty) collection if there are not enough elements.
+     Differs from #copyButFirst: in its error behavior."
 
     ^ self copyFrom: 1 to: self size - n
 
@@ -458,6 +468,8 @@
      '12345' allButLast:5
      '123' allButLast:5
      '' allButLast:5
+
+     '' copyButLast:5
     "
 
     "Modified: / 13.11.2001 / 13:36:28 / cg"
@@ -3615,67 +3627,103 @@
 
 copyButFirst
     "return a new collection consisting of the receiver's elements
-     except for the first element - for convenience."
-
+     except for the first element.
+     Raises an error if the receiver is empty.
+     Differs from #allButFirst in its error behavior."
+
+    self isEmpty ifTrue:[self notEnoughElementsError].
     ^ self copyFrom:2
 
     "
      #($a $b $c $d $e $f $g) copyButFirst
      '1234567890' copyButFirst
+     '1' copyButFirst
+     '' copyButFirst
+
+     '' allButFirst
     "
 !
 
 copyButFirst:count
     "return a new collection consisting of the receiver's elements
-     except for the first count elements - for convenience."
-
+     except for the first count elements
+     Raises an error if the receiver is empty.
+     Differs from #allButFirst: in its error behavior."
+
+    self size < count ifTrue:[self notEnoughElementsError].
     ^ self copyFrom:(count + 1)
 
     "
      #($a $b $c $d $e $f $g) copyButFirst:2
      '1234567890' copyButFirst:2
+     '12' copyButFirst:2
+     '' copyButFirst:2
+
+     '' allButFirst:2
     "
 !
 
 copyButLast
     "return a new collection consisting of the receiver's elements
-     except for the last element - for convenience."
-
+     except for the last element.
+     Raises an error if the receiver is empty.
+     Differs from #allButLast in its error behavior."
+
+    self isEmpty ifTrue:[self notEnoughElementsError].
     ^ self copyTo:(self size - 1)
 
     "
      #($a $b $c $d $e $f $g) copyButLast
      '1234567890' copyButLast
+     '1' copyButLast
+     '' copyButLast
+
+     '' allButLast
     "
 !
 
 copyButLast:count
     "return a new collection consisting of the receiver's elements
-     except for the last count elements - for convenience."
-
+     except for the last count elements.
+     Raises an error if there are not at least count elements.
+     Differs from #allButLast: in its error behavior."
+
+    count > self size ifTrue:[self notEnoughElementsError].
     ^ self copyTo:(self size - count)
 
     "
      #($a $b $c $d $e $f $g) copyButLast:2
      '1234567890' copyButLast:2
+     '12' copyButLast:2
+     '1' copyButLast:2
+
+     '1' allButLast:2
     "
 !
 
 copyFirst:count
-    "return a new collection consisting of the receiver's first count
-     elements - this is just a rename of copyTo: - for compatibility."
-
+    "return a new collection consisting of the receiver's first count elements.
+     Raises an error if there are not enough elements.
+     Differs from #first: in its error behavior."
+
+    count > self size ifTrue:[self notEnoughElementsError].
     ^ self copyFrom:1 to:count
 
     "
      #($a $b $c $d $e $f $g) copyFirst:5
      '1234567890' copyFirst:4
+     '1234' copyFirst:4
+     '12' copyFirst:4
+
+     '1234567890' first:4
+     '123' first:4
+     '' first:4
     "
 !
 
 copyFrom:startIndex
     "return a new collection consisting of receiver's elements from startIndex to the end of the collection.
-     Return an empty collection, if startIndex is beyond the receivers size."
+     Return an empty collection, if startIndex is beyond the receiver's size."
 
     ^ self copyFrom:startIndex to:(self size)
 
@@ -3732,7 +3780,9 @@
 !
 
 copyLast:count
-    "return a new collection consisting of the receiver's last count elements."
+    "return a new collection consisting of the receiver's last count elements.
+     Raises an error if there are not enough elements.
+     Differs from #last: in its error behavior."
 
     |sz|
 
@@ -3742,6 +3792,13 @@
     "
      #($a $b $c $d $e $f $g) copyLast:5
      '1234567890' copyLast:4
+     '12345' copyLast:4
+     '1234' copyLast:4
+     '123' copyLast:4
+     '' copyLast:4
+
+     '123' last:4
+     '' last:4
     "
 !