5Operator

5.1Overview

There are three types of Operators.

  • Prefixed Unary Operator takes an input value specified after it.
  • Suffixed Unary Operator takes an input value specified before it.
  • Binary Operator takes two input values specified on both sides of them.

An Operator has a table of procedures that are indexed by Data Types of given values, one Data Type indexing for Unary Operators and two Data Types for Binary Operators. For instance, operator + has a procedure to calculate between values of number and number and also a procecure beween values of string and string. These procedures are isolated each other as long as combination of the given Data Types is different.

Users can overload operators' procedures through operator instance. If combination of Data Types of the overloading procedure is the same as that of existing one, it would override the registered procedure. Otherwise, it would add a new procedure to the operator.

5.2Precedence

The following table shows operators' precedence order from the lowest to the highest.

Precedence Operators
Lower =>
||
&&
!
in
< > <= >= <=> == !=
..
|
^
&
<< >>
+ -
* / % ?
~
Higher **

5.3Calculation Operators

Basically, Operators are used for mathematical and logical calculation. This subsection explains such functions of operators.

5.3.1Prefixed Unary Operators

Operation +x returns the value of x itself.

Operation Result Data Type
+number number
+complex complex
+rational rational
+array array
+timedelta timedelta

Operation -x returns a negaive value of x.

Operation Result Data Type
-number number
-complex complex
-rational rational
-array array
-timedelta timedelta

Operation ~x returns a bit-inverted value of x.

Operation Result Data Type
~number number

Operation !x returns a logically inverted value of x after evaluating it as a boolean value.

Operation Result Data Type
!any boolean

5.3.2Suffixed Unary Operators

Operation x.. returns an infinite iterator that starts from x and is increased by one.

Operation Result Data Type
number.. iterator

Operation x? returns false if x is false or nil, and true otherwise. This operator is not affected by Implicit Mapping and returns true if x is of list or iterator type.

Operation Result Data Type
any? boolean

5.3.3Binary Operators

Operation x + y returns an added result of x and y.

Operation Result Data Type
number + number number
number + complex complex
number + rational rational
complex + number complex
complex + complex complex
complex + rational (error)
rational + number rational
rational + complex (error)
rational + rational rational
array + array array
datetime + timedelta datetime
timedelta + datetime datetime
timedelta + timedelta timedelta

If x and y are of string or binary type, Operation x + y returns concatenated result of x and y.

Operation Result Data Type
string + string string
binary + binary binary
string + binary binary
binary + string binary
string + any string (`any` will be converted to `string` before concatenation)
any + string string (`any` will be converted to `string` before concatenation)

Operation x - y returns a subtracted result of x and y.

Operation Result Data Type
number - number number
number - complex complex
number - rational rational
complex - number complex
complex - complex complex
complex - rational (error)
rational - number rational
rational - complex (error)
rational - rational rational
array - array array
datetime - timedelta datetime
datetime - datetime timedelta
timedelta - timedelta timedelta

Operation x * y returns a multiplied result of x and y.

Operation Result Data Type
number * number number
number * complex complex
number * rational rational
complex * number complex
complex * complex complex
complex * rational (error)
rational * number rational
rational * complex (error)
rational * rational rational
array * array array
timedelta * number timedelta
number * timedelta timedelta

Applying * operator between string/binary and number will join the string/binary for number times.

Operation Result Data Type
string * number string
number * string string
binary * number binary
number * binary binary

Operation x / y returns a divided result of x and y.

Operation Result Data Type
number / number number
number / complex complex
number / rational rational
complex / number complex
complex / complex complex
complex / rational (error)
rational / number rational
rational / complex (error)
rational / rational rational
array / array array

Operation x % y returns a remainder after dividing x by y.

Operation Result Data Type
number % number number

Operation x ** y returns a powered result of x and y.

Operation Result Data Type
number ** number number
number ** complex complex
complex ** number complex
complex ** complex complex

Operation x == y returns true when x equals to y, and false otherwise.

Operation Result Data Type
any == any boolean

Operation x < y returns true when x is less than y, and false otherwise.

Operation Result Data Type
any < any boolean

Operation x > y returns true when x is greater than y, and false otherwise.

Operation Result Data Type
any > any boolean

Operation x <= y returns true when x is less than or equal to y, and false otherwise.

Operation Result Data Type
any <= any boolean

Operation x >= y returns true when x is greater than or equal to y, and false otherwise.

Operation Result Data Type
any >= any boolean

Operation x <=> y returns 0 when x is equal to y, -1 when x is less than y and 1 when x is greater than y.

