added #from:to:select:
authorClaus Gittinger <cg@exept.de>
Wed, 12 Sep 2001 13:10:35 +0200
changeset 6027 2ef25097672f
parent 6026 00c2d3f29de6
child 6028 0809effa40cd
added #from:to:select:
SequenceableCollection.st
--- a/SequenceableCollection.st	Wed Sep 12 11:42:52 2001 +0200
+++ b/SequenceableCollection.st	Wed Sep 12 13:10:35 2001 +0200
@@ -2342,6 +2342,39 @@
     "
 !
 
+from:startIndex to:stopIndex select:aBlock
+    "evaluate the argument, aBlock for the elements at startIndex..stopIndex
+     and return a collection of those elements for which the block return true."
+
+    |element newColl species needCopy
+     n  "{ Class:SmallInteger }"|
+
+    n := stopIndex - startIndex + 1.
+    species := self species.
+    species growIsCheap ifFalse:[
+        newColl := OrderedCollection new:n.
+        needCopy := true
+    ] ifTrue:[
+        newColl := self copyEmpty:n.
+        needCopy := false
+    ].
+    startIndex to:stopIndex do:[:index |
+        element := self at:index.
+        (aBlock value:element) ifTrue:[
+            newColl add:element
+        ].
+    ].
+    needCopy ifTrue:[
+        newColl := (species withAll:newColl) postCopyFrom:self
+    ].
+    ^ newColl
+
+    "
+     #(faba one two three four five six) 
+        from:2 to:5 select:[:element | element startsWith:'f']      
+    "
+!
+
 keysAndValuesDo:aTwoArgBlock
     "evaluate the argument, aBlock for every element in the collection,
      passing both index and element as arguments."
@@ -5270,6 +5303,6 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.153 2001-09-04 12:45:09 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.154 2001-09-12 11:10:35 cg Exp $'
 ! !
 SequenceableCollection initialize!