Math Operators and Arrays
Sonar is built on the premise that simpler is always better. To allow programmers write simpler, better programs, Sonar allows arrays to be manipulated with mathematical operators like + and -.
The operators that can be used with arrays can be split into two categories:
Operators where the right-hand side value is also an array.
Operators where the right-hand side value is an integer.
Operators with an Array on Both Sides
Only three operators can be used when the right-hand value is also an array.
+ (plus)
Concatenate the two arrays, producing one "mega-array" containing elements of the two operands. Any elements that appear more than once in either operand is repeated (i.e this operation does not create a set).
[1, 2, 3] + [4, 5, 6]
== (equals)
Checks whether the two arrays have the same element in the same order. Returns true or false.
[1, 2, 3] == [1, 2, 3]
!= (doesn't equal)
Returns true if the two arrays aren't equal. Returns false if they are.
[1, 2, 3] != [1, 2]
Operators with an Integer on the Right-hand Side
Three operators can be used when the right-hand side is an integer.
/ (slash)
Splits the array into chunks. The length of each chunk is equal to the value on the right-hand side.
[1,2,3,4,5,6] / 3
- (minus)
Removes the element at the index specified by the integer on the right-hand side. If the integer is greater than, or equal to, the length of the array, an error is returned. Acts similarly to "pop()", except that the value returned here is the new array (pop() returns the removed element).
[1,2,3,4] - 2
* (asterisk)
Repeats the array the number of times specified by the integer on the right-hand side and returns a new array containing the repetitions.
[1,2,3] * 3
Last updated