-
Notifications
You must be signed in to change notification settings - Fork 45
/
iterator.lua
73 lines (63 loc) · 2.5 KB
/
iterator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--- Iterator module
-- @module iterator
require 'stdlib/core'
Iterator = {}
function Iterator.new(list, compact)
fail_if_missing(list, "missing list argument")
local self = {_list = list, _operations = {}, _compact = compact}
for _, method in pairs({"map", "filter", "each"}) do
self[method] = Iterator[method]
end
self.apply = function(self)
local compact = self._compact
local idx = 1
local operations = self._operations
local result = {}
for key, value in pairs(self._list) do
local new_value = value
for _, operation in ipairs(operations) do
local transform = operation.transform
if transform == 1 then
new_value = operation.func(new_value, key, unpack(operation.args))
elseif transform == 2 then
if not operation.func(new_value, key, unpack(operation.args)) then
-- I apologize in advance, lua has no continue stmt
goto continue
end
elseif transform == 3 and operation.func(new_value, key, unpack(operation.args)) then
return result
end
end
if compact then
result[idx] = new_value
idx = idx + 1
else
result[key] = new_value
end
::continue::
end
return result
end
return self
end
function Iterator.map(self, func, ...)
fail_if_missing(type(self) == "table", "Iterator.map is missing reference to itself")
fail_if_missing(func, "Iterator.map is missing a function parameter")
local operations = self._operations
operations[#operations + 1] = { func = func, args = {...}, transform = 1}
return self
end
function Iterator.filter(self, func, ...)
fail_if_missing(type(self) == "table", "Iterator.filter is missing reference to itself")
fail_if_missing(func, "Iterator.filter is missing a function parameter")
local operations = self._operations
operations[#operations + 1] = { func = func, args = {...}, transform = 2}
return self
end
function Iterator.each(self, func, ...)
fail_if_missing(type(self) == "table", "Iterator.each is missing reference to itself")
fail_if_missing(func, "Iterator.each is missing a function parameter")
local operations = self._operations
operations[#operations + 1] = { func = func, args = {...}, transform = 3}
return self
end