lua-users home
lua-l archive

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


On Fri, Mar 16, 2012 at 17:51, Egor Skriptunoff
<egor.skriptunoff@rocketmail.com> wrote:
> Hello, Lua people!
>
>
> I have some suggestions.
> Please, take a look.
>
> ---------------------------------------
>
> 1)
> This function will be useful:
> bit32.weight(n) == Hamming weight of n
> (also known as population count,
> binary digit sum, count of set bits)
>
> ---------------------------------------
>
> 2)
> Frequently both quotient and remainder are needed.
> Let % operator returns pair of values:
> a % b == a-floor(a/b)*b, floor(a/b)
>
> ---------------------------------------
>
> 3)
> Lua lacks fast function to build initialized
> array.  A function like this will be useful
> if implemented in C:
>
> function table.new(size, value)
>   value = value or 0
>   local t = {}
>   for i = 1, size do
>     t[i] = (type(value)=='function')
>            and value(i) or value
>   end
>   return t
> end
>
> ---------------------------------------
>
> 4)
> Why table.concat() works only with tables of
> strings and numbers?
> table.concat() may use tostring() to work with
> arrays of any objects.
>
> ---------------------------------------
>
> 5)
> Syntactic sugar needed for primitive equality.
> Let a===b denotes rawequal(a,b)
> Inspired by Smalltalk :)
>
> ---------------------------------------
>
> 6)
> Why equality operator is so special in Lua?
> Any string or any number is always not equal
> to any table despite of __eq field in metatable.
> Example: it is impossible to implement rational
> numbers in nice manner:
>   local r = rationalNumber '1/12'
>   r = '2/3' - 2*r            -- ok
>   assert(r >  '1/3')         -- ok
>   assert(r >= '1/2')         -- ok
>   assert(r <= '1/2')         -- ok
>   assert(r == '1/2')         -- never
>   -- a blot on the landscape  :(
> Please let __eq metamethod to decide
> whether values of different types are equal.
> It will be useful for DSL Lua scripts,
> where comparison of object and string is handy.
>
> ---------------------------------------
>
> 7)
> Here is my suggestion about comparison operators.
>
> Comparison operators should return:
> **) nil, if values are not comparable
>       (3 > 'Hello') == nil
>       (3 == 'Hello') == nil
> **) false, if opposite comparison holds
>       (3 > 5) == false  -- because (3<=5)==true
>       (3 == 5) == false -- because (3~=5)==true
> **) any true value (i.e., except nil and false),
>     if comparison holds
>       (3 < 5) == true
>       (3 ~= 5) == true
>
>
> How to calculate undefined operators:
>
> Opposite operators should preserve nil values:
> *) a ~= b should be calculated as
>         if (a==b)==nil then return nil
>         else return not(a==b) end
>
> If only __lt is defined in metatable, then:
> *) a <= b should be calculated as
>         if (a>b)==nil then return nil
>         else return not(a>b) end
> *) a == b should be calculated as
>         if (a>b)==nil then return nil
>         else return not((a>b)or(a<b)) end
>
> If only __le is defined, then:
> *) a < b should be calculated as
>         if (a>=b)==nil then return nil
>         else return not(a>=b) end
> *) a == b should be calculated as
>         if (a>=b)==nil then return nil
>         else return (a>=b)and(a<=b) end
>
> If only __eq is defined, then:
> *) a < b should be calculated as
>         return nil
> *) a <= b should be calculated as
>         return nil
>
> If both __lt and __le are defined:
> *) a == b should be calculated as
>         if (a>=b)==nil then return nil
>         else return (a>=b)and(a<=b) end
>
> If both __lt and __eq are defined:
> *) a <= b should be calculated as
>         if (a==b)==nil then return nil
>         else return (a==b)or(a<b) end
>
> If both __le and __eq are defined:
> *) a < b should be calculated as
>         if (a==b)==nil then return nil
>         else return (not(a==b))and(a<=b) end
>
> ---------------------------------------
>
>
>
> And some questions:
>
> ---------------------------------------
>
> 8)
> PiL book says, "The generic loop shares
> two properties with the numeric loop:
> the loop variables are local to the loop body
> and you should never assign any value to them."
>
> Question: does generic loop variables indeed
> sensitive to assigning values to them?
>
> ---------------------------------------
>
> 9)
> Who are the author of bright idea about _ENV upvalue?
> Sandboxing in Lua is perfect now.
> I just want to say thank you.
>
> ---------------------------------------
>
> 10)
> Did anybody make attempt to write a specializer
> for implementing Futamura-Turchin projections
> in Lua (or in subset of Lua)?
> (see "Partial evaluation" on Wikipedia for
> brief description of the subject)
>
> ---------------------------------------
> -- Egor
>

1) How often is this needed? I don't even know what applications use this.

2) A nice idea, but maybe bad for performance, and having an operator return two
 values may be both difficult to implement and difficult to understand.

3) I think what you want here is a way to preallocate space, like the narr and
 nret arguments to lua_createtable? That could be useful.

4) Agreed, this is often an annoyance. For that matter, table.map_concat, which
 would do like table.concat but also take a function to apply to each
item. Then
 you'd just do: s = table.map_concat(t, ' ', tostring)

5) Why?

6) Agree, the way some metamethods don't trigger depending on the types of the
 operands is a bit of a pain.

7) Seems very awkward and error-prone. You're making basic boolean logic
 operators no longer work with booleans, and since nil == false, introducing all
 sorts of potential error.

8) If the manual/PiL says you shouldn't, then you probably shouldn't.

9) Agree.

10) No idea.

-- 
Sent from my toaster.