Re: associative arrays
- Posted by Kat <gertie at PELL.NET> May 25, 2000
- 573 views
Sorry about being behind, but stuff happens here to sidetrack me.... ----- Original Message ----- From: "Carl R. White" <cyrek at BIGFOOT.COM> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Monday, May 22, 2000 4:07 AM Subject: Re: associative arrays > 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 "OO" .. pronounced like "ewwww" (sorta) > > >>[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 What about gettok() ? It wasn't set up to do nested seqs, but it could be, rather easily, i think, just by being recursive. Then toss in a little more overhead and you could do this: * UNTESTED CODE * integer rec rec = db("animal","cow",0) if rec then ? db(rec,noise,0) -- moo ? db(rec,specie,0) -- <shrug> -- and with more code: ? db(rec,"test,attr,ruminant",0) -- TRUE (it's a cow) -- and with more code: inc rec ? db(attr,ruminant,rec) -- llama? inc rec ? db(attr,ruminant,rec) -- goat? end if db(object parm1, object parm2, integer where) If parm1 is an atom integer, then assume the programmer knows what sie is doing, and deal with that rec number. If parm1 is a word sequence (aka legal language string) then go find parm2 as an element of parm1. If where > 0 then look only at recs greater than rec# where. No OO. No invert(). Multiple returns possible. No fixed anything. One list. The db can be freely added to, anywhere in the db. Kat