Skip to main content

Lists

There are several ways to construct a list. The simplest way is by writing a list literal, a comma-separated sequence of expressions within brackets. For example, you can write [10, 20, 30] or ["a", "b", "c"]. Note that any type of value can be placed inside a list. For example, you could have a list of lists, such as [[10, 20], [30, 40]].

The second way to construct a list is via foreach … select … expressions, also known as comprehensions. Comprehensions are described in details below.

The third way to construct a list is to append two lists together. For example, if list1 and list2 are two lists of items of the same type, then list1 + list2 is the list consisting of all items from list1 and list2 (preserving order, with list1 items first).

The fourth way to construct a list is to take the difference of two lists. For example, if list1 and list2 are two lists of items of the same type, then list1 - list2 is the list consisting of all items from list1 that are not in list2 (preserving order).

Additionally, a list of numbers in a range can be constructed with fromTo(a, b), which equals the list of numbers starting from a and ending with b. For example,fromTo(1, 3) is the same as [1, 2, 3]. If the second argument is smaller than the first, as in fromTo(3, 1), the returned value is the empty list.

Besides the above, the following operations are available on lists.

  • length(list): the length of a list. For example, length(["a", "b"]) is 2.
  • item in list: returns true if and only if the element represented by expression item is a member of the list represented by the expression list. For example, "b" in ["a", "b"] returns true.
  • item not in list: returns true if and only if the element represented by expression item is not a member of the list represented by the expression list.
  • distinct(list): returns the unique items in list. In other words, distinct deduplicates the list while preserving the order of the items in the original list.
  • max(list): returns the greatest item in the list, or null if there are no items in the list. The greatest item in a collection is selected with the same order used for comparing items with relational expressions such as less-than (<) and greater-than (>). See Section comparisons for more detail on the order of values.
  • min(list): returns the least item in the list, or null if there are no items in the list. The least item in a collection is selected with the same order used for comparing items with relational expressions such as less-than (<) and greater-than (>). See Section comparisons for more detail on the order of values.
  • maxBy(list, attributeGetter): returns the greatest item in the list, where items are ordered by the value returned by applying attributeGetter to the item. For example, if the following function has been declared getRoutePrefix(route) = route.prefix; // Gets the prefix of a IpEntry route, and if routes is a variable referring to some list of IpEntry records, then maxBy(routes, getRoutePrefix) evaluates to the route with the largest prefix (aka subnet) (see Section comparisons for more on how subnets are ordered). Similar to max and min, this function returns null if there are no items in the list. Note that the second argument to maxBy, called attributeGetter above, must be a function declared within the NQE check.
  • minBy(list, attributeGetter): returns the least item in the list, where items are ordered by the value returned by applying attributeGetter to the item. For example, if the following function has been declared getRoutePrefix(route) = route.prefix; // Gets the prefix of a IpEntry route, and if routes is a variable referring to some list of IpEntry records, then minBy(routes, getRoutePrefix) evaluates to the route with the smallest prefix (aka subnet) (see Section comparisons for more on how subnets are ordered). Similar to max and min, this function returns null if there are no items in the list. Note that the second argument to minBy, called attributeGetter above, must be a function declared within the NQE check.
  • sum(list): The sum of a list of numbers. For example, sum([1, 2, 3]) equals 6.
  • join(delimiter, list): the result of concatenating together the elements in list of type string using delimiter between each element. For example, join("", ["a", "b"]) evaluates to "ab" , join(" ", ["a", "b"]) evaluates to "a b", and join(", ", ["a", "b"]) evaluates to "a, b",