Re: associative arrays
- Posted by "Carl R. White" <cyrek at BIGFOOT.COM> May 22, 2000
- 548 views
I was going to post this reply on Friday, but my work 'net connection was terminated before I got chance to send it. Here we go, 3-4days late... On Fri, 19 May 2000 11:04:56 +1200, jiri babor <jbabor at PARADISE.NET.NZ> wrote: >Carl wrote: > >> [Types are] unnecessary baggage. A debugging tool of dubious value at >> best. Hmm. I always thought they were a good feature of the language, myself. Even C programmers use typedefs occasionally. Maybe I'm starting to show OO tendencies :-o >>[global?] constant -- Readability >> Keys = 1, >> Data = 2 > >How is Keys and Data more readable than 1, 2 ? Especially since there >is no reason to have them global - no one will ever even see them! More OO features. We could allow the library user access to features like: list myDB sequence myKeys -- Create database -- etc. etc. mykeys = myDB[Keys] I realise this gives a hint to the internal structure, and perhaps a library function to return the sequence of keys is a better idea, but there's less work for the library writer this way. >>global function invert(list assoc) >> -- Generate a reverse lookup list >> return {assoc[Data], assoc[Keys]} >>end function > >This one has already cost me one sleepless night. For heaven's sake, >can you suggest a single possible use of it? To go back to my animals example, and using your functions; Assume we have a list with the following data: list noises = -Keys-+-Data-- cat | miaow dog | woof pig | oink cow | moo ...we can easily query the list to find out what noise a pig makes: sequence noise noise = fetch(noises, "pig") --> "oink" But there's no function for finding out what animal makes the noise "moo". By inverting the database, the noises become the Keys and the animals become the Data. So we can say this: list animals sequence animal animals = invert(noises) animal = fetch(animals, "moo") --> "cow" It also works for other one-to-one relationships. It's not so perfect if a Key points to a group of Data though. e.g.: mary --has_chilren--> {jack, joe, jane} ...becomes: {jack, joe, jane} --has_mother--> mary ...when we probably wanted something like this: jack --has_mother--> mary joe --has_mother--> mary jane --has_mother--> mary >>Carl -- Wondering how he manages to irritate Jiri so much... > >On the contrary, I have never been irritated by you, so far. After >all, we usually appear to finish on the same side in most arguments, >on a similar wavelength... /me points remote tuning device in Jiri's direction and starts turning the dial... Carl -- Where *has* that .sigfile got to?!