A region of some underlying array.
A subarray contains an array together with the start and end indices of a region of interest.
Subarrays can be used to avoid copying or allocating space, while being more convenient than
tracking the bounds by hand. The region of interest consists of every index that is both greater
than or equal to start
and strictly less than stop
.
- array : Array α
The underlying array.
- start : Nat
The starting index of the region of interest (inclusive).
- stop : Nat
The ending index of the region of interest (exclusive).
The starting index is no later than the ending index.
The ending index is exclusive. If the starting and ending indices are equal, then the subarray is empty.
The stopping index is no later than the end of the array.
The ending index is exclusive. If it is equal to the size of the array, then the last element of the array is in the subarray.
Instances For
Extracts an element from the subarray, or returns a default value v₀
when the index is out of
bounds.
The index is relative to the start and end of the subarray, rather than the underlying array.
Extracts an element from the subarray, or returns a default value when the index is out of bounds.
The index is relative to the start and end of the subarray, rather than the underlying array. The
default value is that provided by the Inhabited α
instance.
Shrinks the subarray by incrementing its starting index if possible, returning it unchanged if not.
Examples:
The empty subarray.
This empty subarray is backed by an empty array.
Equations
- Subarray.empty = { array := #[], start := 0, stop := 0, start_le_stop := Subarray.empty.proof_1, stop_le_array_size := Subarray.empty.proof_1 }
Equations
- Subarray.instEmptyCollection = { emptyCollection := Subarray.empty }
Equations
- Subarray.instInhabited = { default := ∅ }
The run-time implementation of ForIn.forIn
for Subarray
, which allows it to be used with for
loops in do
-notation.
This definition replaces Subarray.forIn
.
Equations
- s.forInUnsafe b f = Subarray.forInUnsafe.loop s f (USize.ofNat s.stop) (USize.ofNat s.start) b
Equations
- Subarray.instForIn = { forIn := fun {β : Type ?u.15} [Monad m] => Subarray.forIn }
Folds a monadic operation from left to right over the elements in a subarray.
An accumulator of type β
is constructed by starting with init
and monadically combining each
element of the subarray with the current accumulator value in turn. The monad in question may permit
early termination or repetition.
Examples:
#eval #["red", "green", "blue"].toSubarray.foldlM (init := "") fun acc x => do
let l ← Option.guard (· ≠ 0) x.length
return s!"{acc}({l}){x} "
some "(3)red (5)green (4)blue "
#eval #["red", "green", "blue"].toSubarray.foldlM (init := 0) fun acc x => do
let l ← Option.guard (· ≠ 5) x.length
return s!"{acc}({l}){x} "
none
Equations
- Subarray.foldlM f init as = Array.foldlM f init as.array as.start as.stop
Folds a monadic operation from right to left over the elements in a subarray.
An accumulator of type β
is constructed by starting with init
and monadically combining each
element of the subarray with the current accumulator value in turn, moving from the end to the
start. The monad in question may permit early termination or repetition.
Examples:
#eval #["red", "green", "blue"].toSubarray.foldrM (init := "") fun x acc => do
let l ← Option.guard (· ≠ 0) x.length
return s!"{acc}({l}){x} "
some "(4)blue (5)green (3)red "
#eval #["red", "green", "blue"].toSubarray.foldrM (init := 0) fun x acc => do
let l ← Option.guard (· ≠ 5) x.length
return s!"{acc}({l}){x} "
none
Equations
- Subarray.foldrM f init as = Array.foldrM f init as.array as.stop as.start
Checks whether any of the elements in a subarray satisfy a monadic Boolean predicate.
The elements are tested starting at the lowest index and moving up. The search terminates as soon as an element that satisfies the predicate is found.
Example:
#eval #["red", "green", "blue", "orange"].toSubarray.popFront.anyM fun x => do
IO.println x
pure (x == "blue")
green
blue
true
Equations
- Subarray.anyM p as = Array.anyM p as.array as.start as.stop
Checks whether all of the elements in a subarray satisfy a monadic Boolean predicate.
The elements are tested starting at the lowest index and moving up. The search terminates as soon as an element that does not satisfy the predicate is found.
Example:
#eval #["red", "green", "blue", "orange"].toSubarray.popFront.allM fun x => do
IO.println x
pure (x.length == 5)
green
blue
false
Equations
- Subarray.allM p as = Array.allM p as.array as.start as.stop
Runs a monadic action on each element of a subarray.
The elements are processed starting at the lowest index and moving up.
Equations
- Subarray.forM f as = Array.forM f as.array as.start as.stop
Runs a monadic action on each element of a subarray, in reverse order.
The elements are processed starting at the highest index and moving down.
Equations
- Subarray.forRevM f as = Array.forRevM f as.array as.stop as.start
Folds an operation from left to right over the elements in a subarray.
An accumulator of type β
is constructed by starting with init
and combining each
element of the subarray with the current accumulator value in turn.
Examples:
#["red", "green", "blue"].toSubarray.foldl (· + ·.length) 0 = 12
#["red", "green", "blue"].toSubarray.popFront.foldl (· + ·.length) 0 = 9
Equations
- Subarray.foldl f init as = (Subarray.foldlM f init as).run
Folds an operation from right to left over the elements in a subarray.
An accumulator of type β
is constructed by starting with init
and combining each element of the
subarray with the current accumulator value in turn, moving from the end to the start.
Examples:
#eval #["red", "green", "blue"].toSubarray.foldr (·.length + ·) 0 = 12
#["red", "green", "blue"].toSubarray.popFront.foldlr (·.length + ·) 0 = 9
Equations
- Subarray.foldr f init as = (Subarray.foldrM f init as).run
Checks whether any of the elements in a subarray satisfy a Boolean predicate.
The elements are tested starting at the lowest index and moving up. The search terminates as soon as an element that satisfies the predicate is found.
Equations
- Subarray.any p as = (Subarray.anyM p as).run
Checks whether all of the elements in a subarray satisfy a Boolean predicate.
The elements are tested starting at the lowest index and moving up. The search terminates as soon as an element that does not satisfy the predicate is found.
Equations
- Subarray.all p as = (Subarray.allM p as).run
Applies a monadic function to each element in a subarray in reverse order, stopping at the first
element for which the function succeeds by returning a value other than none
. The succeeding value
is returned, or none
if there is no success.
Example:
#eval #["red", "green", "blue"].toSubarray.findSomeRevM? fun x => do
IO.println x
return Option.guard (· = 5) x.length
blue
green
some 5
Equations
- as.findSomeRevM? f = Subarray.findSomeRevM?.find as f as.size ⋯
Equations
- Subarray.findSomeRevM?.find as f 0 x_2 = pure none
- Subarray.findSomeRevM?.find as f i.succ h = do let r ← f as[i] match r with | some val => pure r | none => let_fun this := ⋯; Subarray.findSomeRevM?.find as f i this
Applies a monadic Boolean predicate to each element in a subarray in reverse order, stopping at the
first element that satisfies the predicate. The element that satisfies the predicate is returned, or
none
if no element satisfies it.
Example:
#eval #["red", "green", "blue"].toSubarray.findRevM? fun x => do
IO.println x
return (x.length = 5)
blue
green
some 5
Tests each element in a subarray with a Boolean predicate in reverse order, stopping at the first
element that satisfies the predicate. The element that satisfies the predicate is returned, or
none
if no element satisfies the predicate.
Examples:
#["red", "green", "blue"].toSubarray.findRev? (·.length ≠ 4) = some "green"
#["red", "green", "blue"].toSubarray.findRev? (fun _ => true) = some "blue"
#["red", "green", "blue"].toSubarray 0 0 |>.findRev? (fun _ => true) = none
Returns a subarray of an array, with the given bounds.
If start
or stop
are not valid bounds for a subarray, then they are clamped to array's size.
Additionally, the starting index is clamped to the ending index.
Equations
- One or more equations did not get rendered due to their size.
Allocates a new array that contains the contents of the subarray.
Equations
- One or more equations did not get rendered due to their size.
Equations
- Array.instCoeSubarray = { coe := Array.ofSubarray }
A subarray with the provided bounds.
Equations
- One or more equations did not get rendered due to their size.
A subarray with the provided lower bound that extends to the rest of the array.
Equations
- One or more equations did not get rendered due to their size.
A subarray with the provided upper bound, starting at the index 0.
Equations
- One or more equations did not get rendered due to their size.
Equations
- instAppendSubarray = { append := fun (x y : Subarray α) => let a := x.toArray ++ y.toArray; a.toSubarray }
Equations
- instReprSubarray = { reprPrec := fun (s : Subarray α) (x : Nat) => repr s.toArray ++ Std.Format.text ".toSubarray" }