Numbers
Tracking range
Trackers.number keeps track of the minimum, maximum and average of a stream of numbers, without storing them.
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.
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).
Range
Arrays.minMaxAvg might be useful when working with arrays. It returns the minimum, maximum, average and total.
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:
You can specify different behaviours: ‘numerical’, ‘relative’, ‘relativeSigned’, ‘absolute’ (which is the default). An initial value can also be provided as a second option.
‘numerical’ includes the sign so you can tell if latest value is higher or lower
‘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.
Numbers.differenceFromFixed takes the same parameters (albeit in different order), but compares against a fixed value instead of the last value.