Yup, yours is much betterfoo0 wrote:You can use table.insert like in your first version:Code: Select all
function mergetables(...) local ret = {} for i, tbl in ipairs {...} do for k, v in pairs(tbl) do if type(k) == "number" then table.insert(ret, v) else ret[k] = v end end end return ret end
Small Useful Functions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: Small extra functions
Re: Small extra functions
Anyway, I think that mixed solution is rather ugly. It doesn't overwrite conflicting number keys, but overwrites the rest of types? type(k) == "number" doesn't even mean that the key is an array index (e.g. 0, -1, 0.5 are not "valid" indexes that ipairs would respect).
It would be better to have two separate merge functions. One that only merges array parts of passed tables, with no overwrites but changed order:
Second function that merges whole tables (array and dictionary parts), where last table passed as an argument wins when in conflict (Robin's version).
It would be better to have two separate merge functions. One that only merges array parts of passed tables, with no overwrites but changed order:
Code: Select all
function mergeArrayParts(...)
local ret = {}
for _, tbl in ipairs {...} do
for _, v in ipairs(tbl) do
table.insert(ret, v)
end
end
return ret
end
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: Small Useful Functions
I just thought of something: stencils and inverted stencils are basically the same thing. Why not merge them? So, I made this:
Code: Select all
setStencil = love.graphics.setStencil
function love.graphics.setStencil(function, isInverted)
local function = function or false
if function then
if isInverted then
love.graphics.setInvertedStencil(function)
else
setStencil(function)
end
else
setStencil()
love.graphics.setInvertedStencil()
end
end
Re: Small Useful Functions
The solution you suggest, makes use of a flag argument ("isInverted"). If you are striving for clean code, you should prefer two function over one function with a flag argument.
This is because calling:
is easier to read and understand than
Especially if you haven't looked at your code for a while, the word "false" does not tell you, what exactly it changes in the function.
Your solution would make the API shorter, because it reduces the number of functions, but it will make code less readable.
Check out the two videos:
Indie your face: Clean code
Indie your face: Clean code Part 2
This is because calling:
Code: Select all
love.graphics.setInvertedStencil(myStencil)
Code: Select all
love.graphics.setStencil(myStencil, false)
Your solution would make the API shorter, because it reduces the number of functions, but it will make code less readable.
Check out the two videos:
Indie your face: Clean code
Indie your face: Clean code Part 2
Check out my blog on gamedev
Re: Small Useful Functions
I'm with the great micha on this but if you're hell-bent on this I would do something like:
Then your code would look like:
but I prefer staying with the documented Love functions.
Code: Select all
function setStencil( fn, mode )
if fn then
if mode == 'inverted' then
love.graphics.setInvertedStencil( fn )
else
love.graphics.setStencil( fn )
end
else
love.graphics.setStencil()
love.graphics.setInvertedStencil()
end
end
Code: Select all
setStencil( stencil_function )
-- or
setStencil( stencil_function, 'inverted' )
--
setStencil()
Re: Small Useful Functions
I haven't really thought about this before, but this is probably the reason, why the drawing functions for polygons, rectangles and circles have the drawing mode as a string and not as a boolean. I just realized that. Interesting!
Check out my blog on gamedev
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Small Useful Functions
Plus, with booleans you can only have two options. What if, for 0.10.0, the devs want to add a 'linefill' mode that is better than drawing with 'line' and 'fill' seperately? If a boolean was used, you couldn't do that, because there are only two values available.micha wrote:I haven't really thought about this before, but this is probably the reason, why the drawing functions for polygons, rectangles and circles have the drawing mode as a string and not as a boolean. I just realized that. Interesting!
Help us help you: attach a .love.
Re: Small Useful Functions
About table merging, doesn't this one do the job?
Code: Select all
function mergeTables(addTo, addFrom)
local a, b
for a, b in pairs(addFrom) do
table.insert(addTo, b)
end
end
All your code are belong to us.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Small Useful Functions
Depends.szmol96 wrote:About table merging, doesn't this one do the job?Code: Select all
function mergeTables(addTo, addFrom) local a, b for a, b in pairs(addFrom) do table.insert(addTo, b) end end
If the given tables are both arrays, it should do the job. But in case they are dictionnaries, the output will certainly not be the one expected.
And, about you code, the local a,b declaration is not needed.
Re: Small Useful Functions
Roger that, thanks!And, about you code, the local a,b declaration is not needed.
All your code are belong to us.
Who is online
Users browsing this forum: Google [Bot] and 1 guest