Skip to content
FutureBlitz

    Install

    Import std.collections.array

    std.collections.array

    Searching, sorting, reshaping, and the other Methods every array has.

    What every collection has, on T[], besides the built-in Add, Insert, RemoveAt, Clear, Contains, Clone, Reserve, TrimExcess, Count, and Capacity: searching, the ends, many elements at once, order, and shape. A lookup gives a T?, or a T?? where the elements can be Null, and what's wrong with its arguments panics where it's called. A function given to one that changes the array changes what's left to go over, as in a For ... In, and an element it takes away panics where the Method was called.

    std/collections/array.fb

    Types

    ArrayIterator struct

    Public Struct ArrayIterator<T> Implements Iterator<T>

    An array's elements, in order, as far as its Count when each is read.

    std/collections/array.fb, line 491

    • Public ByRef Method Next(): T? method #

      The next element, in order, or Null after the last.

    Sorting struct

    Public Struct Sorting<T>

    An array SortBy or SortByDescending sorted, whose ThenBy and ThenByDescending sort it again where the keys so far tie, in place.

    Examples

    Local words = ["bb", "a", "ccc", "dd"]
    words.SortBy(Function(w: String) => w.Length).ThenByDescending(Function(w: String) => w)
    Expect(words == ["a", "dd", "bb", "ccc"])

    std/collections/array.fb, line 514

    • @PanicsAtCaller Public Method ThenBy<K: Comparable>(key: Func(T): K): Sorting<T> method #

      Sorts the array again by what key gives, smallest first, where the keys so far tie.

    • @PanicsAtCaller Public Method ThenByDescending<K: Comparable>(key: Func(T): K): Sorting<T> method #

      The same, largest key first.

    Extends

    T[] extend

    Extend T[]

    What every array has besides the built-in Add, Insert, RemoveAt, Clear, Contains, Clone, Reserve, TrimExcess, Count, and Capacity: searching, its ends, many elements at once, sorting, and reshaping.

    std/collections/array.fb, line 16

    • Public Method Remove(value: T): Bool method #

      Removes the first element equal to value, and tells whether there was one.

    • @PanicsAtCaller Public Method Sort() method #

      Sorts the elements in place, smallest first. Equal elements keep their order.

      Examples

      Local numbers = [3, 1, 2]
      numbers.Sort()
      Expect(numbers == [1, 2, 3])
    • @PanicsAtCaller Public Method Sort(before: Func(T, T): Bool) method #

      Sorts the elements in place, by what before says comes first. It's a merge sort, so equal elements keep their order.

      Examples

      Local numbers = [3, 1, 2]
      numbers.Sort(Function(a: Int, b: Int) => a > b)
      Expect(numbers == [3, 2, 1])
    • Public Method Filter(keep: Func(T): Bool): T[] method #

      Each element that keep says to keep, in a new array.

      Examples

      Expect([1, 2, 3, 4].Filter(Function(n: Int) => n % 2 == 0) == [2, 4])
    • Public Method Map<U>(change: Func(T): U): U[] method #

      What change gives for each element, in a new array.

      Examples

      Expect([1, 2, 3].Map(Function(n: Int) => n * 10) == [10, 20, 30])
      Expect([1, 2].Map(Function(n: Int) => "#{n}") == ["#1", "#2"])
    • Public Method Find(test: Func(T): Bool): T? method #

      The first element that test says is the one, or Null.

      Examples

      Expect([5, 8, 12].Find(Function(n: Int) => n > 6) == 8)
      Expect([5, 8, 12].Find(Function(n: Int) => n > 20) == Null)
    • Public Method FindLast(test: Func(T): Bool): T? method #

      The last element that test says is the one, or Null.

    • Public Method FindIndex(test: Func(T): Bool): Int? method #

      Where the first element that test says is the one is, or Null.

    • Public Method IndexOf(value: T): Int? method #

      Where the first element equal to value is, or Null.

      Examples

      Expect(["a", "b", "a"].IndexOf("a") == 0)
      Expect(["a", "b", "a"].IndexOf("c") == Null)
    • Public Method LastIndexOf(value: T): Int? method #

      Where the last element equal to value is, or Null.

    • Public Method BinarySearch(value: T): Int? method #

      Where an element equal to value is in an array sorted smallest first, or Null, looking at about log2(Count) of them.

      Examples

      Local sorted = [1, 3, 5, 7]
      Expect(sorted.BinarySearch(5) == 2)
      Expect(sorted.BinarySearch(4) == Null)
    • Public Method Any(test: Func(T): Bool): Bool method #

      Whether test says yes for any element, going no further than the first.

      Examples

      Expect([1, 2, 3].Any(Function(n: Int) => n > 2))
      Expect(![1, 2, 3].All(Function(n: Int) => n > 2))
    • Public Method All(test: Func(T): Bool): Bool method #

      Whether test says yes for every element, going no further than the first it doesn't.

    • Public Method First(): T? method #

      The first element, or Null when there's none.

    • Public Method Last(): T? method #

      The last element, or Null when there's none.

    • @PanicsAtCaller Public Method RemoveLast(): T method #

      Takes the last element off, and gives it. The array can't be empty.

    • Public Method TryRemoveLast(): T? method #

      Takes the last element off, and gives it, or Null when there's none.

    • Public Method AddAll(values: T[]) method #

      Adds each of the values at the end, in order.

    • Public Method AddAll(values: Iterable<T>) method #

      The same, from any Iterable<T>, all of which is read first.

    • @PanicsAtCaller Public Method InsertAll(index: Int, values: T[]) method #

      Puts the values in, in order, from index on, which can be from 0 to Count.

    • @PanicsAtCaller Public Method InsertAll(index: Int, values: Iterable<T>) method #

      The same, from any Iterable<T>, all of which is read first.

    • @PanicsAtCaller Public Method RemoveRange(start: Int, count: Int) method #

      Takes out count elements, from start on, which must all be in the array.

    • Public Method RemoveAll(test: Func(T): Bool): Int method #

      Takes out each element that test says to, keeping the others in order, and tells how many went. An element test adds is tested too.

      Examples

      Local numbers = [1, 2, 3, 4, 5]
      Expect(numbers.RemoveAll(Function(n: Int) => n % 2 == 1) == 3)
      Expect(numbers == [2, 4])
    • @PanicsAtCaller Public Method Resize(count: Int, fill: T) method #

      Makes the array count elements long, taking elements off the end or adding fill.

    • Public Method Fill(value: T) method #

      Gives every element the value.

    • Public Method Reverse() method #

      Puts the elements in the opposite order, in place.

    • @PanicsAtCaller Public Method Swap(a: Int, b: Int) method #

      Gives each of the two elements the other's place.

    • @PanicsAtCaller Public Method SortBy<K: Comparable>(key: Func(T): K): Sorting<T> method #

      Sorts the elements in place by what key gives for each, smallest first. Equal keys keep their elements' order, and ThenBy on what it gives breaks their ties.

      Examples

      Local people = [("Grace", 36), ("Alan", 41), ("Ada", 36)]
      people.SortBy(Function(p: (String, Int)) => p.1).ThenBy(Function(p: (String, Int)) => p.0)
      Expect(people == [("Ada", 36), ("Grace", 36), ("Alan", 41)])
    • @PanicsAtCaller Public Method SortByDescending<K: Comparable>(key: Func(T): K): Sorting<T> method #

      The same, largest key first.

    • @PanicsAtCaller Public Method SortUnstable() method #

      Sorts the elements in place, smallest first, without making another array: a heap sort, so equal elements may change places.

    • @PanicsAtCaller Public Method SortUnstable(before: Func(T, T): Bool) method #

      Sorts the elements in place, by what before says comes first, without making another array: a heap sort, so equal elements may change places.

    • Public Method Distinct(): T[] method #

      The elements, each only the first time it comes, in a new array.

      Examples

      Expect([1, 2, 1, 3, 2].Distinct() == [1, 2, 3])
    • Public Method Join(separator: String): String method #

      The elements' text, as "{x}" shows each, with separator between each two.

      Examples

      Expect([1, 2, 3].Join(", ") == "1, 2, 3")
    • Public Method Fold<A>(start: A, combine: Func(A, T): A): A method #

      What combine gives for each element in turn, starting from start.

      Examples

      Expect([1, 2, 3].Fold(0, Function(sum: Int, n: Int) => sum + n) == 6)
      Expect([1, 2, 3].Fold("", Function(text: String, n: Int) => text + "{n}") == "123")
    • Public Method Reduce(combine: Func(T, T): T): T? method #

      What combine gives for each element in turn, starting from the first, or Null when there's none.

      Examples

      Expect([3, 7, 5].Reduce(Function(a: Int, b: Int) => Max(a, b)) == 7)
      Local none: Int[] = []
      Expect(none.Reduce(Function(a: Int, b: Int) => a + b) == Null)

    T[] extend

    Extend T[] Implements Iterable<T>

    Makes every array an Iterable<T>, so code that takes any collection takes one.

    std/collections/array.fb, line 485

    • Public Method Iterate(): ArrayIterator<T> method #

      Its elements, in order, which a loop over the array goes through.

    Report an issue with this page or with FutureBlitz on GitHub.