Skip to content

SimpleRollingAverage

Defined in: math/rolling-average.ts:37

A simple implementation of the RollingAverage interface. It maintains an internal array of values and a maximum size for the window. When a new value is added, it checks if the number of values exceeds the maximum size, and if so, it removes the oldest value. The average is calculated by summing the current values and dividing by the number of values in the window.

Example

const rollingAverage = new SimpleRollingAverage(3);
rollingAverage.add(1);
rollingAverage.add(2);
rollingAverage.add(3);
console.log(rollingAverage.getAverage()); // { type: "Left", left: 2 }
rollingAverage.add(4);
console.log(rollingAverage.getAverage()); // { type: "Left", left: 3 }

Implements

Constructors

Constructor

new SimpleRollingAverage(maxSize): SimpleRollingAverage

Defined in: math/rolling-average.ts:46

Creates a new instance of SimpleRollingAverage with the specified maximum size for the window.

Parameters

maxSize

number

The maximum number of values to keep in the rolling average window. Must be greater than 0.

Returns

SimpleRollingAverage

Throws

Will throw an error if maxSize is less than or equal to 0.

Methods

add()

add(value): void

Defined in: math/rolling-average.ts:57

Adds a new value to the rolling average. If the number of values exceeds the maximum size, the oldest value is removed.

Parameters

value

number

The new value to add to the rolling average.

Returns

void

Implementation of

RollingAverage.add


getAverage()

getAverage(): Either<number, string>

Defined in: math/rolling-average.ts:68

Calculates the average of the current values in the rolling average window. If no values have been added, it returns an error message.

Returns

Either<number, string>

An Either containing the average of the values (Left) or an error message (Right) if no values have been added.

Implementation of

RollingAverage.getAverage