about summary refs log tree commit diff stats
path: root/util.lua
blob: a13912caa8692f0407f5303217aa141ed1c08456 (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
38
39
--- lam.util --- utility functions

local m = {}
local string = string
local utf8 = require("utf8")

m.luaerror = error

-- signal an error
-- WHERE is where in the process; DESC is a description of the error; the rest
-- are "irritants"
function m.error (desc, ...)
	local irritants = {}
	for _, i in ipairs({...}) do
		table.insert(irritants, tostring(i))
	end
	m.luaerror(string.format("%s: %s", desc, table.concat(irritants, ", ")
	))
end

-- remove an element from the front of TBL
function m.pop (tbl)
	return table.remove(tbl, 1)
end

function m.tochars (str)
	local cs = {}
	for _, code in utf8.codes(str) do
		table.insert(cs, code)
	end
	return cs
end

function m.constantly (x)
	return function () return x end
end

--------
return m