-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.lua
More file actions
75 lines (66 loc) · 1.8 KB
/
set.lua
File metadata and controls
75 lines (66 loc) · 1.8 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
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
74
75
local Enumerable = require 'enumerable'
local Collectable = require 'collectable'
local Iterator = require 'iterator'
local Set = {}
Set.__index = Set
setmetatable(Set, { __call = function(_, ...) return Set.new(...) end })
function Set.new()
local self = setmetatable({}, Set)
return self
end
function Set.insert(set, val)
assert(set)
assert(val)
set[val] = true
return set
end
function Set.remove(set, val)
assert(set)
assert(val)
set[val] = nil
return set
end
function Set.iterator(set)
assert(set)
return Iterator.new(pairs(set))
end
-- Collectable Protocol
--- IMPURE function
local collector = function(collection, cmd, value)
if cmd == "cont" then
return Set.insert(collection, value)
elseif cmd == "done" then
return collection
elseif cmd == "halt" then
return collection
end
end
function Set.into() Set.into(Set.new()) end
function Set.into(initial) return initial, collector end
-- Enumerable Protocol
function Set.reduce(set, reducer, cmd, acc)
return Set.do_reduce(Set.iterator(set), reducer, cmd, acc)
end
function Set.do_reduce(iter, reducer, cmd, acc)
if cmd == "halt" then
return "halted", acc
elseif cmd == "suspend" then
local fun = function(cmd, acc)
Set.do_reduce(iter, reducer, cmd, acc)
end
return "suspended", acc, fun
elseif cmd == "cont" then
local val = Iterator._next(iter)
if val[2] == nil then
return "done", acc
else
-- val[2] is the value, we want the index
cmd, acc = reducer(val[1], acc)
return Set.do_reduce(iter, reducer, cmd, acc)
end
end
end
-- TODO make this data driven by taking the protocol impl as an arg
Enumerable:_impl(Set)
Collectable:_impl(Set)
return Set