about summary refs log tree commit diff stats
path: root/util.lua
blob: d151858a85ed80619c1e8abdabf67e4c950191da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
--- lam.util

local m = {}

function m.pop (tbl)
	--[[ Remove the first element from TBL. ]]
	return table.remove(tbl, 1)
end

function m.proc (arity, fn)
	--[[ Wrap RN in a check that for its ARITY.
		ARITY can be a number, the minimum number of arguments,
		or a table {MIN, MAX}.  If MIN is nil or absent, it's 0;
		if MAX is nil or absent, it's infinity.  MIN and MAX are
		both inclusive.
	]]
	local rmin, rmax, rstr
	if type(arity) ~= "table" then
		rmin, rmax = arity, arity
		rstr = rmin
	else
		rmin, rmax = arity[1] or 0, arity[2] or 1/0 -- infinity
		rstr = rmin .. ".." .. rmax
	end
	return function (r)
		local rlen = r and #r or 0
		if rlen < rmin or rlen > rmax then
			error(string.format("Wrong arity: %s, need %s",
					rlen,
					rstr))
		end
		return fn(r)
	end
end

---
return m