class: SortedSet
authorStefan Vogel <sv@exept.de>
Wed, 12 Nov 2014 12:31:58 +0100
changeset 3438 01ea6db95b58
parent 3437 1e3c96378a00
child 3439 51b6270c6369
class: SortedSet added: #collect: #select: #sortBlock Keep sortblock when creating a new collection
SortedSet.st
--- a/SortedSet.st	Sat Nov 08 11:20:41 2014 +0100
+++ b/SortedSet.st	Wed Nov 12 12:31:58 2014 +0100
@@ -126,6 +126,12 @@
     "Created: / 06-08-2012 / 12:34:29 / cg"
 ! !
 
+!SortedSet methodsFor:'accessing'!
+
+sortBlock
+    ^ order sortBlock
+! !
+
 !SortedSet methodsFor:'adding & removing'!
 
 addFirst:anObject 
@@ -144,6 +150,49 @@
     "Modified: / 06-08-2012 / 12:37:30 / cg"
 ! !
 
+!SortedSet methodsFor:'enumerating'!
+
+collect:aBlock
+    "for each element in the receiver, evaluate the argument, aBlock
+     and return a new collection with the results"
+
+    |newCollection|
+
+    newCollection := self speciesForAdding new.
+    newCollection setSortBlock:self sortBlock.
+    self do:[:element | newCollection add:(aBlock value:element)].
+    ^ newCollection
+
+    "
+     #(1 2 3 4) collect:[:e | e odd]   
+     (1 to:10) collect:[:e | e even]     
+    "
+
+    "Modified: / 07-08-2010 / 16:26:40 / cg"
+!
+
+select:aBlock
+    "return a new collection with all elements from the receiver, for which
+     the argument aBlock evaluates to true.
+     See also: #removeAllFoundIn: and #removeAllSuchThat:"
+
+    |newCollection|
+
+    newCollection := self speciesForAdding new.
+    newCollection setSortBlock:self sortBlock.
+    self do:[:each |
+        (aBlock value:each) ifTrue:[newCollection add:each].
+    ].
+    ^ newCollection
+
+    "
+     #(1 2 3 4) select:[:e | e odd]   
+     (1 to:10) select:[:e | e even]     
+    "
+
+    "Modified: / 07-08-2010 / 16:26:40 / cg"
+! !
+
 !SortedSet methodsFor:'initialization'!
 
 initializeOrder
@@ -161,9 +210,10 @@
 !SortedSet class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/SortedSet.st,v 1.2 2012-08-06 10:45:03 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/SortedSet.st,v 1.3 2014-11-12 11:31:58 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/SortedSet.st,v 1.2 2012-08-06 10:45:03 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/SortedSet.st,v 1.3 2014-11-12 11:31:58 stefan Exp $'
 ! !
+