Root
Root
is a curried constructor used to create a JS/JSON configuration
object that represents an root operation.
The first parameter to Root
determines the datatype of the return
value of the root operation: Number
(float) or Integer
.
The second parameter is the radicand
; the third is the index
. Both are operations, which must eventually evaluate to
values. The index determines the root: 2 for square root, 3 for cube root,
etc.
The radicand and index operations (multiply, subtract, etc.) may be nested to any depth, but the bottom (leaf) nodes must return values. Values are injected with injectors.
Because Root
is curried, we can simplify our code by partial application,
for example, const R = Root("Number")
. This returns a function
that takes the radicand and index operations and returns a Root
configuration for working with integers.
See below for an example.
The root
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 radicandd
on the tag
(e.g., "Root"), 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 Root
operation object. We call the calculate
function and pass it an
(optional) argument to run our operation(s) and calculate the quotient.
Here our radicand operation is a nested multiplication operation of three
constants (using Constant
).
Our index is also an operation that returns the argument passed to the
composed function (using FromArgument
).
Try leaving one of the numbers undefined to see an Error
returned.