Operators

This auxiliary module exports a set of Lua operators as intrinsic functions to use with the library high-order primitives.

Note

op can be used as a shortcut to operator.

Comparison operators

fun.operator.le(a, b)
Returns

a <= b

fun.operator.lt(a, b)
Returns

a < b

fun.operator.eq(a, b)
Returns

a == b

fun.operator.ne(a, b)
Returns

a ~= b

fun.operator.ge(a, b)
Returns

a >= b

fun.operator.gt(a, b)
Returns

a > b

Arithmetic operators

fun.operator.add(a, b)
Returns

a + b

fun.operator.div(a, b)

An alias for truediv().

fun.operator.truediv(a, b)
Returns

a / b

Performs “true” float division. Examples:

> print(operator.div(10, 3))
3.3333333333333
> print(operator.div(-10, 3))
-3.3333333333333
fun.operator.floordiv(a, b)
Returns

math.floor(a / b)

Performs division where a result is rounded down. Examples:

> print(operator.floordiv(10, 3))
3
> print(operator.floordiv(12, 3))
4
> print(operator.floordiv(-10, 3))
-4
> print(operator.floordiv(-12, 3))
-4
fun.operator.intdiv(a, b)

Performs C-like integer division.

Equivalent to:

function(a, b)
    local q = a / b
    if a >= 0 then return math.floor(q) else return math.ceil(q) end
end

Examples:

> print(operator.floordiv(10, 3))
3
> print(operator.floordiv(12, 3))
4
> print(operator.floordiv(-10, 3))
-3
> print(operator.floordiv(-12, 3))
-4
fun.operator.mod(a, b)
Returns

a % b

Note

Result has same sign as divisor. Modulo in Lua is defined as a % b == a - math.floor(a/b)*b.

Examples:

> print(operator.mod(10, 2))
0
> print(operator.mod(10, 3))
2
print(operator.mod(-10, 3))
2 -- == -1 in C, Java, JavaScript and but not in Lua, Python, Haskell!
fun.operator.neq(a)
Returns

-a

fun.operator.unm(a)

Unary minus. An alias for neq().

fun.operator.pow(a, b)
Returns

math.pow(a, b)

fun.operator.sub(a, b)
Returns

a - b

String operators

fun.operator.concat(a, b)
Returns

a .. b

fun.operator.len(a)
Returns

# a

fun.operator.length(a)

An alias for len().

Logical operators

fun.operator.land(a, b)
Returns

a and b

fun.operator.lor(a, b)
Returns

a or b

fun.operator.lnot(a)
Returns

not a

fun.operator.truth(a)
Returns

not not a

Return true if a is true, and false otherwise. Examples:

> print(operator.truth(1))
true
> print(operator.truth(0))
true -- It is Lua, baby!
> print(operator.truth(nil))
false
> print(operator.truth(""))
true
> print(operator.truth({}))
true