Procházet zdrojové kódy

Observable.average;

bjorn před 9 roky
rodič
revize
5c8a0c349c
4 změnil soubory, kde provedl 75 přidání a 0 odebrání
  1. 7 0
      doc/README.md
  2. 27 0
      rx.lua
  3. 27 0
      src/observable.lua
  4. 14 0
      tests/average.lua

+ 7 - 0
doc/README.md

@@ -22,6 +22,7 @@ RxLua
   - [dump](#dumpname-formatter)
   - [dump](#dumpname-formatter)
   - [all](#allpredicate)
   - [all](#allpredicate)
   - [amb](#ambobservables)
   - [amb](#ambobservables)
+  - [average](#average)
   - [combineLatest](#combinelatestobservables-combinator)
   - [combineLatest](#combinelatestobservables-combinator)
   - [compact](#compact)
   - [compact](#compact)
   - [concat](#concatsources)
   - [concat](#concatsources)
@@ -257,6 +258,12 @@ Given a set of Observables, produces values from only the first one to produce a
 
 
 ---
 ---
 
 
+#### `:average()`
+
+Returns an Observable that produces the average of all values produced by the original.
+
+---
+
 #### `:combineLatest(observables, combinator)`
 #### `:combineLatest(observables, combinator)`
 
 
 Returns a new Observable that runs a combinator function on the most recent values from a set of Observables whenever any of them produce a new value. The results of the combinator function are produced by the new Observable.
 Returns a new Observable that runs a combinator function on the most recent values from a set of Observables whenever any of them produce a new value. The results of the combinator function are produced by the new Observable.

+ 27 - 0
rx.lua

@@ -296,6 +296,33 @@ function Observable.amb(a, b, ...)
   end):amb(...)
   end):amb(...)
 end
 end
 
 
+--- 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
+
 --- Returns a new Observable that runs a combinator function on the most recent values from a set
 --- Returns a new Observable that runs a combinator function on the most recent values from a set
 -- of Observables whenever any of them produce a new value. The results of the combinator function
 -- of Observables whenever any of them produce a new value. The results of the combinator function
 -- are produced by the new Observable.
 -- are produced by the new Observable.

+ 27 - 0
src/observable.lua

@@ -211,6 +211,33 @@ function Observable.amb(a, b, ...)
   end):amb(...)
   end):amb(...)
 end
 end
 
 
+--- 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
+
 --- Returns a new Observable that runs a combinator function on the most recent values from a set
 --- Returns a new Observable that runs a combinator function on the most recent values from a set
 -- of Observables whenever any of them produce a new value. The results of the combinator function
 -- of Observables whenever any of them produce a new value. The results of the combinator function
 -- are produced by the new Observable.
 -- are produced by the new Observable.

+ 14 - 0
tests/average.lua

@@ -0,0 +1,14 @@
+describe('average', function()
+  it('errors when its parent errors', function()
+    expect(Rx.Observable.throw():average().subscribe).to.fail()
+  end)
+
+  it('produces a single value representing the average of the values produced by the source', function()
+    expect(Rx.Observable.fromRange(3, 9, 2):average()).to.produce(6)
+    expect(Rx.Observable.fromTable({-1, 0, 1}):average()).to.produce(0)
+  end)
+
+  it('produces nothing if there are no values to average', function()
+    expect(Rx.Observable.empty():average()).to.produce({})
+  end)
+end)