3.10 string-dict
3.10.1 The StringDict Type
A StringDict can be unfrozen, i.e., used as the basis for MutableStringDict, which does allow modification (see below).
There are no variants for StringDicts, and programs cannot use cases statements with StringDicts. Instead, they can be created with the constructors below, and manipulated with the methods and functions below.
3.10.2 StringDict Constructor
Creates a string-dict with the given elts.
sd1 = [string-dict: "a", 5, "b", 10]
3.10.3 StringDict Methods
Returns none if the key is not in the dictionary, and a some containing the value the key maps to if the key is in the dictionary.
check: [string-dict: "a", 5].get("a") is some(5) [string-dict: "a", 5].get("b") is none end
Returns the value that key maps to if it is present, and throws an exception otherwise.
check: [string-dict: "a", 5].get-value("a") is 5 [string-dict: "a", 5].get-value("b") raises "Key b not found" end
Returns a new string-dict that maps key to value and is otherwise similar to the original string-dict.
check: sd1 = [string-dict: "a", 5, "b", 10] sd1.get-value("a") is 5 sd1.get-value("b") is 10 sd2 = sd1.set("a", 15) sd2.get-value("a") is 15 sd2.get-value("b") is 10 end
Returns true if key is in the string-dict; false if not.
check: sd1 = [string-dict: "a", 5] sd1.has-key("a") is true sd1.has-key("b") is false end
Returns the set of keys in the string-dict.
check: sd1 = [string-dict: "a", 5, "b", 10] sd1.keys() is [tree-set: "a", "b"] sd1.keys() is [tree-set: "b", "a"] end
Returns a new string-dict that doesn’t have the argument key but is otherwise similar to the original string-dict.
check: sd1 = [string-dict: "a", 5, "b", 10] sd1.has-key("a") is true sd1.has-key("b") is true sd2 = sd1.remove("b") sd2.has-key("a") is true sd2.has-key("b") is false end
Returns the number of keys in the string-dict.
check: sd1 = [string-dict: "a", 5, "b", 10] sd1.count() is 2 sd2 = sd1.set("c", 15) sd2.count() is 3 sd3 = sd1.remove("a") sd3.count() is 1 end
Returns a mutable string-dict that has the same keys and values as the original string-dict.
check: sd1 = [string-dict: "a", 5, "b", 10] msd1 = sd1.unfreeze() msd1.set-now("a", 0) msd1.get-value-now("a") is 0 end
3.10.4 The MutableStringDict Type
A MutableStringDict can be sealed to produce a variant that is read-only. A MutableStringDict can also be frozen to produce an immutable StringDict (see above).
There are no variants for MutableStringDicts, and programs cannot use cases statements with MutableStringDicts. Instead, they can be created with the constructors below, and manipulated with the methods and functions below.
3.10.5 MutableStringDict Constructor
Creates an mutable string-dict with the given elts.
msd1 = [mutable-string-dict: "a", 5, "b", 10]
3.10.6 MutableStringDict Methods
Returns none if the key is not in the dictionary, and a some containing the value the key maps to if the key is in the dictionary.
check: [mutable-string-dict: "a", 5].get-now("a") is some(5) [mutable-string-dict: "a", 5].get-now("b") is none end
Returns the value that key maps to if it is present, and throws an exception otherwise.
check: [mutable-string-dict: "a", 5].get-value-now("a") is 5 [mutable-string-dict: "a", 5].get-value-now("b") raises "Key b not found" end
Modifies the mutable-string-dict so that it now maps key to value. This method is called only for its side-effect and so returns nothing
check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.get-value-now("a") is 5 msd1.get-value-now("b") is 10 msd1.set-now("a", 15) is nothing msd1.get-value-now("a") is 15 msd1.get-value-now("b") is 10 end
Returns true if key is in the string-dict; false if not.
check: msd1 = [mutable-string-dict: "a", 5] msd1.has-key-now("a") is true msd1.has-key-now("b") is false end
Returns the set of keys in the string-dict.
check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.keys-now() is [tree-set: "a", "b"] msd1.keys-now() is [tree-set: "b", "a"] end
Modifies the mutable-string-dict so that it no longer has the argument key.
check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.has-key-now("a") is true msd1.has-key-now("b") is true msd1.remove-now("b") is nothing msd1.has-key-now("a") is true msd1.has-key-now("b") is false end
Returns the number of keys in the mutable-string-dict.
check: msd1 = [mutable-string-dict: "a", 5, "b", 10] msd1.count-now() is 2 msd1.set-now("c", 15) msd1.count-now() is 3 msd1.remove-now("a") msd1.count-now() is 2 end
Returns an immutable string-dict that has the same keys and values as the mutable one.
check: msd1 = [mutable-string-dict: "a", 5, "b", 10] sd1 = msd1.freeze() sd2 = sd1.set("a", 10) sd2.get-value("a") is 10 sd1.get-value("a") is 5 end
Returns a sealed version of the mutable-string-dict that has the same keys and values, but does not allow modification. The original mutable-string-dict continues to be modifiable, and such modifications will be visible in the sealed one.
check: msd1 = [mutable-string-dict: "a", 5, "b", 10] smsd1 = msd1.seal() smsd1.get-value-now("a") is 5 smsd1.set-now("a", 15) raises "Cannot modify sealed string dict" msd1.set-now("a", 15) is nothing msd1.get-value-now("a") is 15 smsd1.get-value-now("a") is 15 end