LEARN VOID

Collection helpers

Transform and search lists while keeping the original ordering available.

collection-tools.voidDownload source
use collections

func entry(last_stop: i32) -> i32:
    var stops: [i32] = [12, 8, last_stop]
    var newest = collections.reversed(stops)
    return newest[0]
Input 21Expected result 21

Get a fresh list

With use collections, reversed returns a fresh outer list. Our delivery queue ends with the newest stop. Reading the first element of its reversed copy returns that stop without changing the queue.

Sort and search

Pass the same ordering to sorted and binary_search. Searching requires an already sorted list. Match the search result to distinguish a missing value from a valid index of zero.

Understand copies

Reordering creates new list storage. If elements reference managed objects, the copies still refer to those objects. For insertion-ordered key/value lookup, see Dictionaries.

Continue learning with Dictionaries · Generics.