You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
711 B
Lua
29 lines
711 B
Lua
local Observable = require "rx.observable"
|
|
|
|
--- Returns an Observable that produces the average of all values produced by the original.
|
|
-- @returns {Observable}
|
|
function Observable:average()
|
|
return Observable.create(function(observer)
|
|
local sum, count = 0, 0
|
|
|
|
local function onNext(value)
|
|
sum = sum + value
|
|
count = count + 1
|
|
end
|
|
|
|
local function onError(e)
|
|
observer:onError(e)
|
|
end
|
|
|
|
local function onCompleted()
|
|
if count > 0 then
|
|
observer:onNext(sum / count)
|
|
end
|
|
|
|
observer:onCompleted()
|
|
end
|
|
|
|
return self:subscribe(onNext, onError, onCompleted)
|
|
end)
|
|
end
|