From 783eec9b4592ff3fa54ffa1a855dda2a71f2db64 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 13 Mar 2019 20:55:24 -0700 Subject: Made iteration easier to work with by using .__inext and .__next for custom iteration, and a custom ipairs() and pairs() to use that. --- containers.lua | 71 +++++++++------------------------------------------------- 1 file changed, 10 insertions(+), 61 deletions(-) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index 8d77919..9f3e12e 100644 --- a/containers.lua +++ b/containers.lua @@ -1,4 +1,4 @@ -local List, Dict, Undict, DictEntries, _undict_mt, _dict_mt, _dict_entries_mt +local List, Dict, Undict, _undict_mt, _dict_mt local insert, remove, concat do local _obj_0 = table @@ -426,62 +426,6 @@ Undict = function(d) end)()) return u end -local _dict_entries_mt -_dict_entries_mt = { - __type = "a Dict's Entries", - __index = function(self, k) - if type(k) == 'number' then - if k == 0 then - return nil - end - if k < 0 then - if k < 0 then - k = #self.dict + k + 1 - end - end - local i, last_k = self._last_i, self._last_k - if k < i then - i, last_k = 0, nil - end - local d = self.dict - for i = i + 1, k do - last_k = next(d, last_k) - if last_k == nil then - return nil - end - end - self._last_i, self._last_k = k, last_k - return Dict({ - key = last_k, - d[last_k] - }) - else - return _dict_entries_mt[k] - end - end, - __len = function(self) - return #self.dict - end, - __eq = function(self, other) - return type(other) == type(self) and getmetatable(other) == getmetatable(self) and other.dict == self.dict - end, - __tostring = function(self) - return "(entries in " .. tostring(self.dict) .. ")" - end, - as_nomsu = function(self) - return "(entries in " .. _dict_mt.as_nomsu(self.dict) .. ")" - end, - as_lua = function(self) - return "entries_in" .. _dict_mt.as_lua(self.dict) - end -} -DictEntries = function(d) - return setmetatable({ - dict = d, - _last_i = 0, - _last_k = nil - }, _dict_entries_mt) -end _dict_mt = { __type = "a Dict", __eq = function(self, other) @@ -540,8 +484,14 @@ _dict_mt = { return _accum_0 end)(), ", ") .. "}" end, - as_iterable = function(self) - return DictEntries(self) + __inext = function(self, key) + local nextkey, value = next(self, key) + if nextkey ~= nil then + return nextkey, Dict({ + key = nextkey, + value = value + }) + end end, __band = function(self, other) return Dict((function() @@ -662,6 +612,5 @@ Dict = function(t, more, ...) end return { List = List, - Dict = Dict, - DictEntries = DictEntries + Dict = Dict } -- cgit v1.2.3