Operation Result Data Type
any <=> any number

Operation x in y checks if x is contained in y.

When Operator in takes a value of any type other than list and iterator at its left, it will check if the value is contained in the container specified at its right. If the right value is not of list or iterator, it would act in the same way as Operator ==.

Operation Result Data Type
any in list boolean
any in iterator boolean
any in any boolean

When Operator in takes a value of list or iterator type at its left, it will check if each value of the container's element is contained in the container specified at its right, and return a list of boolean indicating the result of each containing check.

Operation Result Data Type
list in list list
list in iterator list
list in any list
iterator in list list
iterator in iterator list
iterator in any list

When Operator in is used in an argument of for() and cross() function, it would work as an iterable assignment. See Chapter.8. Flow Control for detail.

Operation x & y returns an AND calculation result of x and y.

  • If x and y are of number type, it calculates bitwise AND between them.
  • If x and y are of boolean type, it calculates logical AND between them.
  • If either x or y is nil, it returnsnil.
Operation Result Data Type
number & number number
boolean & boolean boolean
nil & any nil
any & nil nil

Operation x | y returns an OR calculation result of x and y.

  • If x and y are of number type, it calculates bitwise OR between them.
  • If x and y are of boolean type, it calculates logical OR between them.
  • If either x or y is nil, it returns one of their values that is notnil.
Operation Result Data Type
number | number number
boolean | boolean boolean
nil | any nil
any | nil nil

Operation x ^ y returns a XOR calculation result of x and y.

  • If x and y are of number type, it calculates bitwise XOR between them.
  • If x and y are of boolean type, it calculates logical XOR between them.
Operation Result Data Type
number ^ number number
boolean ^ boolean boolean

Operation x << y returns a value of x shifted left by y bits.

Operation Result Data Type
number << number number

Operation x >> y returns a value of x shifted right by y bits.

Operation Result Data Type
number >> number number

Operation x && y returns a conditional AND result of x and y as described below:

  • If x is not of list nor iterator type, it would return the value of x when x is determined as false, and return the value of y otherwise. It won't evaluate y when x comes out to be in false state.
  • If x is of list type, it applies the above operation on each value of the list's elements and returns a list containing the results.
  • If x is of iterator type, it returns an iterator that is to apply the above operation on each value of the iterator's elements.
Operation Result Data Type
any && any any
list && any list
iterator && any iterator

Operation x || y returns a conditional OR result of x and y as described below:

  • If x is not of list nor iterator type, it would return the value of x when x is determined as true, and return the value of y otherwise. It won't evaluate y when x comes out to be in true state.
  • If x is of list type, it applies the above operation on each value of the list's elements and returns a list containing the results.
  • If x is of iterator type, it returns an iterator that is to apply the above operation on each value of the iterator's elements.
Operation Result Data Type
any || any any
list || any list
iterator || any iterator

Operation x..y creates an iterator that returns number value that starts from x and is increased by one until y.

Operation Result Data Type
number..number iterator

Operation x => y returns a list [x, y].

Operation Result Data Type
number => any list
string => any list
symbol => any list

When Operator => is used in an argument declaration of any function definition, it would work as an assignment for a default value. And, when it is used in an argument list of any function call, it would work as a named argument. See Chapter.7. Function for their detail.

5.4Other Operators

Operation string % any returns a result formatted by the string containing specifiers of printf format. The value of any must be a list if more than one argument are necessary.

'Name: %s, Age: %d' % [name, age]

The code above has the same result as the following.

format('Name: %s, Age: %d', name, age)

Operation function * any applies the function on any.

Operation stream << any outputs any to the stream.

sys.stdout << 'Hello World\n'

5.5Operator Overload

You can assign your own functions to operators through operator instance. The example below assings string - string operation by using operator#assign() method.

op = operator(`-)
op.assign(`string, `string) {|x, y|
    x.replace(y, '')
}

After this assignment, the following code results in 'Hello, world'.

'Hello, 1234world' - '1234'

If you want to assign a function of a unary operator, specify one argument in operator#assign() method like below.

op = operator(`-)
op.assign(`string) {|x|
    x.each().reverse().join()
}

Then, the code below has a result '987654321'.

-'123456789'

You can also override existing operators.

You can use operator#entries() method to get all of the functions registered in the operator.

op = operator(`-)
println(op.entries())

The method returns entries registered as binary operators. Specifying a symbol `unary as its argument would return a list of unary operators.

op = operator(`-)
println(op.entries(`unary))