Functions for lists

ListCreate

Construct an empty list. The only argument specifies a string describing the data type of the list cell, or the type itself obtained using relevant functions. YQL doesn’t support lists with an unknown cell type.

Documentation for the type definition format.

Examples

  1. SELECT ListCreate(Tuple<String,Double?>);

For lists - 图1

  1. SELECT ListCreate(OptionalType(DataType("String")));

For lists - 图2

AsList and AsListStrict

Construct a list based on one or more arguments. The argument types must be compatible in the case of AsList and strictly match in the case of AsListStrict.

Examples

  1. SELECT AsList(1, 2, 3, 4, 5);

For lists - 图3

ListLength

The count of items in the list.

Examples

ListHasItems

Check that the list contains at least one item.

Examples

ListCollect

Convert a lazy list (it can be built by such functions as ListFilter, ListMap, ListFlatMap) to an eager list. In contrast to a lazy list, where each new pass re-calculates the list contents, in an eager list the content is built at once by consuming more memory.

Examples

ListSort, ListSortAsc, and ListSortDesc

Sort the list. By default, the ascending sorting order is applied (ListSort is an alias for ListSortAsc).

Arguments:

  1. List.
  2. An optional expression to get the sort key from a list element (it’s the element itself by default).

Examples

  1. $list = AsList(
  2. AsTuple("x", 3),
  3. AsTuple("xx", 1),
  4. AsTuple("a", 2)
  5. );
  6. SELECT ListSort($list, ($x) -> {
  7. RETURN $x.1;
  8. });

For lists - 图4

Note

The example used a lambda function.

ListExtend and ListExtendStrict

Sequentially join lists (concatenation of lists). The arguments can be lists, optional lists, and NULL.
The types of list items must be compatible in the case of ListExtend and strictly match in the case of ListExtendStrict.
If at least one of the lists is optional, then the result is also optional.
If at least one argument is NULL, then the result type is NULL.

Examples

ListUnionAll

Sequentially join lists of structures (concatenation of lists). A field is added to the output list of structures if it exists in at least one source list, but if there is no such field in any list, it is added as NULL. In the case when a field is present in two or more lists, the output field is cast to the common type.

If at least one of the lists is optional, then the result is also optional.

Examples

ListZip and ListZipAll

Based on the input lists, build a list of pairs containing the list items with matching indexes (List<Tuplefirst_list_element_type,second_list_element_type>).

The length of the returned list is determined by the shortest list for ListZip and the longest list for ListZipAll.
When the shorter list is exhausted, a NULL value of a relevant optional type is paired with the elements of the longer list.

Examples

ListEnumerate

Build a list of pairs (Tuple) containing the element number and the element itself (List<TupleUint64,list_element_type>).

Examples

ListReverse

Reverse the list.

Examples

ListSkip

Returns a copy of the list, skipping the specified number of its first elements.

The first argument specifies the source list and the second argument specifies how many elements to skip.

Examples

ListTake

Returns a copy of the list containing a limited number of elements from the second list.

The first argument specifies the source list and the second argument specifies the maximum number of elements to be taken from the beginning of the list.

Examples

ListIndexOf

Searches the list for an element with the specified value and returns its index at the first occurrence. Indexes count from 0. If such element is missing, it returns NULL.

Examples

ListMap, ListFilter, and ListFlatMap

Apply the function specified as the second argument to each list element. The functions differ in their returned result:

  • ListMap returns a list with results.
  • ListFlatMap returns a list with results, combining and expanding the first level of results (lists or optional values) for each item.
  • ListFilter leaves only those elements where the function returned true.

Note

In ListFlatMap, using optional values in function results is deprecated, use the combination of ListNotNull and ListMap instead.

Arguments:

  1. Source list.
  2. Functions for processing list elements, such as:

ListNotNull

Applies transformation to the source list, skipping empty optional items and strengthening the item type to non-optional. For a list with non-optional items, it returns the unchanged source list.

If the source list is optional, then the output list is also optional.

Examples

  1. SELECT ListNotNull([1,2]), -- [1,2]
  2. ListNotNull([3,null,4]); -- [3,4]

For lists - 图5

ListFlatten

Expands the list of lists into a flat list, preserving the order of items. As the top-level list item you can use an optional list that is interpreted as an empty list in the case of NULL.

If the source list is optional, then the output list is also optional.

Examples

  1. SELECT ListFlatten([[1,2],[3,4]]), -- [1,2,3,4]
  2. ListFlatten([null,[3,4],[5,6]]); -- [3,4,5,6]

For lists - 图6

ListUniq

Returns a copy of the list containing only distinct elements.

Examples

ListAny and ListAll

Returns true for a list of Boolean values, if:

  • ListAny: At least one element is true.
  • ListAll: All elements are true.

Otherwise, it returns false.

Examples

ListHas

Show whether the list contains the specified element.

Examples

ListHead, ListLast

Returns the first and last item of the list.

Examples

ListMin, ListMax, ListSum and ListAvg

Apply the appropriate aggregate function to all elements of the numeric list.

Examples

ListFold, ListFold1

Folding a list.

Arguments:

  1. List
  2. Initial state U for ListFold, initLambda(item:T)->U for ListFold1
  3. updateLambda(item:T, state:U)->U

