Operators
All operators (except the pipe operator) will require its arguments to be of the same type. So, it will not be possible to add an int to a float without first converting one or the other to the respective type.
Logical
Section titled “Logical”// andtrue and true; // truetrue and false; // falsefalse and false; // false// ortrue or true; // truetrue or false; // truefalse or false; // false// notnot true; // falsenot false; // trueConcatenation
Section titled “Concatenation”Because strings are arrays of characters, the concatenation operator is the same for both.
[a, b, c] <> [d, e, f] == [a, b, c, d, e, f];"Hello" <> " " <> "world" == "Hello world";You can also use the concatenation operator between maps, provided that they are of the same type:
[str]int months_to_num_1 = ["jan": 1, "feb": 2, "mar": 3];[str]int months_to_num_2 = ["apr": 4, "may": 5, "jun": 6];[str]int months_to_num_3 = ["jul": 7, "aug": 8, "sep": 9];[str]int months_to_num_4 = ["oct": 10, "nov": 11, "dec": 12];
[str]int months_to_num = months_to_num_1 <> months_to_num_2 <> months_to_num_3 <> months_to_num_4;// ["jan": 1, "feb": 2, "mar": 3, "apr": 4, ... , "dec": 12]
[str]float different_type = ["new": 13.0];months_to_num <> different_type; // error: cannot concatenate maps of different typesArithmetic
Section titled “Arithmetic”// addition1 + 2 == 3;// subtraction2 - 1 == 1;// exponent2 ** 6 == 64;// modulo10 % 4 == 2;// division5.0 / 2.0 == 2.5;5 / 2 == 2;// multiplication3 * 2 == 6;Comparisons
Section titled “Comparisons”// equality1 == 1;// inequality1 != 2;// less than1 < 2;// greater than1 > 0;// less than or equal to5 <= 6;5 <= 5;// greater than or equal to7 >= 4;7 >= 7;Bit arithmetic
Section titled “Bit arithmetic”Bit arithmetic will only be allowed for integers.
// bit shift left1 << 4 == 16;// bit shift right6 >> 1 == 3;// bitwise and27 & 1 == 1;// bitwise or8 | 1 == 9;// bitwise xor12 ^ 1 == 13;// bitwise not!6 == -7;// bitwise rotation (circular shift)9 <<< 1 == 3; // rotate left9 >>> 1 == 12; // rotate rightSimilar to functional programming, we can use a |> pipe operator to pass values from one function to the next. This will automatically fill in the first value of the receiving function with the value being passed into it. For example, something like this:
fn square(int[] numbers) -> int[] { mut int[] numbers = numbers;
for i in numbers { numbers[i] = numbers[i] ** 2; }
return numbers;}
fn add_to_all(int[] numbers, int adding) -> int[] { mut int[] numbers = numbers;
for i in numbers { numbers[i] = numbers[i] + adding; }
return numbers;}
mut int[] numbers = [1, 2, 3, 4, 5];numbers = square(numbers); // [1, 4, 9, 16, 25]numbers = add_to_all(numbers, 5); // [6, 10, 15, 21, 30]can be turned into a pipeline, like so:
// Same function definitions as above
int[] numbers = [1, 2, 3, 4, 5] |> square() |> add_to_all(5);While this may seem pointless for a simple case like this, it becomes incredibly helpful for situations where a piece of data needs to be passed around a lot. For example:
// Some data to be transformedDataStruct[] new_data = data |> map(some_fn) |> filter(some_filter) |> sort(some_sort) |> take(10); // Take the first 10 valuesInclusion
Section titled “Inclusion”The in keyword acts as the inclusion operator, used for checking if a value is included in a container value. You can check for:
-
Typein arrayType[]:int[] arr = [1, 2, 3, 4, 5];assert(5 in arr); -
Keyin map[Key]Value:[char]int map = ['a': 1, 'b': 2, 'c': 3];assert('a' in map); -
char/strinstr:str string = "Hello world";assert('H' in string);assert("Hello" in string); -
int/uintin ranges:assert(1 in 1:5); // intassert(1u in 1u:5u); // uint