Skip to content

Commit

Permalink
Added __pairs method to Iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
SinisterRectus committed Sep 19, 2017
1 parent 9ca0b75 commit 43f3830
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions libs/iterables/Cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function Cache:__init(array, constructor, parent)
self._parent = parent
end

function Cache:__pairs()
return next, self._objects
end

function Cache:__len()
return self._count
end
Expand Down
12 changes: 10 additions & 2 deletions libs/iterables/Iterable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ general purpose data structure with features that are better suited for an
object-oriented environment.
Note: All sub-classes must implement their own `__init` and `iter` methods.
Additionally, more efficient versions of `__len` and `get` methods can be
redefined in sub-classes.
Additionally, more efficient versions of `__len`, `__pairs`, and `get` methods
can be redefined in sub-classes.
]]

function Iterable:__pairs()
return wrap(function()
for obj in self:iter() do
yield(obj:__hash(), obj)
end
end)
end

function Iterable:__len()
local n = 0
for _ in self:iter() do
Expand Down
4 changes: 4 additions & 0 deletions libs/iterables/SecondaryCache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function SecondaryCache:__init(array, primary)
self._primary = primary
end

function SecondaryCache:__pairs()
return next, self._objects
end

function SecondaryCache:__len()
return self._count
end
Expand Down

0 comments on commit 43f3830

Please sign in to comment.