lua-users home
lua-l archive

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


Sam Roberts <vieuxtech <at> gmail.com> writes:
> On Sun, Feb 1, 2009 at 2:31 AM, Joshua Haberman <joshua <at> reverberate.org>
wrote:
> > Languages like Ruby, Python, etc. allow you to define hash functions that
> > apply to your objects.  You define a hash function and a comparison
> > function, and this lets user-defined types be keys in hash tables based on
> > their values.
> 
> I'm curious what you would use this for?

Lots of set manipulation.  I'm using Lua to write the compiler for a parsing
framework (http://www.gazelle-parser.org), and this compiler does quite a lot of
Set manipulation:

$ grep Set:new compiler/* | wc -l
      63

Several times I've been forced to resort to stringifying my objects to get value
semantics, which is inefficient and feels like a hack.

> I've only seen it used once in ruby, in resolve.rb where DNS names are
> a sub-class String to make a case-insensitive (but case-preserving)
> string class. It worked fine, but if the language hadn't supported
> this it would have been easy to achieve the goals in other ways.
> 
> I think you could build in pure lua a data structure class, say Hash,
> that uses metamethods to index it's entries based on their __hash().
> Would that be an easier approach than patching the runtime, or not
> work for what you have in mind?

I would have to completely reimplement hash tables in Lua to get this to work,
which would be slow.  There's no way from Lua to use the Lua table
implementation's hash collision resolution algorithm.

Josh