diff options
author | Case Duckworth | 2024-04-09 21:04:17 -0500 |
---|---|---|
committer | Case Duckworth | 2024-04-09 21:04:29 -0500 |
commit | 8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e (patch) | |
tree | 124ef31663ed570bed358dffd9c861d10fabce7b /util.lua | |
parent | Uh (diff) | |
download | lam-8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e.tar.gz lam-8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e.zip |
Reorganization
Diffstat (limited to 'util.lua')
-rw-r--r-- | util.lua | 35 |
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 | ||
3 | local m = {} | 3 | local m = {} |
4 | local string = string | ||
5 | local utf8 = require("utf8") | ||
4 | 6 | ||
7 | m.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" | ||
12 | function m.error (desc, ...) | ||
13 | m.luaerror(string.format("%s: %s", desc, table.concat({...}, " ") | ||
14 | )) | ||
15 | end | ||
16 | |||
17 | -- remove an element from the front of TBL | ||
5 | function m.pop (tbl) | 18 | function m.pop (tbl) |
6 | --[[ Remove the first element from TBL. ]] | ||
7 | return table.remove(tbl, 1) | 19 | return table.remove(tbl, 1) |
8 | end | 20 | end |
9 | 21 | ||
10 | function m.assert_arity (r, min, max) | 22 | function 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 | ||
28 | end | ||
29 | |||
30 | function m.constantly (x) | ||
31 | return function () return x end | ||
19 | end | 32 | end |
20 | 33 | ||
21 | --- | 34 | -------- |
22 | return m | 35 | return m |