Skip to content

Commit

Permalink
Added Iterable:pick method
Browse files Browse the repository at this point in the history
  • Loading branch information
SinisterRectus committed Mar 3, 2019
1 parent 42ef617 commit 160507d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions libs/iterables/Iterable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,32 @@ function Iterable:select(...)
return rows
end

--[=[
@m pick
@p ... string/function
@r function
@d This returns an iterator that, when called, returns the values from each
encountered object, picked by the provided keys. If a key is a string, the objects
are indexed with the string. If a key is a function, the function is called with
the object passed as its first argument.
]=]
function Iterable:pick(...)
local keys = pack(...)
local values = {}
local n = keys.n
return wrap(function()
for obj in self:iter() do
for i = 1, n do
local k = keys[i]
if type(k) == 'function' then
values[i] = k(obj)
else
values[i] = obj[k]
end
end
yield(unpack(values, 1, n))
end
end)
end

return Iterable

0 comments on commit 160507d

Please sign in to comment.