Constant

Constant is a curried constructor used to create a JS/JSON configuration object that represents an constant to be injected into an operation.

(datatype = "Number") =>
  value => ({
    tag: "Constant",
    datatype,
    value,
  })

The first parameter to Constant determines the datatype of the return value injected. This defaults to "Number".

The second parameter is the value to be returned by the Constant operation. Here's how it works:

Constant()(42) // => { tag: "Constant", datatype: "Number", value: 42 }

Because Constant is curried, we can simplify our code by partial application, for example, const Int = Constant("Integer"). This returns a function that takes an integer and returns the Constant operation:

const Int = Constant()
 
Int(42) // => { tag: "Constant", datatype: "Number", value: 42 }

This can help to unclutter our code, reducing cognitive load. Be sure to name well. See below for an example.

The constant function

We pass our configuration object to composeOperators, which composes the operation and returns a single function.

This function takes an optional argument (ignored in this instance) and returns the injected constant as a Right, e.g., { right: 42 }, or a Left with an array of Error objects if something goes wrong, { left: [Error] }.

const constant = operation => (_ => Either<Array<Error>, result>)

composeOperators works by recursing down through the operation object, calling the correct operator function based on the tag (e.g., "Constant"), and composing the functions returned, until it hits an Injector. At that point it rolls everything back up. composeOperators then returns this composed function.

See composeOperators for a more detailed explanation.

The injectors are not actually called until this composed calculate function is run. Hence, evaluation is lazy: the values are not injected until the last moment. In the case of Constant this makes no difference, as the value is, well, constant.

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

Well, there's not much to see here. Constants are constant. But here's an example to play with. We've taken advantage of currying (but you don't have to).

Try passing bad data (e.g., change the 77 to "Bob") to see an Error returned in the console.