dfe10d9d1b8c811623db1cfb4f6436e3e4ead651
1 local utils
= require('utils')
7 function M
._sequential_Q(obj
)
8 return M
._list_Q(obj
) or M
._vector_Q(obj
)
11 function M
._equal_Q(a
,b
)
12 if M
._symbol_Q(a
) and M
._symbol_Q(b
) then
14 elseif M
._sequential_Q(a
) and M
._sequential_Q(b
) then
15 if #a
~= #b
then return false end
16 for i
, v
in ipairs(a
) do
17 if not M
._equal_Q(v
,b
[i
]) then return false end
26 if type(obj
) == "function" then
27 return M
.FunctionRef
:new(obj
)
29 if type(obj
) ~= "table" then return obj
end
33 for k
,v
in pairs(obj
) do
37 -- copy metatable and link to original
38 local old_mt
= getmetatable(obj
)
41 for k
,v
in pairs(old_mt
) do
44 setmetatable(new_mt
, old_mt
)
45 setmetatable(new_obj
, new_mt
)
51 function M
.slice(lst
, start
, last
)
52 if last
== nil then last
= #lst
end
55 for i
= start
, last
do
56 new_lst
[#new_lst
+1] = lst
[i
]
65 function M
.MalException
:new(val
)
66 local newObj
= {val
= val
}
68 return setmetatable(newObj
, self
)
70 function M
._malexception_Q(obj
)
71 return utils
.instanceOf(obj
, M
.MalException
)
75 error(M
.MalException
:new(val
))
81 function NilType
:new(val
)
84 return setmetatable(newObj
, self
)
87 function M
._nil_Q(obj
)
92 function M
._string_Q(obj
)
93 return type(obj
) == "string"
99 function M
.Symbol
:new(val
)
100 local newObj
= {val
= val
}
102 return setmetatable(newObj
, self
)
104 function M
._symbol_Q(obj
)
105 return utils
.instanceOf(obj
, M
.Symbol
)
109 function M
._keyword_Q(obj
)
110 return M
._string_Q(obj
) and "\177" == string.sub(obj
,1,1)
117 function M
.List
:new(lst
)
118 local newObj
= lst
and lst
or {}
120 return setmetatable(newObj
, self
)
122 function M
._list_Q(obj
)
123 return utils
.instanceOf(obj
, M
.List
)
125 function M
.List
:slice(start
,last
)
126 return M
.List
:new(M
.slice(self
,start
,last
))
132 function M
.Vector
:new(lst
)
133 local newObj
= lst
and lst
or {}
135 return setmetatable(newObj
, self
)
137 function M
._vector_Q(obj
)
138 return utils
.instanceOf(obj
, M
.Vector
)
140 function M
.Vector
:slice(start
,last
)
141 return M
.Vector
:new(M
.slice(self
,start
,last
))
147 function M
.HashMap
:new(val
)
148 local newObj
= val
and val
or {}
150 return setmetatable(newObj
, self
)
152 function M
.hash_map(...)
153 return M
._assoc_BANG(M
.HashMap
:new(), unpack(arg
))
155 function M
._hash_map_Q(obj
)
156 return utils
.instanceOf(obj
, M
.HashMap
)
158 function M
._assoc_BANG(hm
, ...)
159 for i
= 1, #arg
, 2 do
160 hm
[arg
[i]]
= arg
[i
+1]
164 function M
._dissoc_BANG(hm
, ...)
174 function M
.MalFunc
:new(fn
, ast
, env
, params
)
175 local newObj
= {fn
= fn
, ast
= ast
, env
= env
,
176 params
= params
, ismacro
= false}
178 return setmetatable(newObj
, self
)
180 function M
._malfunc_Q(obj
)
181 return utils
.instanceOf(obj
, M
.MalFunc
)
187 function M
.Atom
:new(val
)
188 local newObj
= {val
= val
}
190 return setmetatable(newObj
, self
)
192 function M
._atom_Q(obj
)
193 return utils
.instanceOf(obj
, M
.Atom
)
199 function M
.FunctionRef
:new(fn
)
200 local newObj
= {fn
= fn
}
201 return setmetatable(newObj
, self
)
203 function M
._functionref_Q(obj
)
204 return utils
.instanceOf(obj
, M
.FunctionRef
)
206 function M
.FunctionRef
:__call(...)