Skip to content

Lists

Lists in RuLA are ordered collections of values. They can contain any type of value: strings, numbers, other lists, or dictionaries.

Creating Lists

Use square brackets with comma-separated values:

['apple', 'banana', 'cherry']           # List of strings
[1, 2, 3, 4, 5]                          # List of numbers
[]                                       # Empty list
['text', 42, {'key': 'value'}]          # Mixed types

Accessing List Elements

Access elements by index (0-based):

fruits = ['apple', 'banana', 'cherry']
fruits[0]           # Returns 'apple'
fruits[2]           # Returns 'cherry'

List Manipulation

Adding Elements

APPEND([1, 2], 3)             # Returns [1, 2, 3]
CONCAT([1, 2], [3, 4])        # Concatenation: [1, 2, 3, 4]

Filtering

numbers = [1, 2, 3, 4, 5]
FILTER(numbers, x => x > 3)             # Returns [4, 5]
FILTER_FALSE(true, false, true, false)  # Returns [true, true]

Transforming (Mapping)

numbers = [1, 2, 3]
MAP(numbers, x => x * 2)        # Returns [2, 4, 6]

Sorting and Reversing

SORT([3, 1, 2])                 # Returns [1, 2, 3]
SORT(['banana', 'apple'])       # Returns ['apple', 'banana']
REVERSE([1, 2, 3])              # Returns [3, 2, 1]

Getting Unique Elements

SET([1, 2, 2, 3, 3, 3])         # Returns [1, 2, 3]

Flattening Nested Lists

FLATTEN([[1, 2], [3, 4]])       # Returns [1, 2, 3, 4]

Useful List Functions

Function Description Example
COUNT Number of elements COUNT([1, 2, 3]) → 3
SORT Sort in ascending order SORT([3, 1, 2]) → [1, 2, 3]
REVERSE Reverse the order REVERSE([1, 2, 3]) → [3, 2, 1]
FIRST_ELEMENT Get first element FIRST_ELEMENT([1, 2, 3]) → 1
LAST_ELEMENT Get last element LAST_ELEMENT([1, 2, 3]) → 3
APPEND Add element to end APPEND([1, 2], 3) → [1, 2, 3]
INDEX_OF Find position (0-based) INDEX_OF(['a', 'b'], 'b') → 1
SET Remove duplicates SET([1, 1, 2]) → [1, 2]
FLATTEN Flatten one level FLATTEN([[1], [2]]) → [1, 2]
UNION(list1, list2) Combine unique elements UNION([1, 2], [2, 3]) → [1, 2, 3]
INTERSECTION Common elements INTERSECTION([1, 2], [2, 3]) → [2]
SET_DIFFERENCE Elements in list1 not in list2 SET_DIFFERENCE([1, 2], [2, 3]) → [1]
SUM Sum of numbers SUM([1, 2, 3]) → 6
AVG Average of numbers AVG([1, 2, 3]) → 2
MIN Minimum value MIN([3, 1, 2]) → 1
MAX Maximum value MAX([3, 1, 2]) → 3
CONCAT Join all strings CONCAT(['a', 'b']) → "ab"
JOIN Join with separator JOIN(['a', 'b'], '-') → "a-b"
MAP Transform each element MAP([1, 2], x => x * 2) → [2, 4]
FILTER Keep matching elements FILTER([1, 2, 3], x => x > 1) → [2, 3]
ALL All values true? ALL([true, true, false]) → False
ANY Any value true? ANY([true, true, false]) → True
SOME Any value true? (alias for ANY) SOME([true, false]) → True
CUSTOM_MAX Max element ranked by an ordering list CUSTOM_MAX([red, green], [green, red]) → red

Checking List Contents

fruits = ['apple', 'banana']

fruits contains 'apple'                  # True
fruits contains any ['apple', 'pear']    # True
fruits contains only ['apple', 'banana'] # True

'apple' in fruits               # True
'pear' not in fruits            # True

List Operations on Aggregated Data

# Get all values of an information across instances
all_prices = LIST_OF_ANY(order.line_item.price)
SUM(all_prices)
SORT(all_prices)