Ceiling
Ceiling
is a curried constructor used to create a JS/JSON configuration
object that represents an ceiling operation.
The first parameter to Ceiling
determines the datatype of the return
value of the ceiling operation: Number
(float) or
Integer
.
The second parameter is an integer that represents the number of decimal places to include in the result, default 0.
The third parameter is an operation that must eventually evaluate to a numerical value. This is the operand for which we will calculate the ceiling.
The operand operation (add, subtract, etc.) may include nested operations to any depth, but the bottom (leaf) nodes must return values. Values are injected with injectors.
Because Ceiling
is curried, we can simplify our code by partial
application, for example,
const Ceil2 = Ceiling("Integer")(2)
. This returns a function
that takes the operand operation and returns an Ceiling configuration object
for working with numbers to two decimal places.
See below for an example.
The ceiling
function
We pass our configuration object to composeOperators
, which composes the operations and returns a single function. This
function takes an optional argument and returns the result of the
calculation as a Right
, e.g., { right: 42 }
, or a Left
with an array of Error
objects,
{ left: [Error] }
.
composeOperators
works by recursing down through the
operation
object, calling the correct operator function based on
the tag
(e.g., "Ceiling"), and composing the functions returned.
composeOperators
then returns this composed function. See composeOperators
for a more detailed explanation.
The injectors are not called until this composed calculate
function
is run. Hence, evaluation is lazy: the values are not injected until the last
moment.
See the injectors for a complete list of how values may be injected. See the list of operators for the full range of available mathematical operations.
Example
We use composeOperators
to create a calculate
function, passing it our Ceiling
operation object. We call the calculate
function and pass it an
(optional) argument to run our operation(s) and calculate the product.
Here our operation includes an operand in which the value is
passed in via the optional argument to calculate
. This is done
using FromArgument
.
Try leaving one of the numbers undefined to see an Error
returned.