소스 검색

Observable.sum;

bjorn 9 년 전
부모
커밋
03078db764
6개의 변경된 파일33개의 추가작업 그리고 0개의 파일을 삭제
  1. 7 0
      doc/README.md
  2. 7 0
      rx.lua
  3. 8 0
      src/operators/sum.lua
  4. 1 0
      tests/observable.lua
  5. 9 0
      tests/sum.lua
  6. 1 0
      tools/concat.lua

+ 7 - 0
doc/README.md

@@ -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.

+ 7 - 0
rx.lua

@@ -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}

+ 8 - 0
src/operators/sum.lua

@@ -0,0 +1,8 @@
+local Observable = require 'observable'
+
+--- 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

+ 1 - 0
tests/observable.lua

@@ -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')

+ 9 - 0
tests/sum.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)
+end)

+ 1 - 0
tools/concat.lua

@@ -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',