Dictionaries - kevinlawler/kona GitHub Wiki
Dictionaries are K’s version of keyed lists or hashtables. The keys are restricted to being symbol atoms, but the values may be anything, even other dictionaries.
d[`k]:1 2 3
d[`a]:"abc"
d[`k]
1 2 3
d[`a]
"abc"
Most of the standard methods for updating and indexing into data have working analogs for dictionaries.
d[`a]: 4
d[`a]: 5
d[`b]: 6
d[`c]: 7
d[`c`b`a`c`b`a]
7 6 5 7 6 5
d@3#`a
5 5 5
You can extract the values or the keys from a dictionary in the following manner.
d[`i]:23
d: @[d;`j `k;:;45 67]
!d
`i `j `k
d[]
23 45 67
Dictionaries can be created implicitly, as above, or they can be explicitly constructed using the monadic “.” operator, Make/Unmake Dictionary. Make Dictionary takes as its argument a list of lists. The container list may be empty, or it may have many entries. The sublists must be of a certain type. Specifically, they must begin with a symbol and be either two or three elements in length. The third element is for attributes. If the third element exists, it can be null, or it can be a dictionary. Attribute dictionaries store special data about entries. Attribute functionality is not yet well implemented. When monadic “.” is applied to a dictionary it is “Unmake Dictionary.” Unmake Dictionary turns dictionaries back into lists of the special type.
d: .((`a;10);(`b;20)) //Make dictionary and store in d
d
.((`a;10;)
(`b;20;))
d[`a]
10
d[`b]
20
Some of the syntax surrounding dictionaries may at first be hard to decipher. We show what an empty dictionary looks like, and then we show what a dictionary with one element looks like.
.() /create an empty dictionary
.()
.,(`a;10) /create a dictionary with one element
.,(`a;10)
The comma indicates that what follows is a one element list. So the parenthetical expression is the sublist of a container list that isn’t truly visible. The comma, indicating the enlist verb, stands in for it.
The K-Tree is a dictionary. The K-Tree stores all the variables that are accessible to K. Try setting some variables and then viewing the K-Tree.
a:3
b:7
.` /Show the K-Tree. The dot is 'evaluate.' The empty symbol ` is the name of the root.
.((`k
.((`a;3;)
(`b;7;))
))
This expression shows two nested dictionaries. The first is the root dictionary “.”. The second is the dictionary “.k”, which is the directory execution starts in. Let’s build our own dictionary first to get a better sense of how nesting works.
e[`f]:10
d[`e]:e
d[`e][`f] /multi-dimensional indexing works
10
d /show d
.,(`e
.,(`f;10;)
)
d[`e] /show the value of d at `e
.,(`f;10;)
This method of iterated indexing is related to how dot-delimited symbols point to variables on the K-Tree. For example:
d: _n
d[`s]:22
d
.,(`s;22;)
d.s
22
.`d /evaluate the symbol `d
.,(`s;22;)
.`d.s /evaluate the symbol `d.s
22
.`.k.d.s /absolute path
22
_d /check our path
`.k
Absolute references begin with a dot, relative references don’t. This ties into the empty symbol being the root node. You can think of absolute paths as beginning with the empty symbol, with the rest of the directory names appended in a dot delimited fashion.
Function-local variables are also stored internally using dictionaries.
A dictionary is the type 5 datatype.
To remove an entry “e” from a dictionary “d”, you can use the function in the following example.
dic:.((`a;1);(`b;2 3);(`c;"abcde"))
dic
.((`a;1;)
(`b
2 3
)
(`c;"abcde";))
{[d;e] .+(k;d[k:(!d) _dv e])}[dic;`b]
.((`a;1;)
(`c;"abcde";))
Todo: attributes