#!markdown # Simple Moving Average and `IAsyncEnumerable` Khalid said:
I decided to look into it! First step, we have the existing Simple Moving Average calculator from [my post](https://andrewlock.net/creating-a-simple-moving-average-calculator-in-csharp-1-a-simple-moving-average-calculator/). #!csharp public class SimpleMovingAverage { private readonly int _k; private readonly int[] _values; private int _index = 0; private int _sum = 0; public SimpleMovingAverage(int k) { if (k <= 0) throw new ArgumentOutOfRangeException(nameof(k), "Must be greater than 0"); _k = k; _values = new int[k]; } public double Update(int nextInput) { _sum = _sum - _values[_index] + nextInput; _values[_index] = nextInput; _index = (_index + 1) % _k; return ((double) _sum) / _k; } } #!markdown Khalid said he wanted to take an `IAsyncEnumerable` of inputs, and retrieve an `IAsyncEnumerable` of outputs. This is essentially just a mapping from one `IAsyncEnumerable` to another. There's a few ways we could do that, but to keep everything encapsulated, I created an `SmaConverter` class that wraps the `SimpleMovingAverage` to do the conversion: #!csharp public class SmaConverter { private readonly SimpleMovingAverage _sma = new(k: 3); public async IAsyncEnumerableHey @andrewlocknet! It would be cool to see your Simple Moving Average take in a IAsyncEnumerable and recalculate the average as new input comes in.https://t.co/NIw8Ow5PA5
— Khalid 🪀 (@buhakmeh) May 7, 2021