Operators

Operators are used in Alumio to operate on multiple values. They are used by the operator mapper and the prepared operator mapper.

To use an operator usually multiple values have to be copied to an array before the operator can be applied.

{
"price": 65.5,
"tax_rate": 1.1
}

To get the price including tax, the price and the tax rate should be copied. This can be done using accessor-copy transformer.

{
"prototype": "accessor-copy",
"parameters": {
"source": {
"prototype": "key",
"parameters": {
"keys": [
"price",
"tax_rate"
]
}
},
"destination": {
"prototype": "key",
"parameters": {
"root": "price_incl_tax"
}
}
}
}
{
"price": 65.5,
"tax_rate": 1.1,
"price_incl_tax": {
"price": 65.5,
"tax_rate": 1.1
}
}

After the values to operate on have been copied to an array the operator can be applied. Operations must happen on a list without string keys. An intermediary mapper list-values can make the final preparations.

{
"prototype": "value-mapper",
"parameters": {
"mappers": [
{
"prototype": "list-values"
},
{
"prototype": "operator",
"parameters": {
"operator": "multiplication"
}
}
],
"accessor": {
"prototype": "key",
"parameters": {
"keys": [
"price_inc_tax"
]
}
}
}
}

In succession, this will transform the data into:

{
"price": 65.5,
"tax_rate": 1.1,
"price_inc_tax": [65.5, 1.1]
}

Followed by:

{
"price": 65.5,
"tax_rate": 1.1,
"price_inc_tax": 72.05
}

Operator types

The following operators are available by default.

To introduce custom operations, !! a new operator !! can be created.

Addition

Code: addition

> [10, 15, 2] => 27

Subtraction

Code: subtraction

> [10, 2, 4] => 4

Multiplication

Code: multiplication

> [3, 4, 10] => 120

Division

Code: division

> [120, 10, 4] => 3

Max

Code: max

> [12, 7, 14] => 14

Min

Code: min

> [12, 7, 14] => 7

Mod

Code: mod

> [11, 2] => 1

Pow

Code: pow

> [2, 3] => 8

Concat

Code: concat

> ["foo", "bar"] => "foobar"

Logical And

Code: logical-and

> [true, true]  => true
> [true, false] => false

Logical Or

Code: logical-or

> [true, true]   => true
> [true, false] => true
> [false, false] => false

Logical Xor

Code: logical-xor

> [true, true]   => false
> [true, false] => true
> [false, false] => false

Bitwise And

Code: bitwise-and

> [0, 5] => 0
> [1, 5] => 1

Bitwise Or

Code: bitwise-or

> [0, 5] => 5
> [1, 5] => 5

Bitwise Xor

Code: bitwise-xor

> [0, 5] => 5
> [1, 5] => 4

Bitwise Shift Left

Code: bitwise-shift-left

> [0, 5] => 0
> [1, 5] => 32

Bitwise Shift Right

Code: bitwise-shift-right

> [0, 5] => 5
> [1, 5] => 2

Array Diff

Code: array-diff

> [["tnt", "dynamite], ["c4", "dynamite"]] => ["c4"]

Array Diff Keys

Code: array-diff-keys

> [["dynamite" => 8], ["tnt" => 10, "dynamite" => 12]] => ["tnt" => 10]

Array Intersect

Code: array-intersect

> [["tnt", "dynamite], ["c4", "dynamite"]] => ["dynamite"]

Array Intersect Keys

Code: array-intersect-keys

> [["dynamite" => 8], ["tnt" => 10, "dynamite" => 12]] => ["dynamite" => 12]

Array Merge

Code: array-merge

> [["dynamite" => 8], ["tnt" => 10, "dynamite" => 12]] => ["tnt" => 10, dynamite" => 12]