@@ -50,6 +50,7 @@ RxLua
- [skip](#skipn)
- [skipUntil](#skipuntilother)
- [skipWhile](#skipwhilepredicate)
+ - [sum](#sum)
- [take](#taken)
- [takeUntil](#takeuntilother)
- [takeWhile](#takewhilepredicate)
@@ -507,6 +508,12 @@ Returns a new Observable that skips elements until the predicate returns falsy f
---
+#### `:sum()`
+
+Returns an Observable that produces a single value representing the sum of the values produced by the original.
+---
#### `:take(n)`
Returns a new Observable that only produces the first n results of the original.
@@ -1018,6 +1018,13 @@ function Observable:skipWhile(predicate)
end)
end
+--- Returns an Observable that produces a single value representing the sum of the values produced
+-- by the original.
+-- @returns {Observable}
+function Observable:sum()
+ return self:reduce(function(x, y) return x + y end, 0)
+end
--- Returns a new Observable that only produces the first n results of the original.
-- @arg {number=1} n - The number of elements to produce before completing.
-- @returns {Observable}
@@ -0,0 +1,8 @@
+local Observable = require 'observable'
@@ -207,6 +207,7 @@ describe('Observable', function()
dofile('tests/skip.lua')
dofile('tests/skipUntil.lua')
dofile('tests/skipWhile.lua')
+ dofile('tests/sum.lua')
dofile('tests/take.lua')
dofile('tests/takeUntil.lua')
dofile('tests/takeWhile.lua')
@@ -0,0 +1,9 @@
+describe('sum', function()
+ it('passes through errors from the source', function()
+ expect(Rx.Observable.throw():sum().subscribe).to.fail()
+ end)
+ it('produces the sum of the numeric values from the source', function()
+ expect(Rx.Observable.fromRange(3):sum()).to.produce(6)
+end)
@@ -36,6 +36,7 @@ local files = {
'src/operators/skip.lua',
'src/operators/skipUntil.lua',
'src/operators/skipWhile.lua',
+ 'src/operators/sum.lua',
'src/operators/take.lua',
'src/operators/takeUntil.lua',
'src/operators/takeWhile.lua',