Type returned:
U for ListFold, optional U for ListFold1.

Examples

  1. $l = [1, 4, 7, 2];
  2. $y = ($x, $y) -> { RETURN $x + $y; };
  3. $z = ($x) -> { RETURN 4 * $x; };
  4. SELECT
  5. ListFold($l, 6, $y) AS fold, -- 20
  6. ListFold([], 3, $y) AS fold_empty, -- 3
  7. ListFold1($l, $z, $y) AS fold1, -- 17
  8. ListFold1([], $z, $y) AS fold1_empty; -- Null

For lists - 图7

ListFoldMap, ListFold1Map

Converts each list item i by calling the handler(i, state).

Arguments:

  1. List
  2. Initial state S for ListFoldMap, initLambda(item:T)->tuple (U S) for ListFold1Map
  3. handler(item:T, state:S)->tuple (U S)

Type returned: List of U items.

Examples

  1. $l = [1, 4, 7, 2];
  2. $x = ($i, $s) -> { RETURN ($i * $s, $i + $s); };
  3. $t = ($i) -> { RETURN ($i + 1, $i + 2); };
  4. SELECT
  5. ListFoldMap([], 1, $x), -- []
  6. ListFoldMap($l, 1, $x), -- [1, 8, 42, 26]
  7. ListFold1Map([], $t, $x), -- []
  8. ListFold1Map($l, $t, $x); -- [2, 12, 49, 28]

For lists - 图8

ListFromRange

Generate a sequence of numbers with the specified step. It’s similar to xrange in Python 2, but additionally supports floats.

Arguments:

  1. Start
  2. End
  3. Step (optional, 1 by default)

Specifics:

  • The end is not included, i.e. ListFromRange(1,3) == AsList(1,2).
  • The type for the resulting elements is selected as the broadest from the argument types. For example, ListFromRange(1, 2, 0.5) results in a Double list.
  • The list is “lazy”, but if it’s used incorrectly, it can still consume a lot of RAM.
  • If the step is positive and the end is less than or equal to the start, the result list is empty.
  • If the step is negative and the end is greater than or equal to the start, the result list is empty.
  • If the step is neither positive nor negative (0 or NaN), the result list is empty.

Examples

  1. SELECT
  2. ListFromRange(-2, 2), -- [-2, -1, 0, 1]
  3. ListFromRange(2, 1, -0.5); -- [2.0, 1.5]

For lists - 图9

ListReplicate

Creates a list containing multiple copies of the specified value.

Required arguments:

  1. Value.
  2. Number of copies.

Examples

  1. SELECT ListReplicate(true, 3); -- [true, true, true]

For lists - 图10

ListConcat

Concatenates a list of strings into a single string.
You can set a separator as the second parameter.

Examples

ListExtract

For a list of structures, it returns a list of contained fields having the specified name.

Examples

ListTakeWhile, ListSkipWhile

ListTakeWhile returns a list from the beginning while the predicate is true, then the list ends.

ListSkipWhile skips the list segment from the beginning while the predicate is true, then returns the rest of the list ignoring the predicate.
ListTakeWhileInclusive returns a list from the beginning while the predicate is true. Then the list ends, but it also includes the item on which the stopping predicate triggered.
ListSkipWhileInclusive skips a list segment from the beginning while the predicate is true, then returns the rest of the list disregarding the predicate, but excluding the element that matched the predicate and starting with the next element after it.

Required arguments:

  1. List.
  2. Predicate.

If the input list is optional, then the result is also optional.

Examples

  1. $data = AsList(1, 2, 5, 1, 2, 7);
  2. SELECT
  3. ListTakeWhile($data, ($x) -> {return $x <= 3}), -- [1, 2]
  4. ListSkipWhile($data, ($x) -> {return $x <= 3}), -- [5, 1, 2, 7]
  5. ListTakeWhileInclusive($data, ($x) -> {return $x <= 3}), -- [1, 2, 5]
  6. ListSkipWhileInclusive($data, ($x) -> {return $x <= 3}); -- [1, 2, 7]

For lists - 图11

ListAggregate

Apply the aggregation factory to the passed list.
If the passed list is empty, the aggregation result is the same as for an empty table: 0 for the COUNT function and NULL for other functions.
If the passed list is optional and NULL, the result is also NULL.

Arguments:

  1. List.
  2. Aggregation factory.

Examples

  1. SELECT ListAggregate(AsList(1, 2, 3), AggregationFactory("Sum")); -- 6

For lists - 图12

ToDict and ToMultiDict

Convert a list of tuples containing key-value pairs to a dictionary. In case of conflicting keys in the input list, ToDict leaves the first value and ToMultiDict builds a list of all the values.

It means that:

  • ToDict converts List<TupleK, V=""> to Dict<K, V="">
  • ToMultiDict converts List<TupleK, V> to Dict<K, List<V>>

Optional lists are also supported, resulting in an optional dictionary.

Examples

ToSet

Converts a list to a dictionary where the keys are unique elements of this list, and values are omitted and have the type Void. For the List<T> list, the result type is Dict<T, Void="">.
An optional list is also supported, resulting in an optional dictionary.

Inverse function: get a list of keys for the DictKeys dictionary.

Examples