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 -
decimalPlacespermits returning the ceiling of aNumberto the specified decimal places. You can also partially apply this constructor, e.g.,const Ceil2 = Ceiling()(2)(datatypedefaults toNumber). -
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
divisoris divided into thedividendto return thequotient. For example, ifdivisor = 2anddividend = 12then the quotient is6because12 / 2 === 6. -
Exponent -
const Exponent = datatype => operand => ExponentOp Floor-
const Floor = datatype => (decimalPlaces = 0) => operand => FloorOp -
decimalPlacespermits returning the floor of aNumberto the specified decimal places. You can also partially apply this constructor, e.g.,const Floor2 = Floor()(2)(datatypedefaults toNumber). -
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 -
Meanis the same asAverageabove. 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
divisoris divided into thedividendto return themodulus. For example, ifdivisor = 2anddividend = 12then the quotient is6because12 / 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
radicandis the number for which we want the root, and theindexis the degree of the root. For example, ifradicand = 125andindex = 3, then the result is5because5 * 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 -
decimalPlacespermits returning theNumberrounded to the specified decimal places. You can also partially apply this constructor, e.g.,const Round2 = Round()(2)(datatypedefaults toNumber). -
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.5545will round to5.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 -
usePopulationpermits 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
subtrahendis subtracted from theminuendto return thedifference. For example, ifminuend = 11andsubtrahend = 7then the difference is4because11 - 7 === 4 Tangent-
const Tangent = datatype => operand => TangentOp -
Truncate -
const Truncate = datatype => (decimalPlaces = 0) => operand => TruncateOp -
decimalPlacespermits returning the truncation of aNumberto the specified decimal places. You can also partially apply this constructor, e.g.,const Trunc2 = Truncate()(2)(datatypedefaults toNumber). -
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.