-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.lua
More file actions
39 lines (33 loc) · 1.05 KB
/
enum.lua
File metadata and controls
39 lines (33 loc) · 1.05 KB
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
local Enum = {}
Enum.__index = Enum
function Enum.count(enumerable)
Enumerable.is_defined_for(enumerable)
local reducer = function(x, acc)
return "cont", acc + 1
end
local result, count = enumerable:reduce(reducer, "cont", 0)
return count
end
function Enum.foldl(enumerable, acc, fun)
Enumerable.is_defined_for(enumerable)
local reducer = function(x, acc)
return "cont", fun(x, acc)
end
local result, value = enumerable:reduce(reducer, "cont", acc)
print(result, value)
return value
end
local into = function(enumerable, initial, fun, callback)
local value = Enum.foldl(enumerable, initial, callback)
return fun(value, "done")
end
function Enum.into(enumerable, collectable, fun)
Enumerable.is_defined_for(enumerable)
Collectable.is_defined_for(collectable)
local initial, collectable_fun = collectable:into()
local callback = function(x, acc)
return collectable_fun(acc, "cont", fun(x))
end
return into(enumerable, initial, collectable_fun, callback)
end
return Enum