about summary refs log tree commit diff stats
path: root/util.lua
diff options
context:
space:
mode:
authorCase Duckworth2024-04-09 21:04:17 -0500
committerCase Duckworth2024-04-09 21:04:29 -0500
commit8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e (patch)
tree124ef31663ed570bed358dffd9c861d10fabce7b /util.lua
parentUh (diff)
downloadlam-8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e.tar.gz
lam-8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e.zip
Reorganization
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua35
1 files changed, 24 insertions, 11 deletions
diff --git a/util.lua b/util.lua index 8fedbf7..10460a2 100644 --- a/util.lua +++ b/util.lua
@@ -1,22 +1,35 @@
1--- lam.util 1--- lam.util --- utility functions
2 2
3local m = {} 3local m = {}
4local string = string
5local utf8 = require("utf8")
4 6
7m.luaerror = error
8
9-- signal an error
10-- WHERE is where in the process; DESC is a description of the error; the rest
11-- are "irritants"
12function m.error (desc, ...)
13 m.luaerror(string.format("%s: %s", desc, table.concat({...}, " ")
14 ))
15end
16
17-- remove an element from the front of TBL
5function m.pop (tbl) 18function m.pop (tbl)
6 --[[ Remove the first element from TBL. ]]
7 return table.remove(tbl, 1) 19 return table.remove(tbl, 1)
8end 20end
9 21
10function m.assert_arity (r, min, max) 22function m.tochars (str)
11 local rmin = min or 0 23 local cs = {}
12 local rmax = max or 1/0 -- infinity 24 for _, code in utf8.codes(str) do
13 local rlen = #r 25 table.insert(cs, code)
14 if rlen < rmin or rlen > rmax then
15 error(string.format("Wrong arity: %s; expecting %s",
16 rlen,
17 rmin == rmax and rmin or (rmin..".."..rmax)))
18 end 26 end
27 return cs
28end
29
30function m.constantly (x)
31 return function () return x end
19end 32end
20 33
21--- 34--------
22return m 35return m