-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformers.lua
58 lines (52 loc) · 1.08 KB
/
transformers.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
-- a transformer protocol must have:
-- init (no arity)
-- step (two arity)
-- complete (one arity)
local append = (function()
return {
init = function() return {} end,
step = function(tbl, result)
table.insert(tbl, result)
return tbl
end,
complete = function(result) return result end
}
end)()
local concat = (function()
return {
init = function() return '' end,
step = function(str, ch)
str = str .. ch
return str
end,
complete = function(result) return result end
}
end)()
local sum = (function()
local accum = 0
return {
init = function() return 0 end,
step = function(tbl, result)
accum = accum + result
return tbl
end,
complete = function(result) return accum end
}
end)()
local mult = (function()
local accum = 1
return {
init = function() return 0 end,
step = function(tbl, result)
accum = accum * result
return tbl
end,
complete = function(result) return accum end
}
end)()
return {
sum = sum,
mult = mult,
concat = concat,
append = append,
}