Sublist macro

Learn how to select a range of elements from a collection.

This macro selects a range of elements from a collection.

You select a range by specifying a start index and an optional end index. The output is a new collection consisting of elements within the specified range. If you do not specify an end index, the result will include all elements from the start index to the end of the collection.

Although this macro is mainly intended for lists, you can also use it with maps. In this case, the keys and order are preserved in the output.

Syntax

The full syntax for this macro is as follows:

sublist of /*<collection>*/ from /*<start>*/ to /*<end>*/
Try in Playground

To omit the end index, use the following syntax:

sublist of /*<collection>*/ from /*<start>*/
Try in Playground

You can also prefix the macro with a definite article for readability:

the sublist of /*<collection>*/ from /*<start>*/ to /*<end>*/
Try in Playground

Parameters

The following list describes the supported parameters:

collection

The collection from which you want to select a range of elements.

start

The zero-based index of the first element in the range, inclusive.

end(optional)

The zero-based index of the last element in the range, exclusive.

If you omit this parameter, the result will include all elements from the start index to the end of the collection.

Examples

Here is an example of how to get a range of elements from a list:

sublist of [1, 2, 3, 4, 5] from 1 to 3 // [2, 3]
Try in Playground

You can also get a range of elements from a map:

sublist of ["a": 1, "b": 2, "c": 3] from 1 to 2 // ["b": 2]
Try in Playground

To get all elements from a given index, omit the end index:

sublist of [1, 2, 3, 4, 5] from 2 // [3, 4, 5]
Try in Playground