Associative array for Rc proposal
properties:
* array consisting of KEY-VAL pairs
* KEY is an odd-numbered element (1, 3, ...), VAL is an next even-numbered element (2,4, ...)
* no hashing at all
* meant mostly for small arrays
* linear lookup, for N Key-Value pairs average time is O(N), and max time is O(2N)
* basic functionality does not need any syntax extensions
* but also two optional, helpful syntax extensions proposed
* stored in arrays as plain Key-Value pairs, so for example:
% myVar=(Key0 Val0 Key1 Val1) # is both a proper array, and also an associative array
* to add new key-value pairs, simply do
% myVar=(Key2 Val2 $myVar)
* to overwrite a value under key Key1, just set a new one on the front:
% myVar=(Key1 NewVal1 $myVar)
* access to whole array
% echo $myVar -> (Key1 NewVal1 Key2 Val2 Key0 Val0 Key1 Val1) # returns array in the usual way - note the old Key1-Val1 pair and the new Key1-NewVal1 pair. This peculiarity is to be fixed by extension step #2
-- up to this point it work without any extensions
Extension, step #1:
* access with a key -> requires small extension of interpreter, no new syntax
% echo $myVar(Key1) -> NewVal1 # if KEY matches Nth element, returns N+1 element. If N+1 does not exist, reurns (). Mathes only odd-numbered elements for KEY match
* access with several keys -> requires mild extension of interpreter, minute extension of syntax
% echo $myVar(Key1 Key0) -> NewVal1 Val0 # list of VALues, same order as keys, same rules as for single KEY lookup above
* to print all keys (may yield repeated occurrences of the same key!)
% fn keys { for (i) { echo $i; shift 2 } }
% keys $myVar
* to print all values (may yield also old values!)
% fn vals { shift; for (i) { echo $i; shift 2 } }
% vals $myVar
Limitations:
* no way to unset element under a given KEY -> fixed in step #2
* no way to compose an associative array from array of KEYs and array of VALs -> fixed in step #3
* the order of pairs elements in array is arbitrary, and subject to change upon internal operations performed by Rc. However, a pair will not be split, or a new one created.
Possible optimization:
* always move the looked-up pair to the front of the list -> should be an O(1) operation if we have located the pair already
-- up to this point, only small syntax extensions needed.
Extension, step #2, optional:
* assignment under key KEY -> requires new syntax: VARIABLE_NAME(KEY)=VAL, with VAL equal to () being a way of clearing value under a key
% myVar(Key4)='Foo bar'
% echo $myVar(Key4) -> 'Foo bar'
Limitations:
* no way to have nested arrays (no support for such in Rc anyway). So for example, the following is not valid:
% myVar(Key99)=(foo bar baz) # to be discussed, will probably just use `foo' alone
Extension, step #3, optional:
* zipping two arrays, Keys and Vals, to create new one associative array -> a new operator ^^ will be defined
% myKeys=(Key0 Key1 Key2)
% myVals=(Val0 Val1 Val2)
# the operator ^
% ($myKeys^$myVals) -> (Key0Val0 Key1Val1 Key2Val2) # not very useful
# new operator ^^ to be defined:
% ($myKeys^^$myVals) -> (Key0 Val0 Key1 Val1 Key2 Val2) # a new associative array
The operator would also be defined for plain string vs string operation, and array vs string operation (perhaps not very useful, but good for symmetry)
# the original operator:
% (decoration^$myKeys) -> (decorationKey0 decorationKey1 decorationKey2)
% (foo^bar) -> foobar
# the new operator
% (foo^^bar) -> (foo bar)
% ($myKeys^^DEFAULT_VALUE) -> (Key0 DEFAULT_VALUE Key1 DEFAULT_VALUE Key2 DEFAULT_VALUE)