sorcery  Artifact [5046584198]

Artifact 5046584198945d75a5245506564d69541ab722a578dff92c55baf5bc69963d63:

  • File lib/tbl.lua — part of check-in [72eebac4bc] at 2020-09-26 18:49:51 on branch trunk — add writing stand for editing codexes; add scissors, ink, erasure fluid, pens; touch up codex UI; add many recipe notes; add craft divination type for crafttools; defuckulate fucktarded crafttool impl; enhance table library with missing features like lua's table.unpack; many bug fixes and enhancements; blood for the blood god (user: lexi, size: 1805) [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.merge = function(base,override)
	local new = fn.copy(base)
	for k,v in pairs(override) do
		new[k] = v
	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

return fn