I have an object like a Dictionary('CMFireAutomataModel'->a Dictionary('nbAshes'->193 'nbFires'->851 ) )
and I would like to have something like Dictionary('nbAshes'->193 'nbFires'->851 )
.
I don't know how to "unstack" the first dictionary.
Let's say you have a Dictionary
whose keys are Strings
and its values are Numbers
or Dictionaries
of the same sort (i.e., with keys that are strings and values that are dicts or numbers). What we want is a way to "promote" or "unstack" all string keys and numbers to the mother dictionary.
unstack: aDictionary
| dict |
dict := aDictionary class new.
aDictionary keysAndValuesDo: [:k :v | | d |
v isNumber
ifTrue: [dict at: k put: v]
ifFalse: [
d := self unstack: v.
dict addAll: d associations]].
^dict
Note that I've used aDictionary class new
to make sure the method answers with a Dictionary
of the same kind (e.g., an IdentityDictionary
, etc.).
Note also that the method could go in any class. I haven't put it in Dictionary
because I don't think this is general enough (even though that would have simplified the code a little bit)