A matrix is nice for modelling arrays, eg
const A = [1, 2, 3, 4]
const a1 = A[0]
const a2 = A[1]
const a3 = A[2]
const a4 = A[3]
can be modelled with $$ A = \begin{bmatrix} 1\\ 2\\ 3\\ 4 \end{bmatrix} $$
$$ a1 = A_1\\ a2 = A_2\\ a3 = A_3\\ a4 = A_4 $$
I am curious if there is some similar construct to this for modelling hash maps.
const A = {
"one": 1,
"two": 2,
"three": 3,
"four": 4
}
const A_one = A["one"]
const A_two = A["two"]
const A_three = A["three"]
const A_four = A["four"]
in a such a way that
$$ a1 = A_{"one"} \\ a2 = A_{"two"} \\ a3 = A_{"three"}\\ a4 = A_{"four"} $$
I was considering augmenting a matrix, but it's not standard as far as I know.
$$\left[ \begin{array}{c|c} "one"&1\\ "two"&2\\ "three"&3\\ "four"&4 \end{array} \right]$$
I know that hash tables are not matrices; as matrices are ordered, hash tables are unordered, and matrices have a bunch of properties/operations that do not map directly 1:1 to hash tables.
I know that one way a hash tables can be constructed is given a bunch of key, value pairs a input
eg.
$$ \left[ \begin{array}{c@{}c@{}} \left[ \begin{array}{cc} "one"\\ 1 \end{array} \right] \\ \left[ \begin{array}{cc} "two"\\ 2 \end{array} \right] \\ \left[ \begin{array}{cc} "three"\\ 3 \end{array} \right] \\ \left[ \begin{array}{cc} "four"\\ 4 \end{array} \right] \end{array} \right] $$
But an associator function to make this a proper map is still needed.
Surely there must be some existing way of representing hash tables/"spare" arrays without inventing my own notation for them, as maps are more natural in mathematics than matrices, as a matrix can be represented as an ordered map, but a maps can't be represented by matrices because the matrix construct cannot "skip" rows, and cannot have noninteger indexing.
Note:
It seems that unordered map can be represented by discontinuous functions, and ordered maps can be represented as a product of a function and an ordered key vector; eg
const f = x =>
x == "one" ? 1 :
x == "two" ? 2 :
x == "three" ? 3 :
x == "four" ? 4
: undefined
;
const keys = ["one", "two", "three", "four"];
const m = keys.map(x => [x, f(x)]);
// [ [ 'one', 1 ], [ 'two', 2 ], [ 'three', 3 ], [ 'four', 4 ] ]
console.log(m);
The above is effectively the same as
const f = {
"one": 1,
"two": 2,
"three": 3,
"four": 4
};
const m = Object.keys(f).map(x => [x, f[x]]);
// [ [ 'one', 1 ], [ 'two', 2 ], [ 'three', 3 ], [ 'four', 4 ] ]
console.log(m);
and they can be composed naturally to add entries
'use strict';
const UnorderedMap = (f, domain) => {
let res;
res = x => res[x];
domain.forEach(x => res[x] = f(x));
res['domain'] = domain;
res.composeWith = (f2, domain2) => {
var domainSet = new Set();
domain.forEach(x => domainSet.add(x));
domain2.forEach(x => domainSet.add(x));
return UnorderedMap((x => {
return res(x) !== undefined ? res(x) : f2(x);
}), domainSet);
};
return res;
};
let myMap = UnorderedMap((x => x === "zero" ? 0 : undefined), new Set(["zero"]));
myMap = myMap.composeWith((x => x === "one" ? 1 : undefined), new Set(["one"]));
myMap = myMap.composeWith((x => x === "two" ? 2 : undefined), new Set(["two"]));
myMap = myMap.composeWith((x => x === "three" ? 3 : undefined), new Set(["three"]));
console.log(myMap("zero")); // 0
console.log(myMap["zero"]); // 0
console.log(myMap("one")); // 1
console.log(myMap["one"]); // 1
console.log(myMap("two")); // 2
console.log(myMap["two"]); // 2
console.log(myMap("three")); // 3
console.log(myMap["three"]); // 3
console.log(myMap["domain"]); // Set { 'zero', 'one', 'two', 'three' }
let A = {("one",1),…}I have done existential quantification for A to the set, but a set has no indexer, so I can't do A_one, I must create a function f that performs the lookup, and it can no longer be named A_one since A is already quantified as a set construct, and set does not support indexing, another construct is needed to behave both like such a set AND have a lookup function. – Dmytro Dec 30 '16 at 20:08let S = ("one", "two", "three", "four"), let A_{"one"} = 1, let A_{"two"} = 2, let A_{"three"} = 3, let A_{"four"} = 4, let a1 = A_{"one"}, ...it would essentially be a discontinuous function. And if I want to make it ordered, I would need to explicitly create a function that maps some function over A based on S, or create my own construct that implicitly creates such a function – Dmytro Dec 30 '16 at 21:01