Dictionary
table, size
A mutable collection of key-value pairs with hashed lookup. Keys can be
any object that responds to hash and =; symbols and strings are the
most common key types.
Create dictionaries with the literal syntax #{key -> value} or
incrementally with Dictionary new and at:put:. Pairs in the literal
are separated by periods; keys and values can be any expression.
Core operations (at:, at:put:, includesKey:, size, keys,
values, do:, keysAndValuesDo:, removeKey:) are implemented as
VM primitives. This file adds convenience methods on top.
Dictionary new size >>> 0
Dictionary new isEmpty >>> true
#{#x -> 1. #y -> 2} size >>> 2
(#{#name -> 'Alice'} at: #name) >>> 'Alice'
"Dictionary literal syntax"
| d <Dictionary> |
d := #{#name -> 'Alice'. #age -> 30. #active -> true}.
d at: #name
"Build a dictionary with cascaded puts"
| d <Dictionary> |
d := Dictionary new.
d at: #name put: 'Alice'.
d at: #age put: 30.
d size
Class Methods
primitives
Create a new empty dictionary.
Instance Methods
primitives
Return the value associated with key, or nil if not present.
Return the value at key, or evaluate block if absent.
If key exists, evaluate block with its value; otherwise return nil.
Associate value with key. Returns value.
Evaluate block once for each value.
Return true if the dictionary contains key.
Return true if the dictionary has no entries.
Return an array of all keys.
Evaluate block with each key-value pair.
Remove the entry for key. Raises an error if absent.
Remove the entry for key; if absent, evaluate block.
Return the number of entries.
Return an array of all values.
uncategorized
Return the value at key. If the key is absent, evaluate block, store the result under key, and return it.
d := Dictionary new.
d at: #x ifAbsentPut: [42] >>> 42
d at: #x >>> 42
d at: #x ifAbsentPut: [99] >>> 42
Return a shallow copy of the dictionary. The new dictionary has the same keys and values but is a distinct object.
d := Dictionary new.
d at: #x put: 1.
c := d copy.
c at: #x >>> 1
c size >>> 1
Evaluate block once for each key in the dictionary.
d := Dictionary new.
d at: #a put: 1.
d keys size >>> 1
Return true if the dictionary contains at least one entry.
d := Dictionary new.
d notEmpty >>> false
d at: #x put: 1.
d notEmpty >>> true
Return a human-readable string representation of the dictionary.
Dictionary new printString >>> 'Dictionary()'
Return the receiver. Useful at the end of a cascade to return the dictionary itself rather than the last message result.
Dictionary new yourself class >>> #Dictionary