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
Computes the size of the subarray.
Equations
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.
Equations
Instances For
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.
Equations
Instances For
Shrinks the subarray by incrementing its starting index if possible, returning it unchanged if not.
Examples:
#[1,2,3].toSubarray.popFront.toArray = #[2, 3]
#[1,2,3].toSubarray.popFront.popFront.toArray = #[3]
#[1,2,3].toSubarray.popFront.popFront.popFront.toArray = #[]
#[1,2,3].toSubarray.popFront.popFront.popFront.popFront.toArray = #[]
Equations
Instances For
The empty subarray.
This empty subarray is backed by an empty array.
Equations
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Instances For
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
Equations
Instances For
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
Equations
Instances For
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
Instances For
Allocates a new array that contains the contents of the subarray.
Equations
Instances For
A subarray with the provided bounds.
Equations
Instances For
A subarray with the provided lower bound that extends to the rest of the array.
Equations
Instances For
A subarray with the provided upper bound, starting at the index 0.
Equations
Instances For
Allocates a new array that contains the contents of the subarray.