Definitions on Arrays #
This file contains various definitions on Array. It does not contain
proofs about these definitions, those are contained in other files in Batteries.Data.Array.
Check whether xs and ys are equal as sets, i.e. they contain the same
elements when disregarding order and duplicates. O(n*m)! If your element type
has an Ord instance, it is asymptotically more efficient to sort the two
arrays, remove duplicates and then compare them elementwise.
Equations
Instances For
Returns the first minimal element among d and elements of the array.
If start and stop are given, only the subarray xs[start:stop] is
considered (in addition to d).
Equations
Instances For
Find the first minimal element of an array. If the array is empty, d is
returned. If start and stop are given, only the subarray xs[start:stop] is
considered.
Equations
Instances For
Find the first minimal element of an array. If the array is empty, none is
returned. If start and stop are given, only the subarray xs[start:stop] is
considered.
Equations
Instances For
Find the first minimal element of an array. If the array is empty, default is
returned. If start and stop are given, only the subarray xs[start:stop] is
considered.
Instances For
Returns the first maximal element among d and elements of the array.
If start and stop are given, only the subarray xs[start:stop] is
considered (in addition to d).
Instances For
Find the first maximal element of an array. If the array is empty, default is
returned. If start and stop are given, only the subarray xs[start:stop] is
considered.
Instances For
Folds a monadic function over an array from the left, accumulating the partial results starting with
init. The accumulated value is combined with the each element of the list in order, using f.
The optional parameters start and stop control the region of the array to be folded. Folding
proceeds from start (inclusive) to stop (exclusive), so no folding occurs unless start < stop.
By default, the entire array is folded.
Examples:
example [Monad m] (f : α → β → m α) :
Array.scanlM f x₀ #[a, b, c] = (do
let x₁ ← f x₀ a
let x₂ ← f x₁ b
let x₃ ← f x₂ c
pure #[x₀, x₁, x₂, x₃])
:= by simp [scanlM, scanlM.loop]
example [Monad m] (f : α → β → m α) :
Array.scanlM f x₀ #[a, b, c] (start := 1) (stop := 3) = (do
let x₁ ← f x₀ b
let x₂ ← f x₁ c
pure #[x₀, x₁, x₂])
:= by simp [scanlM, scanlM.loop]
Equations
- Array.scanlM f init as start stop = Array.scanlM.loop f init as (min start as.size) (min stop as.size) ⋯ #[]
Instances For
auxiliary tail-recursive function for scanlM
Equations
- One or more equations did not get rendered due to their size.
Instances For
Folds a monadic function over an array from the right, accumulating the partial results starting
with init. The accumulated value is combined with the each element of the list in order using f.
The optional parameters start and stop control the region of the array to be folded. Folding
proceeds from start (exclusive) to stop (inclusive), so no folding occurs unless start > stop.
By default, the entire array is folded.
Examples:
example [Monad m] (f : α → β → m β) :
Array.scanrM f x₀ #[a, b, c] = (do
let x₁ ← f c x₀
let x₂ ← f b x₁
let x₃ ← f a x₂
pure #[x₃, x₂, x₁, x₀])
:= by simp [scanrM, scanrM.loop]
example [Monad m] (f : α → β → m β) :
Array.scanrM f x₀ #[a, b, c] (start := 3) (stop := 1) = (do
let x₁ ← f c x₀
let x₂ ← f b x₁
pure #[x₂, x₁, x₀])
:= by simp [scanrM, scanrM.loop]
Equations
- Array.scanrM f init as start stop = Array.scanrM.loop f init as (min start as.size) stop ⋯ #[]
Instances For
auxiliary tail-recursive function for scanrM
Equations
- One or more equations did not get rendered due to their size.
Instances For
Fold a function f over the array from the left, returning the array of partial results.
scanl (· + ·) 0 #[1, 2, 3] = #[0, 1, 3, 6]
Equations
- Array.scanl f init as start stop = (Array.scanlM (fun (x1 : β) (x2 : α) => pure (f x1 x2)) init as start stop).run
Instances For
Fold a function f over the array from the right, returning the array of partial results.
scanr (· + ·) 0 #[1, 2, 3] = #[6, 5, 3, 0]
Equations
- Array.scanr f init as start stop = (Array.scanrM (fun (x1 : α) (x2 : β) => pure (f x1 x2)) init as start stop).run
Instances For
Fold a monadic function f over the subarray from the left, returning the list of partial results.
Equations
- Subarray.scanlM f init as = Array.scanlM f init as.array as.start as.stop
Instances For
Fold a monadic function f over the subarray from the right, returning the list of partial results.
Equations
- Subarray.scanrM f init as = Array.scanrM f init as.array as.start as.stop
Instances For
Fold a function f over the subarray from the left, returning the list of partial results.
Equations
- Subarray.scanl f init as = Array.scanl f init as.array as.start as.stop
Instances For
Fold a function f over the subarray from the right, returning the list of partial results.
Equations
- Subarray.scanr f init as = Array.scanr f init as.array as.start as.stop
Instances For
Check whether a subarray contains a given element.
Equations
- as.contains a = Subarray.any (fun (x : α) => x == a) as
Instances For
Remove the first element of a subarray. Returns the element and the remaining
subarray, or none if the subarray is empty.
Equations
- One or more equations did not get rendered due to their size.