In the past, I have implemented 2-column "pseudo-dictionaries" in J. It
works, but feels very awkward compared to working with arrays:
   ]d=: (;:'a b c'),.1 2 3;'foo bar';(<;:'x y z')
┌─┬───────┐
│a│1 2 3  │
├─┼───────┤
│b│foo bar│
├─┼───────┤
│c│┌─┬─┬─┐│
│ ││x│y│z││
│ │└─┴─┴─┘│
└─┴───────┘
   get=: {{((<,x) I.@E. {."1 y) { {:"1 y}}
   'b' get d
┌───────┐
│foo bar│
└───────┘

What I mean by awkward is not only the verbosity required to implement a
'get' verb, but also the extra awkwardness when extending this simple
dictionary to be more useful:
When I add a key to d which is an atom (rather than a char array), I need
to rewrite my get verb.
Or, if I restrict keys in dictionaries to only symbol values, then it
becomes more difficult to use real-world data as keys.

I think if J syntax supported this datatype natively, then the above 'get'
verb would just be
'b' { dict
Which is much better as a tool of thought.


To answer Henry's questions:

My definition of a dictionary is:
A collection where data is organized by key, where keys can be symbols,
strings, or numbers (or possibly other data types) and values can be
arbitrary J data.

Essential dictionary operations are:
- create/read/update/delete by key
- query if key exists
- enumerate all keys (returns an array of boxed keys)
- enumerate all values (returns an array of boxed values)
- merge two dictionaries e.g. (Z=: X merge Y) and if X and Y share any key,
then Z gets the key/value pair from Y

Nice features to have, but not necessary:
- keys are stored in J's sorting order
- dictionaries can be serialized/deserialized for persistent disk storage
or for sending to other processes

On Tue, Feb 1, 2022 at 8:37 AM ethiejiesa via Programming <
programm...@jsoftware.com> wrote:

> Maybe negative anwsers could help clarify things?
> - Why is a 2-column inverted table, together with appropriate access
> idioms,
>   not a dictionary?
> - HPC folk have been representing trees as cleverly-arranged arrays for
> years,
>   apparently. Why are tries [0] not dictionaries?
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to