(656 lines)
4 Performs a binary search on a sorted list.8 Assuming the input list is sorted according to the given comparison function,9 return the index where the given item would be inserted to maintain the sorted10 order. That is, if the item is found, return its index, otherwise return the11 place where it would be found if it were inserted and the list were sorted.16 The sorted list to search.21 The comparison function used to determine order. If not specified, the22 default comparison function for the item type will be used.24 assert [1, 3, 5, 7, 9].binary_search(5) == 325 assert [1, 3, 5, 7, 9].binary_search(-999) == 126 assert [1, 3, 5, 7, 9].binary_search(999) == 631 Creates a new list with elements spaced by the specified step value.35 A new list with every `step`-th element from the original list.40 The original list.44 The step value for selecting elements.46 assert [1, 2, 3, 4, 5, 6].by(2) == [1, 3, 5]51 Clears all elements from the list.55 Nothing.60 The mutable reference to the list to be cleared.62 list := &[10, 20]63 list.clear()64 assert list[] == []69 Counts the occurrences of each element in the list.73 A table mapping each element to its count.78 The list to count elements in.80 assert [10, 20, 30, 30, 30].counts() == {10:1, 20:1, 30:3}85 Finds the index of the first occurrence of an element (if any).89 The index of the first occurrence or `none` if not found.94 The list to search through.98 The item to search for.100 assert [10, 20, 30, 40, 50].find(20) == 2101 assert [10, 20, 30, 40, 50].find(9999) == none106 Returns a slice of the list starting from a specified index.110 A new list starting from the specified index.115 The original list.119 The index to start from.121 assert [10, 20, 30, 40, 50].from(3) == [30, 40, 50]126 Checks if the list has an element.130 `yes` if the list has the element, `no` otherwise.135 The list to check.139 The element to check for.141 assert [10, 20, 30].has(20) == yes146 Removes and returns the top element of a heap or `none` if the list is empty.147 By default, this is the *minimum* value in the heap.151 The removed top element of the heap or `none` if the list is empty.156 The mutable reference to the heap.161 The comparison function used to determine order. If not specified, the162 default comparison function for the item type will be used.164 my_heap := &[30, 10, 20]165 my_heap.heapify()166 assert my_heap.heap_pop() == 10171 Adds an element to the heap and maintains the heap property. By default, this172 is a *minimum* heap.176 Nothing.181 The mutable reference to the heap.185 The item to be added.189 The comparison function used to determine order. If not specified, the190 default comparison function for the item type will be used.192 my_heap : &[Int]193 my_heap.heap_push(10)194 assert my_heap.heap_pop() == 10199 Converts a list into a heap.203 Nothing.208 The mutable reference to the list to be heapified.213 The comparison function used to determine order. If not specified, the214 default comparison function for the item type will be used.216 my_heap := &[30, 10, 20]217 my_heap.heapify()222 Inserts an element at a specified position in the list.226 Nothing.231 The mutable reference to the list.235 The item to be inserted.240 The index at which to insert the item.242 Since indices are 1-indexed and negative indices mean "starting from the243 back", an index of `0` means "after the last item".245 list := &[10, 20]246 list.insert(30)247 assert list == [10, 20, 30]249 list.insert(999, at=2)250 assert list == [10, 999, 20, 30]255 Inserts a list of items at a specified position in the list.259 Nothing.264 The mutable reference to the list.268 The items to be inserted.273 The index at which to insert the item.275 Since indices are 1-indexed and negative indices mean "starting from the276 back", an index of `0` means "after the last item".278 list := &[10, 20]279 list.insert_all([30, 40])280 assert list == [10, 20, 30, 40]282 list.insert_all([99, 100], at=2)283 assert list == [10, 99, 100, 20, 30, 40]288 Removes and returns an item from the list. If the given index is present in289 the list, the item at that index will be removed and the list will become one290 element shorter.294 `none` if the list is empty or the given index does not exist in the list,295 otherwise the item at the given index.300 The list to remove an item from.305 The index from which to remove the item.307 Since negative indices are counted from the back, the default behavior is308 to pop the last value.310 list := &[10, 20, 30, 40]312 assert list.pop() == 40313 assert list[] == [10, 20, 30]315 assert list.pop(index=2) == 20316 assert list[] == [10, 30]321 Selects a random element from the list.325 A random element from the list or `none` if the list is empty.330 The list from which to select a random element.335 If provided, this function will be used to get a random index in the list. Returned336 values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number337 generation)339 nums := [10, 20, 30]340 pick := nums.random()!341 assert nums.has(pick)342 empty : [Int]343 assert empty.random() == none348 Removes elements from the list starting at a specified index.352 Nothing.357 The mutable reference to the list.362 The index at which to start removing elements.367 The number of elements to remove.369 Since negative indices are counted from the back, the default behavior is370 to remove the last item.372 list := &[10, 20, 30, 40, 50]373 list.remove_at(2)374 assert list == [10, 30, 40, 50]376 list.remove_at(2, count=2)377 assert list == [10, 50]382 Removes all occurrences of a specified item from the list.386 Nothing.391 The mutable reference to the list.395 The item to be removed.400 The maximum number of occurrences to remove.402 A negative `max_count` means "remove all occurrences".404 list := &[10, 20, 10, 20, 30]405 list.remove_item(10)406 assert list == [20, 20, 30]408 list.remove_item(20, max_count=1)409 assert list == [20, 30]414 Returns a reversed slice of the list.418 A slice of the list with elements in reverse order.423 The list to be reversed.425 assert [10, 20, 30].reversed() == [30, 20, 10]430 Selects a sample of elements from the list, optionally with weighted431 probabilities.435 A list of sampled elements from the list.437 Errors will be raised if any of the following conditions occurs:438 - The given list has no elements and `count >= 1`439 - `count < 0` (negative count)440 - The number of weights provided doesn't match the length of the list.441 - Any weight in the weights list is negative, infinite, or `NaN`442 - The sum of the given weights is zero (zero probability for every element).447 The list to sample from.451 The number of elements to sample.456 The probability weights for each element in the list. These457 values do not need to add up to any particular number, they are relative458 weights. If no weights are given, elements will be sampled with uniform459 probability.464 If provided, this function will be used to get random values for465 sampling the list. The provided function should return random numbers466 between `0.0` (inclusive) and `1.0` (exclusive). (Used for deterministic467 pseudorandom number generation)469 _ := [10, 20, 30].sample(2, weights=[90%, 5%, 5%]) # E.g. [10, 10]474 Shuffles the elements of the list in place.478 Nothing.483 The mutable reference to the list to be shuffled.488 If provided, this function will be used to get a random index in the list. Returned489 values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number490 generation)492 nums := &[10, 20, 30, 40]493 nums.shuffle()494 # E.g. [20, 40, 10, 30]499 Creates a new list with elements shuffled.503 A new list with shuffled elements.508 The list to be shuffled.513 If provided, this function will be used to get a random index in the list. Returned514 values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number515 generation)517 nums := [10, 20, 30, 40]518 _ := nums.shuffled()519 # E.g. [20, 40, 10, 30]524 Returns a slice of the list spanning the given indices (inclusive).528 A new list spanning the given indices. Note: negative indices are counted from529 the back of the list, so `-1` refers to the last element, `-2` the530 second-to-last, and so on.535 The original list.539 The first index to include.543 The last index to include.545 assert [10, 20, 30, 40, 50].slice(2, 4) == [20, 30, 40]546 assert [10, 20, 30, 40, 50].slice(-3, -2) == [30, 40]551 Sorts the elements of the list in place in ascending order (small to large).555 Nothing.560 The mutable reference to the list to be sorted.564 The comparison function used to determine order. If not specified, the565 default comparison function for the item type will be used.567 list := &[40, 10, -30, 20]568 list.sort()569 assert list == [-30, 10, 20, 40]571 list.sort(func(a,b:&Int) a.abs() <> b.abs())572 assert list == [10, 20, -30, 40]577 Creates a new list with elements sorted.581 A new list with sorted elements.586 The list to be sorted.590 The comparison function used to determine order. If not specified, the591 default comparison function for the item type will be used.593 assert [40, 10, -30, 20].sorted() == [-30, 10, 20, 40]594 assert [40, 10, -30, 20].sorted(595 func(a,b:&Int) a.abs() <> b.abs()596 ) == [10, 20, -30, 40]601 Returns a slice of the list from the start of the original list up to a specified index (inclusive).605 A new list containing elements from the start up to the specified index.610 The original list.614 The index up to which elements should be included.616 assert [10, 20, 30, 40, 50].to(3) == [10, 20, 30]617 assert [10, 20, 30, 40, 50].to(-2) == [10, 20, 30, 40]622 Returns a set of the unique elements of the list.626 A set of the unique elements from the list.631 The list to process.633 assert [10, 20, 10, 10, 30].unique() == {10, 20, 30}638 Find the index of the first item that matches a predicate function (if any).642 Returns the index of the first item where the predicate is true or `none` if no643 item matches.648 The list to search through.652 A function that returns `yes` if the item's index should be returned or653 `no` if it should not.655 assert ["BC", "ABC", "CD"].where(func(t:&Text) t.starts_with("A")) == 2656 assert ["BC", "ABC", "CD"].where(func(t:&Text) t.starts_with("X")) == none