Thursday, July 12, 2012

Dictionaries and Vectors as Functions

IMAO this is one of the more interesting bits of theory embedded in q:

Considering a dictionary as a (partial) function from its key (domain) to its value (range), then two dictionaries f and g such that f's value and g's key are of the same type can be composed:

q)f:`a`b`c!1 2 3
q)g:1 2 3!("foo";"bar";"quux")
q)g f
a| "foo"
b| "bar"
c| "quux"
q)(g f)`b
"bar"
q)

Considering a vector v as a dictionary with a key of the vector of integers from 0 to count[v]-1, then v can be composed with a dictionary h of integer value:

q)v:42 137 23
q)h:`a`b`c!0 1 2
q)v h
a| 42
b| 137
c| 23
q)(v h)`b
137
q)

Labels: ,