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.
39 lines
1.4 KiB
Lua
39 lines
1.4 KiB
Lua
local Observable = require "rx.observable"
|
|
local util = require "rx.util"
|
|
|
|
--- Returns a new Observable that produces a single value computed by accumulating the results of
|
|
-- running a function on each value produced by the original Observable.
|
|
-- @arg {function} accumulator - Accumulates the values of the original Observable. Will be passed
|
|
-- the return value of the last call as the first argument and the
|
|
-- current values as the rest of the arguments.
|
|
-- @arg {*} seed - A value to pass to the accumulator the first time it is run.
|
|
-- @returns {Observable}
|
|
function Observable:reduce(accumulator, seed)
|
|
return Observable.create(function(observer)
|
|
local result = seed
|
|
local first = true
|
|
|
|
local function onNext(...)
|
|
if first and seed == nil then
|
|
result = ...
|
|
first = false
|
|
else
|
|
return util.tryWithObserver(observer, function(...)
|
|
result = accumulator(result, ...)
|
|
end, ...)
|
|
end
|
|
end
|
|
|
|
local function onError(e)
|
|
return observer:onError(e)
|
|
end
|
|
|
|
local function onCompleted()
|
|
observer:onNext(result)
|
|
return observer:onCompleted()
|
|
end
|
|
|
|
return self:subscribe(onNext, onError, onCompleted)
|
|
end)
|
|
end
|