Page 6 of 8

Re: What techniques that everyone should know?

Posted: Fri Feb 28, 2014 12:48 am
by master both
I didn't knew that existed thanks for improving the function.

Re: What techniques that everyone should know?

Posted: Fri Feb 28, 2014 6:46 am
by Robin
A better way to go about things would be to make sure your sequences never have holes in the first place. :)

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 4:58 pm
by Xgoff
kikito wrote:
This is really clever, I like it, with the exception that something that is array-like wouldn't fit in here. That is to say, if you keep 'n' as the length of the array as a member of the table, then isArray is false.
As far as I know, table.getn and table.setn were deprecated in Lua 5.1 and removed in 5.2, as well as using a key called 'n' to handle the length. The "standard" definition of array does not include any non-array values any more (except for backwards-compatibility with Lua 5.0 libraries), so I don't think this is an issue.
actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack. (also, the 5.2 definition of table length does allow for non-numeric keys, assuming you still consider that an "array")

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 5:38 pm
by Zarty55
I have some little tricks that are really nice.

If a function only take a table or a string you can exclude the "()" between arguments:

Code: Select all

print"cool"
love.graphics.setColor{255, 120, 200, 150}
I use this a lot, it's one actually nice thing that I got in my entity system.

Also, having shortcuts for the love modules is really nice:

Code: Select all

function setUpReferences()
 	la = love.audio
	le = love.event
	lf = love.filesystem
	lg = love.graphics
	li = love.image
	lj = love.joystick
	lk = love.keyboard
	lma = love.math
	lm = love.mouse
	lp = love.physics
	lso = love.sound
	lsy = love.system
	lth = love.thread
	lti = love.timer
	lw = love.window
end
I actually had some more, but I can't remember them hahaha.

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 5:48 pm
by kikito
Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
:( I would have made it return two values instead (the array + len). I do that all the time. But oh, well. Then what table.pack returns in 5.2 is not an array.

EDIT: maybe we should be talking about sequences instead of arrays. On the table.pack doc, it is mentioned that "the resulting table might not be a sequence", and the term is defined like so:
We use the term sequence to denote a table where the set of all positive numeric keys is equal to {1..n} for some integer n, which is called the length of the sequence
The non-numeric keys are ignored, thus. So maybe our is_array should be renamed to is_pure_sequence.

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 6:04 pm
by Xgoff
kikito wrote:
Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
:( I would have made it return two values instead (the array + len). I do that all the time.
yeah, i wondered myself why they never did that... especially since none of the other non-deprecated table functions use that field anyway (and i hope that doesn't change)

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 10:05 pm
by davisdude
Switching booleans without an if-else-statement.

Code: Select all

function SwitchBoolean( Boolean )
	return not Boolean and true or false
end
I'll try to explain it, but if you have any questions about what this does, see this very well written article.

So, `Boolean` would be the boolean ( `true`, `false`, `nil` ) you're checking.
Let's break it down step by step:

Code: Select all

not Boolean and true
Both not Boolean and true have to be true. In this case, true will always be true, so now the other: not Boolean. As shown earlier in the thread, not makes true false and false true. So, if you input true, not true will be false.

Code: Select all

or false
Otherwise, make it false.

TL;DR/Summary:
Basically if both of the first options are true then it equal the second first option, or true in this case. Otherwise, it equals the second option, or false.

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 10:12 pm
by Xgoff
davisdude wrote:Switching booleans without an if-else-statement.

Code: Select all

function SwitchBoolean( Boolean )
	return not Boolean and true or false
end
I'll try to explain it, but if you have any questions about what this does, see this very well written article.

So, `Boolean` would be the boolean ( `true`, `false`, `nil` ) you're checking.
Let's break it down step by step:

Code: Select all

not Boolean and true
Both not Boolean and true have to be true. In this case, true will always be true, so now the other: not Boolean. As shown earlier in the thread, not makes true false and false true. So, if you input true, not true will be false.

Code: Select all

or false
Otherwise, make it false.

TL;DR/Summary:
Basically if both of the first options are true then it equal the second first option, or true in this case. Otherwise, it equals the second option, or false.
technically you only need `not Boolean`

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 10:17 pm
by davisdude
Oh... :P
I feel dumb now... :P
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.

Re: What techniques that everyone should know?

Posted: Mon Mar 03, 2014 11:52 pm
by Zarty55
davisdude wrote:Oh... :P
I feel dumb now... :P
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.
Hahaha there's no big deal. I've already made much horrible stuff than that. 6 months ago I wrote a function for drawing "filled circles" by checking if each pixel inside the "bounding box" of the radius was closer to the center than the radius. So yeah, not quite ideal...

I also used to normalize a vector that I had created like "x, y = cos(dir)*speed, sin(dir)*speed"...