Container Operations
Built-in functions for inspecting and transforming containers such as lists, maps, strings, and byte sequences.
len
Return the length of a string, list, map, or byte sequence.
len("hello") // 5
len([1, 2, 3]) // 3
len({a: 1, b: 2}) // 2
len(bytes("hi")) // 2keys
Return the keys of a map or the indices of a list.
keys({a: 1, b: 2}) // ["a", "b"]
keys([10, 20, 30]) // [0, 1, 2]sorted
Return a sorted copy of a list. An optional comparator function can be provided
to control the sort order. The comparator receives two elements and returns
true if the first should come before the second.
sorted([3, 1, 2]) // [1, 2, 3]
sorted(["banana", "apple", "cherry"]) // ["apple", "banana", "cherry"]
// Sort by string length using a comparator
sorted(["python", "go", "js"], (a, b) => len(a) < len(b))
// ["go", "js", "python"]The original list is not modified.
reversed
Return a reversed copy of a list or string.
reversed([1, 2, 3]) // [3, 2, 1]
reversed("hello") // "olleh"The original value is not modified.
filter
Return a new list containing only the elements for which the given function returns true.
filter([1, 2, 3, 4, 5], x => x > 3) // [4, 5]
filter(["go", "", "risor"], x => len(x) > 0) // ["go", "risor"]This is equivalent to calling the .filter() method on a list:
[1, 2, 3, 4, 5].filter(x => x > 3) // [4, 5]chunk
Split a list into smaller lists of a given size. The last chunk may contain fewer elements.
chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4], 4) // [[1, 2, 3, 4]]
chunk([1, 2, 3], 1) // [[1], [2], [3]]range
Generate a lazy integer sequence. Supports one, two, or three arguments.
// range(stop) -- from 0 up to (not including) stop
list(range(5)) // [0, 1, 2, 3, 4]
// range(start, stop) -- from start up to (not including) stop
list(range(2, 6)) // [2, 3, 4, 5]
// range(start, stop, step) -- with a custom step
list(range(0, 10, 3)) // [0, 3, 6, 9]
list(range(10, 0, -2)) // [10, 8, 6, 4, 2]Because range returns a lazy iterator, you must call list() to materialize
it into a list. You can also use it directly with the pipe operator:
range(10) |> filter(x => x % 2 == 0) |> list // [0, 2, 4, 6, 8]