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: Somewhere in Brazil
Contact:

Re: Small Useful Functions

Post by HugoBDesigner » Mon Oct 06, 2014 8:10 pm

A good way of doing the transpose function (and also copy most tables (except for tables that contain themselves)) is by the table.copy function I made a while ago:

Code: Select all

function table.copy(t, s) --s for tables inside table
   local s = s or false
   if not t then
      return {}
   end
   local returner = {}
   for i, v in pairs(t) do
      if s and type(v) == "table" then
         returner[i] = table.copy(v, true)
      else
         returner[i] = v
      end
   end
   return returner
end
HugoBDesigner - my Twitter
HugoBDesigner - my Blog

User avatar
Sapper
Prole
Posts: 15
Joined: Sun Oct 05, 2014 12:01 am

Re: Small Useful Functions

Post by Sapper » Mon Oct 06, 2014 8:18 pm

HugoBDesigner wrote:A good way of doing the transpose function (and also copy most tables (except for tables that contain themselves)) is by the table.copy function I made a while ago:

Code: Select all

function table.copy(t, s) --s for tables inside table
   local s = s or false
   if not t then
      return {}
   end
   local returner = {}
   for i, v in pairs(t) do
      if s and type(v) == "table" then
         returner[i] = table.copy(v, true)
      else
         returner[i] = v
      end
   end
   return returner
end
that reminds me, simple serialization function. only handles strings, numbers, booleans and nested tables.

Code: Select all

function pack(tab)
	local str = "{" 
	local subpack = function(k,v) 
		return "".."["..((type(k) =="number"and k)
                or("'"..k.."'")).."]".."="..v.."," 
	end
	for k,v in pairs(tab) do 
		local t=type(v)
		if t=="table" then str=str.."\n \t"..subpack(k,pack(v))
		elseif t=="boolean" then str=str..subpack(k,tostring(v))
		elseif t=="string" then str=str..subpack(k,"'"..v.."'")
		else str=str..subpack(k,v)
		end
	end
	str = str.."}"
	return str
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 » Mon Oct 06, 2014 9:18 pm

If you're doing serialization, you're better off with Smallfolk (if you need to send it over an unsafe channel, like the internet), Lady (if you want to make savegames without hassle) or Ser (for everything else).
Help us help you: attach a .love.

User avatar
Sapper
Prole
Posts: 15
Joined: Sun Oct 05, 2014 12:01 am

Re: Small Useful Functions

Post by Sapper » Tue Oct 07, 2014 7:50 pm

Robin wrote:If you're doing serialization, you're better off with Smallfolk (if you need to send it over an unsafe channel, like the internet), Lady (if you want to make savegames without hassle) or Ser (for everything else).
yeah but those are full libs, not small usefull functions ;)

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Small Useful Functions

Post by bartbes » Wed Oct 08, 2014 8:19 am

Sometimes the right tool for the job isn't a "small useful function" (one could argue about the usefulness of an incomplete serialization solution).

User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Small Useful Functions

Post by Positive07 » Tue Oct 14, 2014 2:28 am

Expanding on this one

Code: Select all

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

Code: Select all

local function requirei(d, ...)
   local ret = {}
   for i, v in ipairs{...} do
      ret[i] = require(d .. v)
   end   
   return unpack(ret)
end
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

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 » Tue Oct 14, 2014 10:30 am

Just for fun:

Code: Select all

local function requirei(d, ...)
	if ... == nil then
		return
	end
	return require(d .. ...), requirei(d, select(2, ...))
end
Help us help you: attach a .love.

User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Small Useful Functions

Post by Positive07 » Tue Oct 14, 2014 12:34 pm

Why would it be better... it does the same and doesnt look more readable... is there any benefit?
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Small Useful Functions

Post by undef » Tue Oct 14, 2014 2:07 pm

Positive07 wrote:Why would it be better... it does the same and doesnt look more readable... is there any benefit?
Well it does not really do the same on a machine level, but let's face it - mass requiring of files is hardly something that needs to be optimized.
(If you're interested what happens below, I recommend using the command "luac -l" on a file containing the function. This can be very helpful if you need to optimize hard loops as well )

But as Robin already said, it's just for fun. And I think it's a quite cool way to describe that function.
(And it reminds us that there is the select function which can be quite useful at times)
I couldn't resist to change it a bit though^^

Code: Select all

local function requirei(d, ...)
    if ... then
        return require(d .. ...), requirei(d, select(2, ...))
    end
end
twitter | steam | indieDB

Check out quadrant on Steam!

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 » Tue Oct 14, 2014 4:11 pm

Yeah, it's just fun and games (hopefully no-one will lose an eye). ;)

Also: ooh, that one is better. Here's one without select:

Code: Select all

local function requirei(d, m, ...)
    if m then
        return require(d .. m), requirei(d, ...)
    end
end
Help us help you: attach a .love.

Post Reply

Who is online

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