Page 3 of 5

Re: Share your favourite helper functions

Posted: Sat Nov 17, 2012 9:43 am
by spir
Robin wrote:
Inny wrote:So, implementing round(x) as floor(x+0.5) is wrong.
Not necessarily. There are a lot of different rules for tie-breaking, and one is not necessarily "right" or "wrong".
Thank you to bring the subject on the table, Robin. Can one point to a doc where is explained the reason for the "round-to-even" rule? I read several times that it's supposed to produce equal counts of numbers up and down, but never expalined why. For me, intuitively, common, school-math round to next (x.5 --> x+1, -x.5 --> -x-1) is obviously right. My intuition is probably wrong here though, I won't contradict great brains of mathematicians, but I want to understand why I'm wrong. :(

Denis

Re: Share your favourite helper functions

Posted: Sat Nov 17, 2012 9:47 am
by spir
Robin wrote:
spir wrote:EDIT2: But my dream tool func is rather:

Code: Select all

   local x = 1
   note(x)      --> "x : 1"
   --> not just "1"
Option: add in parens the func name and line number of the "note" statement:

Code: Select all

f = function (x)
   local x = 1
   note(x)      --> "x : 1 (f, 333)"
end
Those two aren't really possible in Lua (unless you hack together a preprocessor or manage to incorporate MetaLua or something).
Yes, you are right, Robin. Thus the word "dream"... (but this may need a new check with tools in the debug lib allowing to get local names, I guess; haven't studied yet, though).

Re: Share your favourite helper functions

Posted: Sat Nov 17, 2012 10:11 am
by spir
spir wrote:
Robin wrote:
Inny wrote:So, implementing round(x) as floor(x+0.5) is wrong.
Not necessarily. There are a lot of different rules for tie-breaking, and one is not necessarily "right" or "wrong".
Thank you to bring the subject on the table, Robin. Can one point to a doc where is explained the reason for the "round-to-even" rule? I read several times that it's supposed to produce equal counts of numbers up and down, but never expalined why. For me, intuitively, common, school-math round to next (x.5 --> x+1, -x.5 --> -x-1) is obviously right. My intuition is probably wrong here though, I won't contradict great brains of mathematicians, but I want to understand why I'm wrong. :(

Denis
I answer myself about simplicity: the wikipedia article now has nice formulas, which led me to:

Code: Select all

--[[  Round to next integer and round to given precision.
      Using "round half to infinite" or "round half away from zero".
      A formula for round0 is : sign(n) * floor(abs(n) + 0.5)
]]
math.round0 = function (n)       -- to next int (1.5 --> 2, -1.5 --> -2)
   if n >= 0 then return math.floor(n + 0.5)
   else return - math.floor(-n + 0.5) 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
denis

Re: true escaped quoted string?

Posted: Sat Nov 17, 2012 10:20 am
by Robin
spir wrote:I don't get what you mean here: I'm producing an escaped string (thus backslahes are all escaped in the replacement patterns).
You had elseif c == '\"'. I changed it to elseif c == '"'. That's all.
spir wrote:(other things)
Do you know Python? It has two functions for converting things to strings: str, which is like Lua's tostring, and repr, which is like your tostring. To only have str is good, to have both is better, but to only have repr is madness, because now you have a problem when you want to display things to the user.

To prevent "\"\\\"nested\\\"\"" quoting stacking up, you need to write a function real_tostring:

Code: Select all

function real_tostring(t)
    if type(t) == "string" then
        return t
    end
    return tostring(t)
end
and then you need to change all functions that look like this:

Code: Select all

function someFunc(someArg)
    someArg = tostring(someArg)
    -- ...
to

Code: Select all

function someFunc(someArg)
    someArg = real_tostring(someArg)
    -- ...
The real problem comes when someone else has to maintain your code, and they find al these calls to real_tostring in your code. They'll assume tostring(foo) == foo holds if foo is a string (because it usually does), and reason:
"real_tostring is the same as"

Code: Select all

function real_tostring(t)
    if type(t) == "string" then
        return tostring(t)
    end
    return tostring(t)
end
"which is the same as"

Code: Select all

function real_tostring(t)
    return tostring(t)
end
"which means real_tostring is just a waste of stack space. I'll remove the functions and change all the calls to real_tostring to tostring."

And that's a problem.

So: I think your function string.quoted is really useful, and if you write a function that writes out tables, be sure to use string.quoted on string keys and values, but you shouldn't change the behaviour of standard library functions (especially not one as basic as tostring).

Re: Share your favourite helper functions

Posted: Sat Nov 17, 2012 10:45 am
by Azhukar
spir wrote:
Robin wrote:
spir wrote:EDIT2: But my dream tool func is rather:

Code: Select all

   local x = 1
   note(x)      --> "x : 1"
   --> not just "1"
Option: add in parens the func name and line number of the "note" statement:

Code: Select all

f = function (x)
   local x = 1
   note(x)      --> "x : 1 (f, 333)"
end
Those two aren't really possible in Lua (unless you hack together a preprocessor or manage to incorporate MetaLua or something).
Yes, you are right, Robin. Thus the word "dream"... (but this may need a new check with tools in the debug lib allowing to get local names, I guess; haven't studied yet, though).
Sure they are possible:

Code: Select all

local function note(var,scope)
	local printedstartlocal = false
	local printedstartenviroment = false
	if ((not scope) or (scope == "local")) then
		local i = 0
		while (true) do
			i = 1 + i
			local name,value = debug.getlocal(2,i)
			if (name ~= nil) then
				if (name:sub(1,1) ~= "(") then
					if (value == var) then
						if (not printedstartlocal) then
							printedstartlocal = true
							print("Local variables:")
						end
						print(name.." : "..tostring(value))
					end
				end
			else
				break
			end
		end
	end
	if ((not scope) or (scope == "enviroment")) then
		local env = getfenv()
		for name,value in pairs(env) do
			if (value == var) then
				if (not printedstartenviroment) then
					if (printedstartlocal) then print("") end
					printedstartenviroment = true
					print("Enviroment variables:")
				end
				print(name.." : "..tostring(value))
			end
		end
	end
end

y = 123
local function test(x)
	local x = 123
	local z = 123
	local q = 1
	note(x)
end
test()

Re: true escaped quoted string?

Posted: Sat Nov 17, 2012 11:59 am
by spir
Robin wrote: So: I think your function string.quoted is really useful, and if you write a function that writes out tables, be sure to use string.quoted on string keys and values, but you shouldn't change the behaviour of standard library functions (especially not one as basic as tostring).
Yo, I understand your point of view better, now. And you are certainly right, in the general case. (Maybe I code too much on my own, or rather actually echanging with a few people who mostly share my obsession of practicle debugging tools. Anyway.)

Your solution of explicitely using string.quoted is good and works anyway. The only point is it (systematically) complicates a bit the coding of tostring functions or more generally "output writing" functions, in every single case. I mean when we write such a func for a given element, for a general "type" like array-, set-, . object-like tables, or for a custom type, then we always need to consider string items especially. Not a big deal, but I find myself idiot when faced with such stupid, repetitively mechanic tasks (that's precisley what machines are good for, thus I wish the machine to do it for me).

Anyway, you are indeed very on the point of sharing code.

Denis

Re: Share your favourite helper functions

Posted: Sun Nov 18, 2012 6:13 pm
by Nsmurf
I recently made a blog post about loading levels from images. It takes a file path and a array as input, and returns a level that is compatible with YellowAfterLife’s Platformer engine, although you could export it into almost any format. It's also a nice example of how to use love.image, for those of you who are as confused about it as I was :crazy:.

Re: Share your favourite helper functions

Posted: Tue Nov 20, 2012 1:01 am
by verilog
Nice code snippet, Nsmurf, thanks for sharing. Also, nice blog post on collision detection, I found it quite helpful.

Re: Share your favourite helper functions

Posted: Tue Nov 20, 2012 1:20 am
by Nsmurf
verilog wrote:Nice code snippet, Nsmurf, thanks for sharing. Also, nice blog post on collision detection, I found it quite helpful.
Thanks, I'm glad to hear that people like my blog! :awesome:

Re: Share your favourite helper functions

Posted: Tue Nov 20, 2012 1:21 am
by substitute541
Nsmurf wrote:I recently made a blog post about loading levels from images. It takes a file path and a array as input, and returns a level that is compatible with YellowAfterLife’s Platformer engine, although you could export it into almost any format. It's also a nice example of how to use love.image, for those of you who are as confused about it as I was :crazy:.
Nice! Finally, something easier to load my levels :D