Small Useful Functions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Small extra functions

Post by HugoBDesigner »

foo0 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
Yup, yours is much better :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Re: Small extra functions

Post by foo0 »

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:

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
Second function that merges whole tables (array and dictionary parts), where last table passed as an argument wins when in conflict (Robin's version).
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Small Useful Functions

Post by HugoBDesigner »

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
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Small Useful Functions

Post by micha »

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:

Code: Select all

love.graphics.setInvertedStencil(myStencil)
is easier to read and understand than

Code: Select all

love.graphics.setStencil(myStencil, false)
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
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Small Useful Functions

Post by Ref »

I'm with the great micha on this but if you're hell-bent on this I would do something like:

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
Then your code would look like:

Code: Select all

setStencil( stencil_function )
-- or
setStencil( stencil_function, 'inverted' )
--
setStencil()
but I prefer staying with the documented Love functions.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Small Useful Functions

Post by micha »

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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Small Useful Functions

Post by Robin »

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!
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.
Help us help you: attach a .love.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: Small Useful Functions

Post by szmol96 »

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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Small Useful Functions

Post by Roland_Yonaba »

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
Depends.
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.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: Small Useful Functions

Post by szmol96 »

And, about you code, the local a,b declaration is not needed.
Roger that, thanks!
All your code are belong to us.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 45 guests