-
Notifications
You must be signed in to change notification settings - Fork 0
/
koral_reducer.lua
107 lines (85 loc) · 1.88 KB
/
koral_reducer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
-- max object for reducing a list of numbers to a single number
-- max object properties
-- luacheck: globals inlets outlets outlet
this.inlets = 1
local _mode = 'bypass' -- default
-- max method
---@diagnostic disable-next-line: lowercase-global
function mode(v)
_mode = v
end
local _processors = {}
------------- bypass
_processors['bypass'] = function(input)
return input
end
------------- pick
local function _pick(input, tester)
local picked = input[1]
for i = 1, #input do
if tester(input[i], picked) then
picked = input[i]
end
end
return picked
end
_processors['max_abs'] = function(input)
return _pick(input,
function(a, b) return math.abs(a) > math.abs(b) end
)
end
_processors['abs_max_abs'] = function(input)
return math.abs(_processors['max_abs'](input) )
end
_processors['min_abs'] = function(input)
return _pick(input,
function(a, b) return math.abs(a) < math.abs(b) end
)
end
_processors['abs_min_abs'] = function(input)
return math.abs(_processors['min_abs'](input) )
end
_processors['max'] = function (input)
return _pick(input,
function (a, b) return a > b end
)
end
_processors['min'] = function(input)
return _pick(input,
function (a, b) return a < b end
)
end
------------- accumulate
_processors['mean'] = function(input)
local mean = 0
for i = 1, #input do
mean = mean + input[i]
end
mean = mean / #input
return mean
end
_processors['norm'] = function(input)
local norm = 0
for i = 1, #input do
norm = norm + input[i] ^ 2
end
norm = math.sqrt(norm)
return norm
end
------------- process
local function _process(input)
local processor = _processors[_mode]
if processor then
outlet(0, processor(input) )
end
end
-- max method
---@diagnostic disable-next-line: lowercase-global
function list(...)
_process({...})
end
-- max method
---@diagnostic disable-next-line: lowercase-global
function float(v)
_process({v})
end