immutable-math
Some mathematatics for Immutable.js for aggregations and stuff.
Documentation here.
install
npm install immutable-math
Usage
Functions in this library use partially applied functions.
For example, one way to use average
is to call it, passing in any parameters required (average
requires no extra params), then call the returned function passing in your data.
const numbers = fromJS([1,1,1,5]);
return average()(numbers);
For any immutable-math functions that return an Iterable
, this design of using a partially applied function allows for easy chaining by using them inside of an update()
method, if your input Iterable
has an update function.
return fromJS([1,1,1,5])
.update(exampleFunction())
.sort()
.toJS();
You can also define a function to perform a specific operation, and use it multiple times by passing in different input data.
const numbersA = fromJS([
{num: 1},
{num: 1},
{num: 1},
{num: 5}
]);
const numbersB = fromJS([
{num: 3},
{num: 5}
]);
const averageByNum = averageBy(ii => ii.get('num'));
const averageA = numbersA.update(averageByNum);
const averageB = numbersB.update(averageByNum);