diff options
Diffstat (limited to 'util.lua')
-rw-r--r-- | util.lua | 31 |
1 files changed, 8 insertions, 23 deletions
diff --git a/util.lua b/util.lua index d151858..8fedbf7 100644 --- a/util.lua +++ b/util.lua | |||
@@ -7,29 +7,14 @@ function m.pop (tbl) | |||
7 | return table.remove(tbl, 1) | 7 | return table.remove(tbl, 1) |
8 | end | 8 | end |
9 | 9 | ||
10 | function m.proc (arity, fn) | 10 | function m.assert_arity (r, min, max) |
11 | --[[ Wrap RN in a check that for its ARITY. | 11 | local rmin = min or 0 |
12 | ARITY can be a number, the minimum number of arguments, | 12 | local rmax = max or 1/0 -- infinity |
13 | or a table {MIN, MAX}. If MIN is nil or absent, it's 0; | 13 | local rlen = #r |
14 | if MAX is nil or absent, it's infinity. MIN and MAX are | 14 | if rlen < rmin or rlen > rmax then |
15 | both inclusive. | 15 | error(string.format("Wrong arity: %s; expecting %s", |
16 | ]] | 16 | rlen, |
17 | local rmin, rmax, rstr | 17 | rmin == rmax and rmin or (rmin..".."..rmax))) |
18 | if type(arity) ~= "table" then | ||
19 | rmin, rmax = arity, arity | ||
20 | rstr = rmin | ||
21 | else | ||
22 | rmin, rmax = arity[1] or 0, arity[2] or 1/0 -- infinity | ||
23 | rstr = rmin .. ".." .. rmax | ||
24 | end | ||
25 | return function (r) | ||
26 | local rlen = r and #r or 0 | ||
27 | if rlen < rmin or rlen > rmax then | ||
28 | error(string.format("Wrong arity: %s, need %s", | ||
29 | rlen, | ||
30 | rstr)) | ||
31 | end | ||
32 | return fn(r) | ||
33 | end | 18 | end |
34 | end | 19 | end |
35 | 20 | ||