--- a/KeyedCollection.st Wed Mar 15 20:45:50 2017 +0100
+++ b/KeyedCollection.st Thu Mar 16 13:36:12 2017 +0100
@@ -57,6 +57,67 @@
"
! !
+!KeyedCollection class methodsFor:'instance creation'!
+
+withAssociations:aCollectionOfAssociations
+ "return a new instance where associations are taken from the argument"
+
+ |newDict sz "{ Class: SmallInteger }"|
+
+ sz := aCollectionOfAssociations size.
+ newDict := self new:sz.
+ aCollectionOfAssociations do:[:assoc |
+ newDict at:assoc key put:assoc value
+ ].
+ ^ newDict
+
+ "
+ KeyValueList withAssociations:{ #'one'->1 .
+ #'two'->2 .
+ #'three'->3 .
+ #'four'->4 }
+ "
+
+ "Created: / 16-03-2017 / 12:15:38 / stefan"
+!
+
+withKeys:keyArray andValues:valueArray
+ "return a new instance where keys and values are taken from
+ the argumentArrays."
+
+ |newColl sz "{ Class: SmallInteger }"|
+
+ sz := keyArray size.
+ newColl := self new:sz.
+ 1 to:sz do:[:index |
+ newColl at:(keyArray at:index) put:(valueArray at:index).
+ ].
+ ^ newColl
+
+ "Created: / 16-03-2017 / 11:06:51 / stefan"
+ "Modified (format): / 16-03-2017 / 12:12:09 / stefan"
+!
+
+withKeysAndValues:anArray
+ "return a new instance where keys and values are taken from alternating
+ elements of anArray"
+
+ |newDict sz "{ Class: SmallInteger }"|
+
+ sz := anArray size.
+ newDict := self new:(sz // 2).
+ 1 to:sz by:2 do:[:i |
+ newDict at:(anArray at:i) put:(anArray at:i+1)
+ ].
+ ^ newDict
+
+ "
+ KeyValueList withKeysAndValues:#('one' 1 'two' 2 'three' 3 'four' 4)
+ "
+
+ "Created: / 16-03-2017 / 12:13:27 / stefan"
+! !
+
!KeyedCollection class methodsFor:'queries'!
isAbstract
@@ -69,6 +130,14 @@
!KeyedCollection methodsFor:'accessing'!
+associationAt:key
+ "return an association consisting of aKey and the element indexed
+ by aKey -
+ report an error, if no element is stored under aKey."
+
+ ^ Association key:key value:(self at:key)
+!
+
at:key
"return the value stored under akey.
Raise an error if not found"