Skip to content

Numbers

Tracking range

Trackers.number keeps track of the minimum, maximum and average of a stream of numbers, without storing them.

import { Trackers } from 'https://unpkg.com/ixfx/dist/bundle.js';
// Initialise
const t = Trackers.number();
// Add some random values
for (let i=0;i<10;i++) t.seen(Math.floor(Math.random()*100)));
// Get computed values
t.avg // eg. 25.3
t.min // eg. 2
t.total // eg. 534

An example usage might be to track the range of a sensor over time. The pointer scale demo (source) uses numberTracker and pointsTracker.

Counting

Numbers.count is a generator that produces integer values from zero.

import { count } from 'https://unpkg.com/ixfx/dist/numbers.js';
let str = ``;
for (const v of count(5)) {
v += v + ' ';
} // v: "0 1 2 3 4 "
// Or:
const values = [...count(5)]; // [0,1,2,3,4]

Numbers.count is useful because the code is more readable than using a for loop to increment, and it cuts down on likely errors.

Values within a range

Numbers.numericRange generates values in a range by a given step amount (much like a classic for loop).

import { numericRange } from 'https://unpkg.com/ixfx/dist/numbers.js';
// Count from 0..1 by steps of 0.1
let loopForever = numericRange(0.1, 0, 1);
for (v of loopForever) {
console.log(v);
}

Range

Arrays.minMaxAvg might be useful when working with arrays. It returns the minimum, maximum, average and total.

import { Arrays } from 'https://unpkg.com/ixfx/dist/data.js';
const mma = Arrays.minMaxAvg([100,20,0,50]); // { total:170, max:100, min:0, avg:42.5 }

Difference

Numbers.differenceFromLast computes the change of a value compared to the previously-provided value. This can be more readable than keeping track of the value in a separate variable, if all you really care about is the difference.

By default, it returns the absolute difference, ignoring whether the latest value is higher or lower:

const d = differenceFromLast();
d(10); // 0 - initial value, so difference is 0
d(11); // 1
d(10); // 1

You can specify different behaviours: ‘numerical’, ‘relative’, ‘relativeSigned’, ‘absolute’ (which is the default). An initial value can also be provided as a second option.

const d = differenceFromLast(`absolute`, 10);
d(11); // 1

‘numerical’ includes the sign so you can tell if latest value is higher or lower

const d = differenceFromLast(`numerical`);
d(10); // 0
d(11); // 1
d(10); // -1

‘relative’ takes the difference from the last value, and then also dividing by the last value, giving some sense of the relative difference. ‘relative’ ignores if the value is increasing or decreasing, use ‘relativeSigned’ to include ’-’ when the new value is below the last.

import { differenceFromLast } from 'https://unpkg.com/ixfx/dist/numbers.js'
const d = differenceFromLast(`relative`);
d(10); // 0
d(11); // 0.1
d(10); // 0.09 (1/11)

Numbers.differenceFromFixed takes the same parameters (albeit in different order), but compares against a fixed value instead of the last value.

import { differenceFromFixed } from 'https://unpkg.com/ixfx/dist/numbers.js'
const d = differenceFromFixed(100, `relativeSigned`);
d(100); // 0
d(150); // 0.5
d(10); // -0.90