[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [ANN] Lua 5.3.0 (work1) now available
- From: Joshua Jensen <josh.jjensen@...>
- Date: Sat, 06 Jul 2013 14:51:42 -0600
----- Original Message -----
From: Luiz Henrique de Figueiredo
Date: 7/6/2013 12:26 PM
The only thing I found that appears to be missing is the ability to
determine the actual type of the number.
One shouldn't need to do this but you can do it as follows:
function isinteger(x)
return tonumber(tostring(x),10)~=nil
end
This isinteger() function is pretty heavyweight and becomes a garbage
collection nightmare when operating over a lot of data. But I guess it
can be done inefficiently.
In any case, I wouldn't go so far as to say one shouldn't need to know
the type of the number. There are a number of reasons for this. Most
of my ideas deal in serialization; another example is below. However, I
can also think of cases where I want to ensure the incoming arguments to
a function are integers. Related to serialization is proper reflection.
Perhaps there could be a subtype() function or even an extension of
type() that either returns two values 'number' and 'integer' or that
takes an extra argument to return the actual numeric type?
-- Write nifty binary format
input =
{
these_are_floats = { 1.0, 2.0, 3.0, 4.0, 5.0 },
these_are_integers = { 1, 2, 3, 4, 5 },
these_are_strings = { "one", "two", "three", "four", "five" },
}
function WriteBinaryBlob(data)
if type(data[1]) == 'number' then
if subtype(data[1]) == 'integer' then
return struct.pack(string.rep('I', #data), unpack(data))
else
return struct.pack(string.rep('f', #data), unpack(data))
end
elseif type(data[1]) == 'string' then
return struct.pack(string.rep('s', #data), unpack(data))
end
end
for key, value in pairs(input) do
WriteBinaryBlob(value)
end
It would be nice if there was a method to do so in Lua itself. One use
case is for serializing integer values to disk in text form properly.
tostring does this already.
True, but I may choose not to use tostring().
Anyway, thanks for all of your efforts getting this going!
-Josh