#FEATURE by stefan
class: KeyedCollection
added:
#keysAndValuesSelect:
#keysSelect:
#select:
--- a/KeyedCollection.st Tue Sep 18 15:08:47 2018 +0200
+++ b/KeyedCollection.st Tue Sep 18 15:26:45 2018 +0200
@@ -634,12 +634,129 @@
"Created: / 19.6.1998 / 00:56:52 / cg"
!
+keysAndValuesSelect:aBlock
+ "return a new collection with all elements from the receiver, for which
+ the argument aBlock evaluates to true.
+ The block gets keys and values as separate arguments.
+
+ See also:
+ #associationsSelect: (which passes key-value pairs),
+ #keysSelect: (which passes key values),
+ #select: (which only passes the value)
+
+ This is much like #associationsSelect:, but aBlock gets the
+ key and value as two separate arguments.
+ #associationsSelect: is a bit slower.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
+
+ |newCollection|
+
+ newCollection := self species new.
+ self keysAndValuesDo:[:key :value |
+ (aBlock value:key value:value) ifTrue:[
+ newCollection at:key put:value
+ ]
+ ].
+ ^ newCollection
+
+ "
+ |ages|
+
+ ages := SmallDictionary new.
+ ages at:'cg' put:37.
+ ages at:'ca' put:33.
+ ages at:'sv' put:36.
+ ages at:'tk' put:28.
+
+ ages keysAndValuesSelect:[:name :age |
+ (name startsWith:'c') or:[age < 30]].
+ "
+
+ "Created: / 18-09-2018 / 15:22:05 / Stefan Vogel"
+!
+
+keysSelect:aBlock
+ "return a new collection with all elements from the receiver, for which
+ the argument aBlock evaluates to true.
+ The block gets the individual keys as its single argument.
+
+ See also:
+ #associationsSelect: (which passes key->value associations),
+ #keysAndValuesSelect: (which passes key & value args)
+ #select: (which passes values as arg),
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
+
+ |newCollection|
+
+ newCollection := self species new.
+ self keysAndValuesDo:[:key :value |
+ (aBlock value:key) ifTrue:[
+ newCollection at:key put:value
+ ]
+ ].
+ ^ newCollection
+
+ "
+ |d|
+
+ d := SmallDictionary new.
+ d at:#foo put:#bar.
+ d at:#bar put:#baz.
+ d at:#baz put:#foo.
+
+ d keysSelect:[:el | el startsWith:'b'].
+ "
+
+ "Created: / 18-09-2018 / 15:23:26 / Stefan Vogel"
+!
+
printElementsDo:aBlock
"redefined, so #printOn: prints associations"
^ self associationsDo:aBlock
"Created: / 14-09-2018 / 17:47:48 / Stefan Vogel"
+!
+
+select:aBlock
+ "return a new collection with all elements from the receiver, for which
+ the argument aBlock evaluates to true.
+ The block gets the individual values as its single argument.
+
+ See also:
+ #associationsSelect: (which passes key->value associations),
+ #keysAndValuesSelect: (which passes key & value args)
+ #keysSelect: (which passes key values),
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
+
+ |newCollection|
+
+ newCollection := self species new.
+ self keysAndValuesDo:[:key :value |
+ (aBlock value:value) ifTrue:[
+ newCollection at:key put:value
+ ]
+ ].
+ ^ newCollection
+
+ "
+ |d|
+
+ d := SmallDictionary new.
+ d at:#foo put:#bar.
+ d at:#bar put:#baz.
+ d at:#baz put:#foo.
+
+ d select:[:el | el startsWith:'b'].
+ "
+
+ "Created: / 18-09-2018 / 15:25:15 / Stefan Vogel"
! !