Skip to content

Arithmetic

Filtrera provides essential arithmetic operators for numerical, text, and temporal manipulations. These operators are designed for clarity and precision in business applications.

Available Operators

Addition (+)

  • Numbers: Adds numerical values.
    5 + 3 // Result: 8
  • Text: Concatenates strings.
    'Hello' + 'World' // Result: 'HelloWorld'
  • Time: Adds durations to instants.
    2023-05-03T15:23 + 1 days // Result: 2023-05-04T15:23
  • Durations: Adds durations of the same unit.
    2 days + 1 days // Result: 3 days
    Durations of different units cannot be added:
    2 days + 1 hours // Error

Subtraction (-)

  • Numbers: Subtracts numerical values.
    10 - 4 // Result: 6
  • Time: Subtracts durations from instants.
    2023-05-03T15:23 - 2 hours // Result: 2023-05-03T13:23
  • Durations: Subtracts durations of the same unit.
    5 hours - 2 hours // Result: 3 hours
  • Instants: Subtracting two instants results in a duration in ticks.
    2023-05-03T15:23 - 2023-05-02T12:00 // Result: duration in ticks

Multiplication (*)

  • Numbers: Multiplies numerical values.
    6 * 7 // Result: 42

Division (/)

  • Numbers: Divides numerical values.
    20 / 5 // Result: 4
    Division by zero is caught by the parser unless the divisor is a runtime parameter.

Exponentiation (^)

  • Numbers: Raises a number to the power of another number.
    2 ^ 3 // Result: 8

Type Checking and Error Handling

Filtrera is statically typed with type inference:

  • Type Inference: Most types are inferred, reducing the need for explicit declarations.
  • Error Reporting: The parser reports errors if incompatible types are used with an operator or if there is an illegal operation such as division by zero (unless the divisor is a runtime parameter).

Examples

Basic Arithmetic

5 + 3 // Result: 8
10 - 4 // Result: 6
6 * 7 // Result: 42
20 / 5 // Result: 4
2 ^ 3 // Result: 8

Time Calculations

2023-05-03T15:23 + 1 days // Result: 2023-05-04T15:23
2023-05-03T15:23 - 2 hours // Result: 2023-05-03T13:23
2023-05-03T15:23 - 2023-05-02T12:00 // Result: duration in ticks

Text Concatenation

'Hello' + 'World' // Result: 'HelloWorld'

Complex Expressions

Operator precedence in Filtrera follows standard mathematical rules, with parentheses used to ensure clarity and correct order of operations.

(5 + 3) * 2 - (8 / 4) ^ 2 // Result: 12

Evaluation steps:

  1. `5 + 3` results in `8`
  2. `8 * 2` results in `16`
  3. `8 / 4` results in `2`
  4. `2 ^ 2` results in `4`
  5. `16 - 4` results in `12`

Custom Functions

Custom functions can be created using the `let` keyword. Here’s an example of a function that adds or concatenates two values:

let add (l, r) => l + r

The function parameters are inferred to be of types `number | text | instant | duration`, making it versatile for various operations.

Summary

Filtrera’s arithmetic operators are designed for clear and efficient numerical, text, and temporal operations, with robust type checking and error handling mechanisms to ensure correctness and reliability in business applications.