Share your favourite helper functions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Share your favourite helper functions
I'm pretty sure it's doable if you have access to Lua's debug library. You can get a list of the current locals with their names, so printing the name of a particular one shouldn't be very different.
The debug lib is not guaranteed to be defined in all cases. A function which relies on debug should take this into account - maybe "failing gracefully" (supressing input from debug if it's not present, instead of just raising an error and dying).
The debug lib is not guaranteed to be defined in all cases. A function which relies on debug should take this into account - maybe "failing gracefully" (supressing input from debug if it's not present, instead of just raising an error and dying).
When I write def I mean function.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Share your favourite helper functions
Oh, yeah, that might yield some results. Something like:kikito wrote:I'm pretty sure it's doable if you have access to Lua's debug library.
Code: Select all
function getnameof(value)
local idx = 1
while true do
local ln, lv = debug.getlocal(2, idx)
if ln ~= nil then
if value == lv then return ln end
else
error "no local name for value found"
end
idx = 1 + idx
end
end
Help us help you: attach a .love.
true escaped quoted string?
I just realised that, apparently, the "%q" string format tag does not truely escaped newlines !!!
Why so? It does escape tab properly, so why not newlines? And what's this backslash roaming there anyway? %q adds an '\' but only "graphical" (so to say), a real '\\' char without escaping the newline. (Hum, I'll check the bytes to see what's in there exactly). However, in the meanwhile, here is a func that does the job. Tell me if it's correct (I'm not sure of all what can be a special char in Lua), and if there is a simpler/better/more efficient way to do it. If you add (as I did) the following meta-incantations, then your strings will print out properly escaped (thus, ideal for programmer feedback) as default:
Denis
Code: Select all
print (string.format("%q", "XXX\tXXX\nXXX"))
--[[ output:
"XXX\9XXX\
XXX"
]]
Code: Select all
string.quoted = function (s)
local esc_s = s:gsub(".", function (c)
if c < ' ' then return '\\' .. string.byte(c)
elseif c == '\"' then return '\\"'
elseif c == '\\' then return '\\\\'
else return c
end
end
)
return '"' .. esc_s .. '"'
end
-- quick test
print(string.quoted( " \t \n \" \\ \0 \31 "))
string_mt = getmetatable("")
string_mt.__tostring = string.quoted
-- re quick test
print " \t \n \" \\ \0 \31 "
... la vita e estrany ...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: true escaped quoted string?
It does, though.spir wrote:I just realised that, apparently, the "%q" string format tag does not truely escaped newlines !!!
Code: Select all
somestring = "hello\
world"
Code: Select all
somestring = "hello\nworld"
Your string.quoted looks good to me, although I don't know how efficient gsub over "." is.
Two things:
Code: Select all
elseif c == '"' then return '\\"'
More importantly:
Code: Select all
string_mt = getmetatable("")
string_mt.__tostring = string.quoted
Normally, tostring is the identity function for strings. So tostring(foo) == foo if foo is a string. Those two lines break that.
Help us help you: attach a .love.
Re: Share your favourite helper functions
That implementation of math.round on https://love2d.org/wiki/Additional_math is incorrect.
Round(-1.5) == -2, as seen here: http://www.wolframalpha.com/input/?i=round%28-1.5%29
Floor(-1.5+0.5) == -1, as seen here: http://www.wolframalpha.com/input/?i=fl ... %2B+0.5%29
So, implementing round(x) as floor(x+0.5) is wrong.
Round(-1.5) == -2, as seen here: http://www.wolframalpha.com/input/?i=round%28-1.5%29
Floor(-1.5+0.5) == -1, as seen here: http://www.wolframalpha.com/input/?i=fl ... %2B+0.5%29
So, implementing round(x) as floor(x+0.5) is wrong.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Share your favourite helper functions
Not necessarily. There are a lot of different rules for tie-breaking, and one is not necessarily "right" or "wrong".Inny wrote:So, implementing round(x) as floor(x+0.5) is wrong.
Help us help you: attach a .love.
Re: Share your favourite helper functions
Egad, I tested a bunch of values in wolfram alpha and it appears that they implement the Half To Even style. I'm not sure what to think about this now.Robin wrote:Not necessarily. There are a lot of different rules for tie-breaking, and one is not necessarily "right" or "wrong".Inny wrote:So, implementing round(x) as floor(x+0.5) is wrong.
Re: true escaped quoted string?
Yo, I'll change that (with a gsub mapping special chars, and another for the rest of those < 0x20).Robin wrote: Your string.quoted looks good to me, although I don't know how efficient gsub over "." is.
I don't get what you mean here: I'm producing an escaped string (thus backslahes are all escaped in the replacement patterns).Robin wrote: Two things:You don't need the backslash there, and in other places you don't use it.Code: Select all
elseif c == '"' then return '\\"'
Yo, it's a nice property, but have we the choice. __tostring is launched by a call to tostring, isn't it? So, if I want string to be correct eg in table out put, I must hook tostring. So that eg --provided you have a proper tostring for table notation to your taste--, tostring(t) works fine:Robin wrote: More importantly:I wouldn't do this.Code: Select all
string_mt = getmetatable("") string_mt.__tostring = string.quoted
Normally, tostring is the identity function for strings. So tostring(foo) == foo if foo is a string. Those two lines break that.
Code: Select all
t = {[true]="true", ['nil']="nil", nltab="\n\t", [1]="1", [9]="9"}
print(t)
--> {true="true", 9="9", 1="1", nltab="\n\t", "nil"="nil"}
-- or
--> {true="true", 9="9", 1="1", nltab="\9\10", "nil"="nil"}
-- but not (!) that:
--[[
{true=true, 9=9, 1=1, nltab=
, nil=nil}
]]
programmer --> notation -----> Lua --> tostring of s -------> notation --> programmer
With notations equal on both sides as far as possible (with funcs, threads, userdata, let's wait for next Xmas).
[/code]
... la vita e estrany ...
Re: Share your favourite helper functions
I think as you (but my math knowledge is limited to faraway school). I use the following:Inny wrote:That implementation of math.round on https://love2d.org/wiki/Additional_math is incorrect.
Round(-1.5) == -2, as seen here: http://www.wolframalpha.com/input/?i=round%28-1.5%29
Floor(-1.5+0.5) == -1, as seen here: http://www.wolframalpha.com/input/?i=fl ... %2B+0.5%29
So, implementing round(x) as floor(x+0.5) is wrong.
Code: Select all
math.round0 = function (n) -- to next int (1.5 --> 2, -1.5 --> -2)
local i = math.floor(n)
if n >= 0 then
if (n - i) < 0.5 then return i else return i + 1 end
else
if (n - i) <= 0.5 then return i else return i + 1 end
end
end
math.round = function (n, p) -- to given power-of-ten precision
local factor = 10^p
local mult = n * factor
local mult_round0 = math.round0(mult)
return mult_round0 / factor
end
end
Code: Select all
note("=== rounding to next int ===")
local rnd0 = math.round0
for _,i in ipairs {0.0, 1.0, 1.1, 1.5, 1.9} do
notef("math.round0(%.1f) : %s", i, rnd0(i))
end
for _,i in ipairs {-1.0, -1.1, -1.5, -1.9} do
notef("math.round0(%.1f) : %s", i, rnd0(i))
end
line()
note("=== rounding to given precision ===")
local rnd = math.round
local n = 123.4567890
for _,p in ipairs {7,3,2,1,0,-1,-2} do
notef("math.round(%f, %d) : %s", n, p, rnd(n,p))
end
local n = -123.4567890
for _,p in ipairs {7,3,2,1,0,-1,-2} do
notef("math.round(%f, %d) : %s", n, p, rnd(n,p))
end
Also, why does do you think Lua provides us with floor and ceil? I think i never, ever needed one of them (and they are highly dangerous to use, due to those corner cases --and we humans seem to be unable to think right with negative numbers, as shown by this example). I constantly need instead int-part and round.
Denis
... la vita e estrany ...
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests