Array Operators

The following operators are valid for arrays. Note: in addition to these operators, there is a membership infix operator that works on arrays and maps, and there are also Set Operators, which are extremely useful when comparing & inspecting arrays. 

all()

The all() operator takes a function as its sole argument. All() returns a true value if the function returns true for every item in the target array or if the target array is empty. 

For example

any()

The any() operator takes a function as its sole argument. Any() returns a true value if the function returns true for at least one item in the target array.

For example

append()

The append() operator combines two arrays into a single array containing all elements belonging to the original arrays. For example:

If either the object or the argument is not an array, it will be wrapped in an array.

The following examples show the behavior of append() for various types of objects:

collect()

The collect() operator takes a function as its only argument and returns a map. The function should take a single argument and return a literal. The function should be thought of as a categorization function. It is applied to each member of the array in turn and the result is used as a collection key to group the members of the array into lists whose members all yield the same result. 

filter()

The filter() operator takes a function as its only argument and returns an array. The new array contains any members of the original array for which the function evaluates to true. The function given as the argument must take one argument and return a Boolean value. The length of the new array will be less than or equal to the length of the original array. For example:

head()

The head() operator returns the first member of an array as a single value, or null if the array is empty.

index()

The index() operator returns the index in the array of the first item that matches its argument

The types of matched values must be num, str, or bool. The operator does not match other types of values.

Returns -1 if no such item could be found.

join()

The join() operator takes a string as its sole argument. The original array is joined into a single string with the argument placed between the array elements. If the argument is omitted, the default separator is ','. For example:

length()

The length() operator returns a number that is the length of the array. Note that because arrays use zero-based indexing, the length is the one greater than the index value of the last element in the array. For example:

map()

The map() operator returns an array that contains the results of applying the function given as the operator's argument to each member of the original array. The function given as an argument must take one argument. The length of the resulting array will be equal to the length of the target array. For example:

none()

The none() operator takes a function as its sole argument. None() returns a true value if the function returns false for every item in the target array or if the target array is empty. None() is the logical negation of any()

For example

notall()

The notall() operator takes a function as it's sole argument. Notall() returns a true value if the function returns false for at least one item in the target array. This is the logical inverse of all().

For example

pairwise()

The pairwise() operator is applied to an array of two arrays and takes a two-argument function as it's sole argument. Pairwise() returns a new array that is the result of applying the function to each of the members of the two arrays in the target array pairwise.  

For example

reduce()

The reduce() operator applies a function, given as its first argument, to members of an array in left associative order. An optional second argument to the operator is the default value for the reduction.

If the default value is not supplied, reduce() returns the result of applying function to the first 2 items in the array, then applying the function to that result and the 3rd item, etc. If the array contains no items, 0 is returned. If the array has only one item, the item is returned.

If the default value is supplied, reduce() returns the result of applying function to the default value and the first item in the array, then applying the function to that result and the 2nd item, etc. If array contains no items, reduce() returns the default value.

The function (supplied to the reduce() operator as its first argument) should take at least two arguments. The first argument accumulates the result, starting with the second operator argument, if any, as described above. The function's second argument is each element, in turn, of the array upon which reduce is operating. There is also a third argument, which is the current index in the array, and a fourth argument, which is the entire array.

When using reduce() with arrays that contain values whose type differs from the return type of the function, the first argument to the function should have the same type as the returned value, and you must supply a default value that has the same type as the return value of the function.  For example:

reverse()

The reverse() operator reverses the order of the array. 

slice()

The slice() operator creates a new array based on the beginning and end indices passed as arguments:

  • Array indices are zero-based.
  • The default for the beginning index is 0. slice(j) is the same as slice(0,j)
  • A reference to an OOB index (less than 0 or greater than the size of the array - 1) will return an empty array []

For example:

splice()

The splice() operator creates a new array that is the result of deleting, inserting, or replacing elements in the target array. The operators takes the following arguments:
  • the zero-based index of where to start the splice
  • the number of elements to remove at the location given by the first argument
  • an optional value to be spliced in the array at the location given by the first argument 
The following example shows elements being removed from an array:


If the optional third argument is included it will be inserted. If the argument is an array, the elements of the array will all be inserted. 

If the third argument is not an array, its value will be inserted. 

In the preceding examples, we've been removing zero elements (i.e. simply inserting). If the second argument is non-zero, then that many elements will be removed before the elements of the third argument are inserted at the location where the elements were removed:

If the second argument is larger than the remaining elements in the array, the array will be truncated at the location given by the first parameter:

sort()

The sort() operator takes an optional argument and returns an array that is the original array sorted according to the following criteria:

  • If the argument is empty or the string "default", the array will be sorted in ascending order using the cmp operator.
  • If the argument is the string "reverse", the array will be sorted in descending order using the cmp operator.
  • If the argument is the string "numeric", the array will be sorted in ascending order using the <=> operator.
  • If the argument is the string "ciremun" ("numeric" backwards), the array will be sorted in descending order using the <=> operator.
  • If the argument is a function, the function will be used to perform pair-wise comparisons of the members of the array for purposes of doing the sort. The function must take two arguments and return -1, 0, or 1 depending on whether the first argument is less than, equal to, or greater than the second.

Note that because the default behavior is to do a string comparison, number sorts can give unexpected results, as shown in the first example.

For example:

tail()

The tail() operator returns a new array that is the original array with the first element removed, or an empty array if the original array is empty. For example:

Note

This note is for the operators which expect a function as their argument: all, any, collect, filter, map, none, and notall. The function provided is called once for each element of the target array, in order, and actually has three arguments passed to it: first, the array element, second, the current index in the array, and third, the entire target array. The last two are rarely if ever needed, but are available for those rare cases. Ordinarily, only the first argument, the array element, is needed.

For example, suppose you needed to know if any element in an array was less than its index in the array.