lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


[off list]

Thanks for the explainations, David.

Denis

David Given <dg@cowlark.com> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> spir wrote:
> [...]
> >>> -3- How often does it happen to have string keys which are not kind of names?
> >> Sorry, don't know what you mean here.
> > 
> > Sure, I was not really clear. Probably I meant two things:
> > * Names in the sense of identifier, so that we can write subscripting t.name.
> > * Names in the conceptual sense, meaning that keys are names of attributes in the model.
> > So, my questions was whether there are common cases where strings which happen to be table keys are not names in one or the other sense. Id est mapping strings (other than names) to whatever.
> 
> I commonly use tables as keys whenever I want to keep data associated
> with objects that I don't own. For example, if a library hands me a
> parsed XML tree, each element is a table; if I want to tag elements, I
> can't write to the tree itself without changing it, so I create a table
> mapping XML elements to a structure of my control. Because each XML
> element is a object, which is a table, each one is considered unique
> when used as table keys.
> 
> [...]
> >>> -4- How often does it happen to have keys which are neither strings nore integers?
> >> Again, I do it quite a lot, usually for sets, where t[k] == true for
> >> values in the set.
> > 
> > Here I don't understand. I know the use of mapping as sets with items as pseudo-keys (to take profit of quick and constant time access). But I don't see what you mean above.
> 
> When all you care about is whether the object is in the set or not, then
> you simply use the item as a key with true as the value. (Any non-nil
> value will work, but true is conventional.)
> 
> function add_to_set(set, object) set[object] = true end
> function remove_from_set(set, object) set[object] = nil end
> function test_in_set(set, object) return set[object] end
> 
> The classic example of when this kind of thing is useful is when
> traversing a cyclic graph and you want to keep track of which nodes
> you've already seen.
> 
> Tables are all things to all people --- you can persuade them to become
> most primitive data structures! I haven't yet found a cunning sorted
> list idiom, though...
> 
> - --
> ┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
> │
> │ "Under communism, man exploits man. Under capitalism, it's just the
> │ opposite." --- John Kenneth Galbrith
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iD8DBQFLDFp8f9E0noFvlzgRApzTAJ93fTgM4xbIylKqCUeL+FOj4JZlaACfQVAT
> feGv/DdjuY/gT3Q43exgdUQ=
> =glR6
> -----END PGP SIGNATURE-----
> 




________________________________

la vita e estrany

http://spir.wikidot.com/