Share your Utils

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Share your Utils

Post by tentus »

Noticed that I'm using this a fair bit nowadays, so I thought I'd share.

Code: Select all

-- returns the next string in a list of strings.
-- "current" is a string. "list" is a table of strings.
function nextInList(current, list)
	for i=1, #list do
		if current == list[i] then
			if i == #list then
				return list[1]
			else
				return list[i + 1]
			end
		end
	end
	return current
end
Kurosuke needs beta testers
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Share your Utils

Post by BlackBulletIV »

May as well dump Grace's data utilities here:

Code: Select all

--[[
    Swaps the current item between a and b. If current
    is equal to a, then b will be returned, and vice
    versa.
    
    -- Parameters --
    current:mixed
    The current value.
    
    a:mixed
    The first possible value.
    
    b:mixed
    The second possible value.
    
    -- Returns --
    mixed
    If current == a, returns b. If current == b, returns a.
--]]
function Grace.swap(current, a, b)
    if current == a then
        return b
    else
        return a
    end
end

--[[
    Randomly chooses one of the parameters given.
    
    -- Parameters --
    ...:mixed
    One or more values to choose from.
    
    -- Returns --
    One of those values.
--]]
function Grace.choose(...)
    return arg[math.random(1, arg.n)]
end

--[[
    Copies (shallow) an object. If the object is a table
    it will create a new table and set each key and value
    from the object to the new table. It will also set the
    metatable to be the same.
    
    -- Parameters --
    object:mixed
    
    -- Returns --
    The copied object.
--]]
function Grace.copy(object)
    if type(object) ~= "table" then
        return object
    end
    
    local new = {}
    for k, v in pairs(object) do
        new[k] = v
    end
    
    return setmetatable(new, getmetatable(object))
end

--[[
    Deeply copies an object. Works the same way as copy() except
    that it recursively copies every key and value. It does the
    same for the metatable.
    
    -- Parameters --
    object:mixed
    
    -- Returns --
    The deeply copied object.
--]]
function Grace.deepCopy(object)
    local lookup = {}
    
    local function copy(obj)
        if type(obj) ~= "table" then
            return obj
        elseif lookup[obj] then
            return lookup[obj]
        end

        local new = {}
        lookup[obj] = new
        for k, v in pairs(obj) do
            new[copy(k)] = copy(v)
        end
        
        return setmetatable(new, copy(getmetatable(obj)))
    end
    
    return copy(object)
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Share your Utils

Post by bartbes »

Some stuff in there is just horrible.

Code: Select all

--[[
function Grace.swap(current, a, b)
    return current == a and b or a
end --ternary ftw

function Grace.choose(...)
    return select(math.random(1, select("#", ...), ...)
    --or at the very least put it in a table yourself and use
    --the # operator, this is lua 5.1
end
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Share your Utils

Post by BlackBulletIV »

Why is not using a ternary statement horrible? Does the same thing, but I do admit the ternary looks nicer.

I don't see what's bad about using arg and arg.n; are they going to be removed from the language or something? It's not like my code is any longer, to the contrary.

Finally, I'm reading the 5.0 Programming in Lua, and converting from ActionScript.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Share your Utils

Post by bartbes »

Yes, arg and especially table.n are deprecated and should not be used.
User avatar
slime
Solid Snayke
Posts: 3147
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Share your Utils

Post by slime »

arg (and arg.n and tbl.n and table.getn and all that stuff) was deprecated 6 years ago. It probably won't work. As bartbes pointed out, a second way to write a more correct version would be something like

Code: Select all

function Grace.choose(...)
	if not ... then return end
	local tbl = {...}
	return tbl[math.random(#tbl)]
end
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Share your Utils

Post by BlackBulletIV »

slime wrote:deprecated 6 years ago.
Oh, well that explains. I'll have to convert some code now, lol.
slime wrote:It probably won't work.
Funny thing is, it actually does work.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Share your Utils

Post by Robin »

BlackBulletIV wrote:
slime wrote:It probably won't work.
Funny thing is, it actually does work.
The subtle difference between "you shouldn't use it" and "you can't use it". ;)
slime wrote:a second way to write a more correct version would be something like

Code: Select all

function Grace.choose(...)
	if not ... then return end
	local tbl = {...}
	return tbl[math.random(#tbl)]
end
The problem is that doesn't work:

Code: Select all

function isempty(...)
    if not ... then
        print "empty"
    else
        print "not empty"
    end
end
isempty() --> empty
isempty(1) --> not empty
isempty(false) --> empty
isempty(false, true) --> empty
So every sequence starting with a false value is considered empty. A way to make it work would be:

Code: Select all

function Grace.choose(...)
	if select('#', ...) == 0 then return end
	local tbl = {...}
	return tbl[math.random(#tbl)]
end
Help us help you: attach a .love.
User avatar
slime
Solid Snayke
Posts: 3147
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Share your Utils

Post by slime »

My bad. I can never get away from the select function! :o
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Share your Utils

Post by Robin »

slime wrote:My bad. I can never get away from the select function! :o
You could, actually, do without select(), I just realised.

Code: Select all

function Grace.choose(...)
   local tbl = {...}
   if #tbl == 0 then return end
   return tbl[math.random(#tbl)]
end
or even:

Code: Select all

function Grace.choose(...)
   local tbl = {...}
   return #tbl > 0 and tbl[math.random(#tbl)] or nil
end
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 9 guests