Next: , Previous: , Up: Top  


8 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 or tables.

Dictionaries in k9 are often used. As an example in the benchmark chapter the market quote and trade data are dictionaries of symbols (name keys) and market data (table values).

8.1 Dictionary Creation ⇒ x!y

 d0:`pi`e`c!3.14 2.72 3e8;d0            / elements
pi|3.14 
e |2.72 
c |3e+08

 d1:`time`temp!(12:00 12:01 12:10;25.0 25.1 25.6);d1 / lists
time|12:00 12:01 12:10
temp|25 25.1 25.6

 d2:0 10 1!37.4 46.3 0.1;d2             / keys as numbers
 0|37.4
10|46.3
 1|0.1

 d3:`a`b`a!1 2 3;d3                     / non-unique keys
a|1
b|2
a|3

 d3`a                                   / `a value returned
1

8.2 Dictionary Indexing ⇒ x@y

Dictionaries, like lists, can be 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      
1 2
 x@`a`c    
1 2
5 6
 / all these notaions for indexing work, output surpressed
 x@`b;  / at
 x(`b); / parenthesis
 x `b;  / space
 x[`b]; / square bracket

8.3 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

8.4 Dictionary Value ⇒ x[]

The values from a dictionary are retreived by 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 temp from d1, one would convert d1 into values (as value .), take the second index (take the value 1), take the second element (take the temp 1), and then query the first value (element 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.

8.5 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 

8.6 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 

8.7 Flipping a Dictionary into a Table ⇒ +x

This command flips a dictionary into a table but will be covered in detail in the table section. Flipping a dictionary whose values are a single element has no effect.

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

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

 do~+d0
1b

 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

8.8 Functions that operate on each value in a dictionary

There a number of simple functions on dictionaries that operate on the values. If ’f’ is a function then f applied to a dictionary return a dictionary with the same keys and the values are 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

8.9 Functions that operate over values in a dictionary

There are functions on dictions that operate over the values. Given function f applied to a dictionary d then f d returns a value.

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

 *d0
3.14

Next: , Previous: , Up: Top