Import std.collections.array
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
-
Removes the first element equal to
value, and tells whether there was one. -
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]) -
Sorts the elements in place, by what
beforesays 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]) -
Each element that
keepsays to keep, in a new array.Examples
Expect([1, 2, 3, 4].Filter(Function(n: Int) => n % 2 == 0) == [2, 4]) -
What
changegives 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"]) -
The first element that
testsays 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) -
The last element that
testsays is the one, or Null. -
Where the first element that
testsays is the one is, or Null. -
Where the first element equal to
valueis, or Null.Examples
Expect(["a", "b", "a"].IndexOf("a") == 0) Expect(["a", "b", "a"].IndexOf("c") == Null) -
Where the last element equal to
valueis, or Null. -
Where an element equal to
valueis 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) -
Whether
testsays 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)) -
Whether
testsays yes for every element, going no further than the first it doesn't. -
The first element, or Null when there's none.
-
The last element, or Null when there's none.
-
Takes the last element off, and gives it. The array can't be empty.
-
Takes the last element off, and gives it, or Null when there's none.
-
Adds each of the
valuesat the end, in order. -
The same, from any
Iterable<T>, all of which is read first. -
Puts the
valuesin, in order, fromindexon, which can be from 0 to Count. -
The same, from any
Iterable<T>, all of which is read first. -
Takes out
countelements, fromstarton, which must all be in the array. -
Takes out each element that
testsays to, keeping the others in order, and tells how many went. An elementtestadds 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]) -
Makes the array
countelements long, taking elements off the end or addingfill. -
Gives every element the
value. -
Puts the elements in the opposite order, in place.
-
Gives each of the two elements the other's place.
-
Sorts the elements in place by what
keygives 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)]) -
The same, largest
keyfirst. -
Sorts the elements in place, smallest first, without making another array: a heap sort, so equal elements may change places.
-
Sorts the elements in place, by what
beforesays comes first, without making another array: a heap sort, so equal elements may change places. -
The elements, each only the first time it comes, in a new array.
Examples
Expect([1, 2, 1, 3, 2].Distinct() == [1, 2, 3]) -
The elements' text, as "{x}" shows each, with
separatorbetween each two.Examples
Expect([1, 2, 3].Join(", ") == "1, 2, 3") -
What
combinegives for each element in turn, starting fromstart.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") -
What
combinegives 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)
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
-
Its elements, in order, which a loop over the array goes through.
Report an issue with this page or with FutureBlitz on GitHub.