Operators

All operator constructors are curried. The first parameter is the datatype returned, which defaults to Number but can be set to Integer. Note: mixes of integers and floats promote to floats.

The remaining parameters are used to configure the operand or operands that will be used in the calculation. In some special cases, they may configure other options such as the number of decimal places.

The operands are themselves operations, which may be nested to any depth; however, the bottom (leaf) nodes must be value injectors.

The result returned from the operator functions below is an Operation configuration object. For example:

This object can be passed to the composeOperators function, which recurses through the operations returning and composing the mathematical functions and injectors.

The function returned from composeOperators takes an optional numerical argument and then runs the calculation and returns either a Left<Array<Error>> if the calculation failed, or a Right<Number | Integer>, e.g., { right: 42 }, if it succeeded. See the above example.

We can use composeOperators to create the composed "calculate" function, as below. When we call calculate with an optional argument, it does the calculation and returns an Either result, as above.

composeOperators(op) => (arg => Either<Array<Error>, Number | Integer>)

Operator constructors

AbsoluteValue
const AbsoluteValue = datatype => operand => AbsoluteValueOp
The function this operation creates will return the absolute value of whatever the operand evaluates to. For example, if the operand evaluates to { right: -7 }, then the function will return { right: 7 }.
Add
const Add = datatype => (addends = []) => AddOp
The function this operation creates will evaluate the operands and then sum the resulting numbers together and return the result. For example, if the operands evaluate to [{ right: 2 },{ right: 2 }] then the function will return { right: 4 }.
ArcCosine
const ArcCosine = datatype => operand => ArcCosineOp
ArcHyperbolicCosine
const ArcHyperbolicCosine = datatype => operand => ArcHyperbolicCosineOp
ArcHyperbolicSine
const ArcHyperbolicSine = datatype => operand => ArcHyperbolicSineOp
ArcHyperbolicTangent
const ArcHyperbolicTangent = datatype => operand => ArcHyperbolicTangentOp
ArcSine
const ArcSine = datatype => operand => ArcSineOp
ArcTangent
const ArcTangent = datatype => operand => ArcTangentOp
Average
const Average = datatype => (operands = []) => AverageOp
The function this operation creates will evaluate the operands and sum the resulting numbers together, then divide that number by the number of operands and return the result. For example, if the operands evaluate to [{ right: 5 },{ right: 9 }] then the function will return { right: 7 }.
Ceiling
const Ceiling = datatype => (decimalPlaces = 0) => operand => CeilingOp
decimalPlaces permits returning the ceiling of a Number to the specified decimal places. You can also partially apply this constructor, e.g., const Ceil2 = Ceiling()(2) (datatype defaults to Number).
Now you can use this partially-applied function to generate the Ceiling operation: const ceilingOp = Ceil2(Constant()(5.5555)) which will return { right: 5.56 } when evaluated in a calculation.
Cosecant
const Cosecant = datatype => operand => CosecantOp
Cosine
const Cosine = datatype => operand => CosineOp
Cotangent
const Cotangent = datatype => operand => CotangentOp
Divide
const Divide = datatype => dividend => divisor => DivideOp
The divisor is divided into the dividend to return the quotient. For example, if divisor = 2 and dividend = 12 then the quotient is 6 because 12 / 2 === 6.
Exponent
const Exponent = datatype => operand => ExponentOp
Floor
const Floor = datatype => (decimalPlaces = 0) => operand => FloorOp
decimalPlaces permits returning the floor of a Number to the specified decimal places. You can also partially apply this constructor, e.g., const Floor2 = Floor()(2) (datatype defaults to Number).
Now you can use this partially-applied function to generate the Floor operation: const floorOp = Floor2(Constant()(5.5555)) which will return { right: 5.55 } when evaluated in a calculation.
HyperbolicCosine
const HyperbolicCosine = datatype => operand => HyperbolicCosineOp
HyperbolicSine
const HyperbolicSine = datatype => operand => HyperbolicSineOp
HyperbolicTangent
const HyperbolicTangent = datatype => operand => HyperbolicTangentOp
Hypotenuse
const Hypotenuse = datatype => (operands = []) => HypotenuseOp
Log
const Log = datatype => operand => LogOp
LogBaseTwo
const LogBaseTwo = datatype => operand => LogBaseTwoOp
Max
const Max = datatype => (operands = []) => MaxOp
Mean
const Mean = datatype => (operands = []) => MeanOp
Mean is the same as Average above.
Median
const Median = datatype => (operands = []) => MedianOp
Min
const Min = datatype => (operands = []) => MinOp
Mode
const Mode = datatype => (operands = []) => ModeOp
Modulo
const Modulo = datatype => dividend => divisor => ModuloOp
The divisor is divided into the dividend to return the modulus. For example, if divisor = 2 and dividend = 12 then the quotient is 6 because 12 / 2 === 6.
Multiply
const Multiply = datatype => (multipliers = []) => MultiplyOp
NaturalLog
const NaturalLog = datatype => operand => NaturalLogOp
Negate
const Negate = datatype => operand => NegateOp
Power
const Power = datatype => base => exponent => PowerOp
Reciprocal
const Reciprocal = datatype => operand => ReciprocalOp
Remainder
const Remainder = datatype => dividend => divisor => RemainderOp
Root
const Root = datatype => radicand => index => RootOp
The radicand is the number for which we want the root, and the index is the degree of the root. For example, if radicand = 125 and index = 3, then the result is 5 because 5 * 5 * 5 === 125. Put another way, the cube root of 125 is 5.
RootMeanSquare
const RootMeanSquare = datatype => (operands = []) => RootMeanSquareOp
Round
const Round = datatype => (decimalPlaces = 0) => operand => RoundOp
decimalPlaces permits returning the Number rounded to the specified decimal places. You can also partially apply this constructor, e.g., const Round2 = Round()(2) (datatype defaults to Number).
Now you can use this partially-applied function to generate the Round operation: const roundOp = Round2(Constant()(5.5555)) which will return { right: 5.56 } when evaluated in a calculation. (5.5545 will round to 5.55.)
Secant
const Secant = datatype => operand => SecantOp
Sign
const Sign = datatype => operand => SignOp
Sine
const Sine = datatype => operand => SineOp
StandardDeviation
const StandardDeviation = datatype => (usePopulation = false) =>
  (operands = []) => StandardDeviationOp
usePopulation permits calculation of the standard deviation from the mean (the default) or population standard deviation from the mean.
Subtract
const Subtract = datatype => minuend => subtrahend => SubtractOp
The subtrahend is subtracted from the minuend to return the difference. For example, if minuend = 11 and subtrahend = 7 then the difference is 4 because 11 - 7 === 4
Tangent
const Tangent = datatype => operand => TangentOp
Truncate
const Truncate = datatype => (decimalPlaces = 0) => operand => TruncateOp
decimalPlaces permits returning the truncation of a Number to the specified decimal places. You can also partially apply this constructor, e.g., const Trunc2 = Truncate()(2) (datatype defaults to Number).
Now you can use this partially-applied function to generate the Truncate operation: const truncOp = Trunc2(Constant()(-5.5555)) which will return { right: -5.56 } when evaluated in a calculation.