Next: , Previous: , Up: Top  


6 Dictionary

Dictionaries are a data type of key-value pairs typically used to retrieve the value by using the key. In other computer languages they are also known as associative arrays and maps. Keys should be unique to avoid lookup value confusion but uniqueness is not enforced. The values in the dictionary can be single elements, lists, tables, or even other dictionaries.

Dictionaries in k9 are often used. As an example from finance, market quotes and trade data are dictionaries of symbols (name keys) and market data (table values).

6.1 Dictionary Creation ⇒ x!y or [x;y]

Dictionaries are created by using the key symbol or square bracket notation and listing the keys (x) and values (y).

x!y

 d0:`a37!12; d0                          / key is `a37 and value is 12
a37|12

 d1:`pi`e`c!3.14 2.72 3e8;d1             / three keys `pi, `e, `c
pi|3.14 
e |2.72 
c |3e+08

 `a`b`c!(1 2 3;10 20 30;100 200 300 499) / values are lists
a|1 2 3      
b|10 20 30   
c|100 200 300 499

[x:y]

 d0:[a37:12]
a37|12

 d1:[pi:3.14;e:2.72;c:3e8];d1
pi|3.14
e |2.72
c |3e+08

 [a:1 2 3;b:10 20 30;c:100 200 300]
a|1 2 3      
b|10 20 30   
c|100 200 300

Often one will need to modify the data while building the dictionary. User defined functions can easily accomplish this task.

 `x`sinX`cosX!{(x;sin y;cos z)} . 3#,0.1*(!62)
x   |0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 ..
sinX|0 0.09983342 0.1986693 0.2955202 0.3894183 0..
cosX|1 0.9950042 0.9800666 0.9553365 0.921061 0.8..

6.2 Dictionary Join ⇒ x,y

This joins dictionaries together and the right or y dictionary will overwrite the left or x dictionary if common keys are present.

 d1:`a`b`c!3 2 1;d1
a|3
b|2
c|1

 d2:`c`d`e!90 80 70;d2
c|90
d|80
e|70

 ^d1,d2 / ^ sort by key
a| 3
b| 2
c|90
d|80
e|70

 ^d2,d1 / ^ sort by key
a| 3
b| 2
c| 1
d|80
e|70

6.3 Dictionary Indexing ⇒ x@y

Dictionaries, like lists, can be key indexed in a number of ways.

 x:`a`b`c!(1 2;3 4;5 6);x
a|1 2
b|3 4
c|5 6

x@`a   / single index     
1 2

x@`a`c / multiple index   
1 2
5 6

/ all these notations for indexing work (output not shown)
 x@`b;  / at
 x(`b); / parenthesis
 x `b;  / space
 x[`b]; / square bracket

6.4 Dictionary Key ⇒ !x

The keys from a dictionary are retreived by using the ! function.

 !d0
`pi`e`c
 !d1
`time`temp
 !d2
0 10 1

6.5 Dictionary Value ⇒ x[]

The values from a dictionary are retrieved using bracket notation.

 d0[]
3.14 2.72 3e+08
 d1[]
12:00 12:01 12:10
25.   25.1  25.6 

 d2[]
37.4 46.3 0.1

One could return a specific value by indexing into a specific location. As an example, in order to query the first value of the values associated with the key temp from d1, one would convert d1 into values (a pair of lists), and index that by [1;0].

 d1
time|12:00 12:01 12:10
temp|25 25.1 25.6     

 d1[]
12:00 12:01 12:10
25    25.1  25.6 

 d1[][1]
25 25.1 25.6
 d1[][1;0]
25.

6.6 Sorting a Dictionary by Key ⇒ ^x

 d0
pi|3.14 
e |2.72 
c |3e+08

 ^d0
c |3e+08
e |2.72 
pi|3.14 

6.7 Sorting a Dictionary by Value ⇒ <x (>x)

 d0
pi|3.14 
e |2.72 
c |3e+08

 <d0
e |2.72 
pi|3.14 
c |3e+08

 >d0
c |3e+08
pi|3.14 
e |2.72 

6.8 Flipping a Dictionary into a Table ⇒ +x

This command flips a dictionary into a table and will be covered in detail in the table chapter.

 d1
time|12:00 12:01 12:10
temp|25 25.1 25.6     

 +d1
time  temp
----- ----
12:00 25  
12:01 25.1
12:10 25.6

 d1~+d1
0b

6.9 Functions that operate on each value in a dictionary

There are a number of simple functions on dictionaries that operate on their values. If ’f’ is a function then f applied to a dictionary returns a dictionary with the same keys and the values resulting from the application of ’f’.

Examples

 d2
 0|37.4
10|46.3
 1|0.1 

 -d2
 0|-37.4
10|-46.3
 1|-0.1 

 d2+3
 0|40.4
10|49.3
 1|3.1 

 d2-1.7
 0|35.7
10|44.6
 1|-1.6

 d2*10
 0|374
10|463
 1|1  

 d2%100
 0|0.374
10|0.463
 1|0.001

6.10 Functions that return over values from a dictionary

There are functions on dictionaries that operate over the values. Given function f applied to a dictionary d, f d returns one or more values without the original keys.

 d0
pi|3.14
e |2.72
c |3e+08

 *d0
3.14

Next: , Previous: , Up: Top