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.
Operator constructors
-
AbsoluteValue
-
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
-
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
-
ArcHyperbolicCosine
-
ArcHyperbolicSine
-
ArcHyperbolicTangent
ArcSine
-
ArcTangent
Average
-
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
-
decimalPlaces
permits returning the ceiling of aNumber
to the specified decimal places. You can also partially apply this constructor, e.g.,const Ceil2 = Ceiling()(2)
(datatype
defaults 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
Cosine
-
Cotangent
Divide
-
The
divisor
is divided into thedividend
to return thequotient
. For example, ifdivisor = 2
anddividend = 12
then the quotient is6
because12 / 2 === 6
. -
Exponent
Floor
-
decimalPlaces
permits returning the floor of aNumber
to the specified decimal places. You can also partially apply this constructor, e.g.,const Floor2 = Floor()(2)
(datatype
defaults 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
-
HyperbolicSine
-
HyperbolicTangent
-
Hypotenuse
Log
-
LogBaseTwo
Max
Mean
-
Mean
is the same asAverage
above. Median
Min
Mode
Modulo
-
The
divisor
is divided into thedividend
to return themodulus
. For example, ifdivisor = 2
anddividend = 12
then the quotient is6
because12 / 2 === 6
. -
Multiply
-
NaturalLog
Negate
Power
-
Reciprocal
-
Remainder
Root
-
The
radicand
is the number for which we want the root, and theindex
is the degree of the root. For example, ifradicand = 125
andindex = 3
, then the result is5
because5 * 5 * 5 === 125
. Put another way, the cube root of 125 is 5. -
RootMeanSquare
Round
-
decimalPlaces
permits returning theNumber
rounded to the specified decimal places. You can also partially apply this constructor, e.g.,const Round2 = Round()(2)
(datatype
defaults 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.5545
will round to5.55
.) Secant
Sign
Sine
-
StandardDeviation
-
usePopulation
permits calculation of the standard deviation from the mean (the default) or population standard deviation from the mean. -
Subtract
-
The
subtrahend
is subtracted from theminuend
to return thedifference
. For example, ifminuend = 11
andsubtrahend = 7
then the difference is4
because11 - 7 === 4
Tangent
-
Truncate
-
decimalPlaces
permits returning the truncation of aNumber
to the specified decimal places. You can also partially apply this constructor, e.g.,const Trunc2 = Truncate()(2)
(datatype
defaults 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.