[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: RFE: table ctor syntax enhancement for many-to-one mappings
- From: Dirk Laurie <dpl@...>
- Date: Sat, 20 Aug 2011 21:10:08 +0200
On Fri, Aug 19, 2011 at 05:29:53PM +0200, Lorenzo Donati wrote:
>
> I think syntax should not be designed with the focus of simplifying
> writing code. The design goal should be simplifying *reading* code.
>
Remember that Luiz once said:
A language does not grow in a healthy way by adding things
for which you cannot find a reason to omit.
The remark made the OP lose his temper, but the philosophy behind it is
what has kept Lua lean and hungry through seventeen years of development.
So one must always weigh the a new proposal against the way one would
implement it at present, and decide whether it is so much more readable
and so much more convenient and so much more efficient that its advantages
outweigh the disadvantage of making Lua bigger.
Now on this question of many-keyed values in a table. I'm taking
as an example a simple function on the first 25 positive integers.
The most compact notation is this:
f = {1,1,1,2,1; 2,1,2,3,2; 1,2,1,2,3; 2,1,2,1,2; 3,2,1,2,5}
but (a) it is not available for a non-array; (b) it's very
demanding for a human to read.
Your proposal is without doubt much more readable. Anyone can
instantly see what the function is.
f = { [1,2,3,5,7,11,13,17,19,23] = 1,
[4,6,8,10,12,14,16,18,20,22,24] = 2,
[9,15,21] = 3,
[25] = 5 }
But Tony's implementation in current Lua is not too far behind
(although I'm not overfond of the name `flatten_keys`:
-- see his post for the definition of `flatten_keys`
f = flatten_keys { [{1,2,3,5,7,11,13,17,19,23}] = 1,
[{4,6,8,10,12,14,16,18,20,22,24}] = 2,
[{9,15,21}] = 3,
[25] = 5 }
as is the similar idea with inverted tables:
function unindex(a)
local b={}
for k,t in pairs(a) do for _,v in ipairs(t) do
b[v]=k
end end
return b
end
f = unindex { [1] = {1,2,3,5,7,11,13,17,19,23},
[2] = {4,6,8,10,12,14,16,18,20,22,24},
[3] = {9,15,21},
[5] = {25} }
There seems to be no reason to omit the proposed syntax extension. ;-)
Dirk