about summary refs log tree commit diff stats
path: root/type.lua
diff options
context:
space:
mode:
authorCase Duckworth2024-04-01 22:16:50 -0500
committerCase Duckworth2024-04-01 22:16:50 -0500
commitf28763f3afcd2c51e9b69318e3749b6bb0a4f1ea (patch)
treeed4ab2f55fa3854cd33f1954b9faf02fda507b25 /type.lua
parentError on wrong unquote[-splicing] (diff)
downloadlam-f28763f3afcd2c51e9b69318e3749b6bb0a4f1ea.tar.gz
lam-f28763f3afcd2c51e9b69318e3749b6bb0a4f1ea.zip
Implement list length
This lays groundwork for arity checks.
Diffstat (limited to 'type.lua')
-rw-r--r--type.lua14
1 files changed, 14 insertions, 0 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