Re: What techniques that everyone should know?
Posted: Fri Feb 28, 2014 12:48 am
I didn't knew that existed thanks for improving the function.
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")kikito wrote: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.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.
Code: Select all
print"cool"
love.graphics.setColor{255, 120, 200, 150}
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 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.Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
The non-numeric keys are ignored, thus. So maybe our is_array should be renamed to is_pure_sequence.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
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)kikito wrote:I would have made it return two values instead (the array + len). I do that all the time.Xgoff wrote:actually, it could be an issue. the 'n' field was (unfortunately) reintroduced to some extent in 5.2 via table.pack
Code: Select all
function SwitchBoolean( Boolean )
return not Boolean and true or false
end
Code: Select all
not Boolean and true
Code: Select all
or false
technically you only need `not Boolean`davisdude wrote:Switching booleans without an if-else-statement.I'll try to explain it, but if you have any questions about what this does, see this very well written article.Code: Select all
function SwitchBoolean( Boolean ) return not Boolean and true or false end
So, `Boolean` would be the boolean ( `true`, `false`, `nil` ) you're checking.
Let's break it down step by step: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
not Boolean and true
Otherwise, make it false.Code: Select all
or 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.
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...davisdude wrote:Oh...
I feel dumb now...
EDIT: What a crummy 400th post.
I was just, uh... demonstrating ternary operations with an arbitrary example... yeah, that's it.