I'm not so sure. I keep running into situations where I want to have an ordered data structure, but where I also want an associative lookup and/or update.
For example, my shell PATH. It is definitely order dependent. But I often want to do operations such as
- is DIR already in PATH
- add DIR to PATH if it is not already present
- prepend or append
- add DIR to PATH next to DIR_already_in_path
- just before, or just after
- replace DIR0 in PATH (assuming it is already there) with DIR1
For example, hook lists - like hook lists for emacs.
For example, like Bash PROMPT_COMMAND. This really isn't a hook list, but imagine that it is, using ; as the separator (gets more complicated with IFs, etc.). Here I have been wanting a more general associative lookup, not just exact matching.
- E.g. replace any element that looks like a command that invokes my logical-path program.*logical-path.* with a different invocation of that program
Just like associative arrays, hashes, have greaty impoved programmer productivity by being built in data structures in Perl and Python (and to a weaker extent in C++ STL std::map), it may be worth thinking about standardizing ordered associative arrays. Using Perl associative array syntax (not my favorite, but it's easy at hand):
$OAA{key} # error if more than 1 match?
$OAA{key:1} $OAA{key:2} ... # first match, second match, ...
$OAA{key:#} # number of matches
$OAA{key} = value # replace. error if no match
$OAA{key} = {} # delete - or some other way of indicating droppage
$OAA{key} <<= value # insert just before
$OAA{key} >>= value # insert just after
$OAA{key:*} = ... # replace all occurrences
We might distinguish key from value, as in key=value. Or we might allow the key and the value to be the same, i.e. the entire value might be matched against.
We might require key to be exactly matched - or we might allow regexps, or arbitrary expressions.
$OAA{i WHERE $OAA{i} < 2} = ... # replace all occurrencesWe might allow the value to have structure, with fields.
$OAA{rec WHERE $OAA{rec}.age > 10 and $OAA{rec}.weight <70 br="br">$OAA{.age > 10 and .weight <70 blockquote="blockquote">Nothing that you can't do with map here - but the notation may be more pleasant. And compact, pleasant notation matters. (As opposed to compact unpleasant notation.)
Verges on relations.
---
Extend to multi dimensional?
70>70>
No comments:
Post a Comment