lua-users home
lua-l archive

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


On 19/08/2011 9.36, Dirk Laurie wrote:
On Thu, Aug 18, 2011 at 10:46:27PM +0200, Lorenzo Donati wrote:

I stumbled into this idea while writing a long set-like table. Wouldn't
it be nice if the follwing:


set = { a = true, b = true, [1] = true }

could be abbreviated as:

set = { ["a","b",1] = true }


Uh-oh.  Bad timing.  We've just been directed on another thread
to<http://en.wikipedia.org/wiki/Parkinson%27s_Law_of_Triviality>,
which refers to Wadler's Law:

     the bulk of discussion on programming language design centers
     around syntax (which, for purposes of the argument is considered
     a solved problem), as opposed to semantics.

But to return to your point: surely this is a Lua idiom?

     set = {}
     for _,v in ipairs{"a","b",1} do set[v]=true end

Yes. It is. And I know that.

Please, reread my post, it wasn't focused on sets (see the message subject), but on general many-to-one mappings (of which a set may be viewed as a particular case, if you consider the characteristic function of the set as the mapping). I only mentioned sets as a trivial example.

The problem arise when the mapping is complex and the elements on the LHS are many more of those on the RHS, but those on the RHS are still many (not just true/false).

As I pointed out, you can always write the mapping the other way around and then reverse it, but it is error prone and verbose.

[...]


Dirk



-- Lorenzo