Page 7 of 13

Re: Small Useful Functions

Posted: Mon Oct 06, 2014 8:10 pm
by HugoBDesigner
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

Re: Small Useful Functions

Posted: Mon Oct 06, 2014 8:18 pm
by Sapper
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

Re: Small Useful Functions

Posted: Mon Oct 06, 2014 9:18 pm
by Robin
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).

Re: Small Useful Functions

Posted: Tue Oct 07, 2014 7:50 pm
by Sapper
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 ;)

Re: Small Useful Functions

Posted: Wed Oct 08, 2014 8:19 am
by bartbes
Sometimes the right tool for the job isn't a "small useful function" (one could argue about the usefulness of an incomplete serialization solution).

Re: Small Useful Functions

Posted: Tue Oct 14, 2014 2:28 am
by Positive07
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

Re: Small Useful Functions

Posted: Tue Oct 14, 2014 10:30 am
by Robin
Just for fun:

Code: Select all

local function requirei(d, ...)
	if ... == nil then
		return
	end
	return require(d .. ...), requirei(d, select(2, ...))
end

Re: Small Useful Functions

Posted: Tue Oct 14, 2014 12:34 pm
by Positive07
Why would it be better... it does the same and doesnt look more readable... is there any benefit?

Re: Small Useful Functions

Posted: Tue Oct 14, 2014 2:07 pm
by undef
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

Re: Small Useful Functions

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