Thanks. This is nice. I'm thinking that the at:at: could be a nice addition to our Dictionary class.
On Sun, May 28, 2017 at 4:24 PM, Stan Silver via Pharo-users < pharo-users@lists.pharo.org> wrote: > > > ---------- Forwarded message ---------- > From: Stan Silver <stanley.sil...@yahoo.com> > To: pharo-users@lists.pharo.org > Cc: > Bcc: > Date: Sun, 28 May 2017 07:24:06 -0700 (PDT) > Subject: Re: Dictionary whose values are dictionaries > Stephane Ducasse-3 wrote > > Hi > > > > I'm looking for an implementation of dictionary whose value is also a > > dictionary. > > > > Stef > > 1. These go only one level down, but I have defined these methods on class > Dictionary. > > at: firstKey at: secondKey > ^ self at: firstKey at: secondKey ifAbsent: [ self > errorKeyNotFound ] > > at: firstKey at: secondKey ifAbsent: aZeroArgBlock > | subDictionary | > subDictionary := self at: firstKey ifAbsent: [ ^ aZeroArgBlock > value ]. > ^ subDictionary at: secondKey ifAbsent: [ aZeroArgBlock value ] > > at: firstKey at: secondKey ifAbsentPut: aZeroArgBlock > | subDictionary | > subDictionary := self at: firstKey ifAbsentPut: [ Dictionary new ]. > ^ subDictionary at: secondKey ifAbsentPut: [ aZeroArgBlock value ] > > at: firstKey at: secondKey put: aValue > | subDictionary | > subDictionary := self at: firstKey ifAbsentPut: [ Dictionary new ]. > ^ subDictionary at: secondKey put: aValue > > 2. I use this class for testing. > > MockInstance>> > doesNotUnderstand: aMessage > " > Getter: If the value is already there, it is either a branch or a > leaf, so > return it. > Getter: If the value is missing, it is a branch, so add a new > instance of > self and return it. > Setter: It is a leaf, so add the value. > > | r | > r := self new. > r one two three: 3. > r one two three > > (The above should return 3) > " > > aMessage selector isUnary > ifTrue: [ ^ self at: aMessage selector ifAbsentPut: [ self > class new ] ]. > aMessage selector isKeyword > ifTrue: [ > aMessage numArgs = 1 > ifTrue: [ ^ self at: aMessage selector > allButLast put: aMessage > arguments first ] ]. > ^ super doesNotUnderstand: aMessage > > at: aSymbol ifAbsentPut: aValue > ^ self values at: aSymbol ifAbsentPut: aValue > > at: aSymbol put: aValue > self values at: aSymbol put: aValue > > values > ^ values ifNil: [ values := Dictionary new ] > > > > > > -- > View this message in context: http://forum.world.st/ > Dictionary-whose-values-are-dictionaries-tp4948453p4948484.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > > >