Standard Library
Collection Functions
Functions for working with lists and maps
Collection Functions
Functions for manipulating lists and maps. These functions are available globally.
List Functions
reverse
Return a new list with elements in reverse order.
Syntax: call reverse with list
Example:
set original to list 1, 2, 3, 4, 5
set reversed to call reverse with original # Returns [5, 4, 3, 2, 1]Note: Returns a new list; does not modify the original.
sort
Return a new sorted list.
Syntax: call sort with list
Example:
set unsorted to list 3, 1, 4, 1, 5
set sorted to call sort with unsorted # Returns [1, 1, 3, 4, 5]
set words to list "zebra", "apple", "banana"
set sorted_words to call sort with words # Returns ["apple", "banana", "zebra"]Note:
- Returns a new list; does not modify the original
- Works with numbers or strings, but all elements must be the same type
- Sorting is done in ascending order
Length
len
Get the length of a list or map.
Syntax: call len with collection
Example:
set numbers to list 1, 2, 3, 4, 5
set length to call len with numbers # Returns 5Examples
Sorting Numbers
set scores to list 85, 92, 78, 96, 88
set sorted_scores to call sort with scores
print "Original:"
print scores
print "Sorted:"
print sorted_scoresReversing a List
set items to list "first", "second", "third"
set reversed_items to call reverse with items
for item in reversed_items:
print item # Prints: third, second, firstFinding Maximum Value
set numbers to list 3, 7, 2, 9, 1, 5
# Sort and get last element
set sorted_nums to call sort with numbers
set length to call len with sorted_nums
set max_val to sorted_nums at (length minus 1)
print max_val # Prints: 9Working with Sorted Data
set data to list 42, 15, 8, 23, 4, 16
set sorted_data to call sort with data
print "Sorted data:"
for num in sorted_data:
print num
# Get median (middle value)
set length to call len with sorted_data
set middle_index to length divided by 2
set median to sorted_data at middle_index
print f"Median: {median}"Combining List Operations
set original to list 5, 2, 8, 1, 9, 3
# Sort first
set sorted to call sort with original
# Then reverse to get descending order
set descending to call reverse with sorted
print "Ascending:"
print sorted
print "Descending:"
print descending