diff options
-rw-r--r-- | type.lua | 14 | ||||
-rw-r--r-- | util.lua | 14 |
2 files changed, 24 insertions, 4 deletions
diff --git a/type.lua b/type.lua index 3c26188..07f78f1 100644 --- a/type.lua +++ b/type.lua | |||
@@ -97,6 +97,20 @@ function m.cons (a, b) | |||
97 | end | 97 | end |
98 | return "(" .. table.concat(out, " ") .. ")" | 98 | return "(" .. table.concat(out, " ") .. ")" |
99 | end, | 99 | end, |
100 | __len = | ||
101 | function (self) | ||
102 | local function go (lis, acc) | ||
103 | -- improper lists don't have lengths | ||
104 | -- ... but don't error here. | ||
105 | if not m.isa(lis, "cons") then | ||
106 | return nil | ||
107 | end | ||
108 | if lis[2] == m.null then return acc | ||
109 | else return go(lis[2], acc+1) | ||
110 | end | ||
111 | end | ||
112 | return go(self, 1) | ||
113 | end, | ||
100 | } | 114 | } |
101 | return setmetatable(t, mt) | 115 | return setmetatable(t, mt) |
102 | end | 116 | end |
diff --git a/util.lua b/util.lua index 938848c..b5a57b1 100644 --- a/util.lua +++ b/util.lua | |||
@@ -1,11 +1,17 @@ | |||
1 | --- lam.util | 1 | --- lam.util |
2 | 2 | ||
3 | local util = {} | 3 | local m = {} |
4 | local unpack = table.unpack or unpack | ||
5 | 4 | ||
6 | function util.pop (tbl) | 5 | function m.pop (tbl) |
6 | --[[ Remove the first element from TBL. ]] | ||
7 | return table.remove(tbl, 1) | 7 | return table.remove(tbl, 1) |
8 | end | 8 | end |
9 | 9 | ||
10 | function m.arity (r, min, max) | ||
11 | --[[ Return whether R is within MIN and MAX (inclusive). ]] | ||
12 | local len = #r | ||
13 | return len >= min and len <= max | ||
14 | end | ||
15 | |||
10 | --- | 16 | --- |
11 | return util | 17 | return m |