about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCase Duckworth2024-04-01 22:16:50 -0500
committerCase Duckworth2024-04-01 22:16:50 -0500
commitf28763f3afcd2c51e9b69318e3749b6bb0a4f1ea (patch)
treeed4ab2f55fa3854cd33f1954b9faf02fda507b25
parentError on wrong unquote[-splicing] (diff)
downloadlam-f28763f3afcd2c51e9b69318e3749b6bb0a4f1ea.tar.gz
lam-f28763f3afcd2c51e9b69318e3749b6bb0a4f1ea.zip
Implement list length
This lays groundwork for arity checks.
-rw-r--r--type.lua14
-rw-r--r--util.lua14
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)
102end 116end
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
3local util = {} 3local m = {}
4local unpack = table.unpack or unpack
5 4
6function util.pop (tbl) 5function m.pop (tbl)
6 --[[ Remove the first element from TBL. ]]
7 return table.remove(tbl, 1) 7 return table.remove(tbl, 1)
8end 8end
9 9
10function 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
14end
15
10--- 16---
11return util 17return m