sorcery  Artifact [4e80a60df8]

Artifact 4e80a60df8e0ea0097086ead0f2acb5310f1f77c82a39c65d893cef1f1f95eb6:

  • File lib/tbl.lua — part of check-in [3f6a913e4e] at 2020-09-29 12:40:28 on branch trunk — * remove former hacky registration system, replace with consistent and flexible API; rewrite metal/gem generation to take advantage of this new API; tweaks to init system to enable world-local tweaks to lore and sorcery behavior * initial documentation commit * initial steps towards calendar - add default date format, astrolabe; prepare infra for division/melding/transmutation spells, various tweaks and fixes (user: lexi, size: 2543) [annotate] [blame] [check-ins using]

local fn = {}

fn.shuffle = function(list)
	for i = #list, 2, -1 do
		local j = math.random(i)
		list[i], list[j] = list[j], list[i]
	end
end

fn.scramble = function(list)
	local new = table.copy(list)
	fn.shuffle(new)
	return new
end

fn.copy = function(t)
	local new = {}
	for i,v in pairs(t) do new[i] = v end
	setmetatable(new,getmetatable(t))
	return new
end

fn.deepcopy = table.copy or function(t)
	new = {}
	for k,v in pairs(t) do
		if type(v) == 'table' then
			new[k] = fn.deepcopy(v)
		else
			new[k] = v
		end
	end
	return new
end

fn.merge = function(base,override)
	local new = fn.copy(base)
	for k,v in pairs(override) do
		new[k] = v
	end
	return new
end

fn.deepmerge = function(base,override)
	local new = {}
	local keys = fn.merge(fn.keys(base),fn.keys(override))
	for _,k in pairs(keys) do
		if type(base[k]) == 'table' and
		   type(override[k]) == 'table' then
			new[k] = fn.deepmerge(base[k], override[k])
		elseif override[k] then
			new[k] = override[k]
		else
			new[k] = base[k]
		end
	end
	return new
end

fn.append = function(r1, r2)
	local new = fn.copy(r1)
	for i=1,#r2 do
		new[#new + 1] = r2[i]
	end
	return new
end

fn.capitalize = function(str)
	return string.upper(string.sub(str, 1,1)) .. string.sub(str, 2)
end

fn.has = function(tbl,value)
	for k,v in pairs(tbl) do
		if value == v then return true, k end
	end
	return false, nil
end

fn.keys = function(lst)
	local ks = {}
	for k,_ in pairs(lst) do
		ks[#ks + 1] = k
	end
	return ks
end

fn.pick = function(lst)
	local keys = fn.keys(lst)
	return keys[math.random(#keys)]
end

fn.unpack = function(tbl,i)
	if i and #tbl == i then return tbl[i] end
	i = i or 1
	return tbl[i], fn.unpack(tbl, i+1)
end

fn.each = function(tbl,fn)
	local r = {}
	for k,v in pairs(tbl) do
		r[#r+1] = fn(v,k)
	end
	return r
end

fn.each_o = function(tbl,fn)
	local keys = fn.keys(tbl)
	table.sort(keys)
	return fn.each(keys, function(k,i)
		return fn(tbl[k],k,i)
	end)
end

fn.iter = function(tbl,fn)
	for i=1,#tbl do
		fn(tbl[i], i)
	end
end

fn.map = function(tbl,fn)
	local new = {}
	for k,v in pairs(tbl) do
		local nv, nk = fn(v, k)
		new[nk or k] = nv
	end
	return new
end

fn.fold = function(tbl,fn,acc)
	if #tbl == 0 then
		fn.each_o(tbl, function(v)
			acc = fn(acc, v, k)
		end)
	else
		for i=0,#tbl do
			acc = fn(acc,tbl[i],i)
		end
	end
	return acc
end

fn.walk = function(tbl,path)
	if type(path) == 'table' then
		for _,p in pairs(path) do
			if tbl[p] == nil then return nil end
			tbl = tbl[p]
		end
	else
		tbl = tbl[path]
	end
	return tbl
end

return fn