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

Re: Small Useful Functions

Post by Robin »

It also might not add them in the order you want to -- if you want the values from the other table added in order, use ipairs.
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 »

Gotcha.
All your code are belong to us.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Small Useful Functions

Post by Doctory »

Code: Select all

requirei = local function(d, ...)
	args = {...}
	for k, i in ipairs(args) do
		file = d .. i
		require(file)
	end	
end
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 »

Or:

Code: Select all

local function requirei(d, ...)
	for i, v in ipairs{...} do
		require(d .. v)
	end	
end
,

which has the advantage of not overwriting or leaking the globals args and file. Also this doesn't contain a syntax error on the first line.
Help us help you: attach a .love.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Small Useful Functions

Post by Doctory »

i suppose this would be helpful as well:

Code: Select all

local function clear(t)
	for i,_ in ipairs(t) do
		table.remove(t, i)
	end
end
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 »

That function is both ineffective and inefficient: it only removes half of the items, and it takes 4 times as long for a table that's 2 times as big.

Better would be:

Code: Select all

local function clear(t)
	for i = #t, 1, -1 do
		t[i] = nil
	end
end
This removes all the items, without shuffling them around.
Help us help you: attach a .love.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Small Useful Functions

Post by davisdude »

Hopefully there's nothing wrong with this one (just to keep Robin's job a little easier ;))

Code: Select all

local function RemoveHole( Table ) -- Removes nils from tables.
	local New = {}

	for Index, Value in pairs( Table ) do
		New[Index] = Value
	end
	
	return New
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Small Useful Functions

Post by micha »

Robin wrote:Better would be:

Code: Select all

local function clear(t)
	for i = #t, 1, -1 do
		t[i] = nil
	end
end
If you remove all of the entries, then you don't even need to traverse the table backwards. This works, too:

Code: Select all

local function clear(t)
	for i = 1,#t do
		t[i] = nil
	end
end
davisdude wrote:Hopefully there's nothing wrong with this one (just to keep Robin's job a little easier ;))

Code: Select all

- snip -
What exactly should this function do? What do you mean by "remove nils values?" A variable with value "nil" does not exist. So if you have a nil-value in a table then the key-value pair does not exist. Assigning nil to a variable is a way to delete variable.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Small Useful Functions

Post by davisdude »

Ya know, I'm not even sure what I was thinking when I wrote that.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Sapper
Prole
Posts: 15
Joined: Sun Oct 05, 2014 12:01 am

Re: Small Useful Functions

Post by Sapper »

Heres a few, kinda useful for tetris type games and map generating

new array

Code: Select all

function newAr(h,w)
	local ar = {}
	for i=1,h do
		ar[i] = {}
		for j=1,w do
			ar[i][j] = 0
		end
	end
	return ar
end
transpose array

Code: Select all

function transpose(ar)
	local ar2 = newAr(#ar,#ar[1])
	for i=1,#ar do
		for j=1,#ar[i] do
			ar2[j][i] = ar[i][j]
		end
	end
	return ar2
end
flip array

Code: Select all

function vFlip(ar)
	local ar2 = newAr(#ar,#ar[1])
	for i=1,#ar do
		for j=1,#ar[i] do
			ar2[#ar - i + 1][j] = ar[i][j]
		end
	end
	return ar2
end

function hFlip(ar)
	local ar2 = newAr(#ar,#ar[1])
	for i=1,#ar do
		for j=1,#ar[i] do
			ar2[i][#ar[i]-j + 1] = ar[i][j]
		end
	end
	return ar2
end
and then rotate array

Code: Select all

function rotLeft(ar)
    return vFlip(transpose(ar))
end

function rotRight(ar)
    return hFlip(transpose(ar))
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], pgimeno and 10 guests