Operations using indexes #
mapIdx #
@[inline]
Given a list as = [a₀, a₁, ...]
function f : Fin as.length → α → β
, returns the list
[f 0 a₀, f 1 a₁, ...]
.
Equations
- as.mapFinIdx f = List.mapFinIdx.go as f as #[] ⋯
Instances For
@[specialize #[]]
def
List.mapFinIdx.go
{α : Type u_1}
{β : Type u_2}
(as : List α)
(f : Fin as.length → α → β)
(bs : List α)
(acc : Array β)
:
Auxiliary for mapFinIdx
:
mapFinIdx.go [a₀, a₁, ...] acc = acc.toList ++ [f 0 a₀, f 1 a₁, ...]
Equations
- List.mapFinIdx.go as f [] x x_1 = x.toList
- List.mapFinIdx.go as f (a :: as_1) x x_1 = List.mapFinIdx.go as f as_1 (x.push (f ⟨x.size, ⋯⟩ a)) ⋯
Instances For
@[inline]
Given a function f : Nat → α → β
and as : List α
, as = [a₀, a₁, ...]
, returns the list
[f 0 a₀, f 1 a₁, ...]
.
Equations
- List.mapIdx f as = List.mapIdx.go f as #[]
Instances For
@[specialize #[]]
Auxiliary for mapIdx
:
mapIdx.go [a₀, a₁, ...] acc = acc.toList ++ [f acc.size a₀, f (acc.size + 1) a₁, ...]
Equations
- List.mapIdx.go f [] x✝ = x✝.toList
- List.mapIdx.go f (a :: as) x✝ = List.mapIdx.go f as (x✝.push (f x✝.size a))
Instances For
mapFinIdx #
theorem
List.getElem_mapFinIdx_go
{α : Type u_1}
{β : Type u_2}
{bs : List α}
{acc : Array β}
{as : List α}
{f : Fin as.length → α → β}
{i : Nat}
{h : bs.length + acc.size = as.length}
{w : i < (mapFinIdx.go as f bs acc h).length}
:
(mapFinIdx.go as f bs acc h)[i] = if w' : i < acc.size then acc[i] else f ⟨i, ⋯⟩ bs[i - acc.size]
theorem
List.mapFinIdx_append
{α : Type u_1}
{β : Type u_2}
{K L : List α}
{f : Fin (K ++ L).length → α → β}
:
(K ++ L).mapFinIdx f = (K.mapFinIdx fun (i : Fin K.length) => f (Fin.castLE ⋯ i)) ++ L.mapFinIdx fun (i : Fin L.length) => f (Fin.cast ⋯ (Fin.natAdd K.length i))
mapIdx #
@[simp]
theorem
List.getElem?_mapIdx
{α : Type u_1}
{α✝ : Type u_2}
{f : Nat → α → α✝}
{l : List α}
{i : Nat}
:
(mapIdx f l)[i]? = Option.map (f i) l[i]?
theorem
List.mapIdx_eq_enum_map
{α : Type u_1}
{α✝ : Type u_2}
{f : Nat → α → α✝}
{l : List α}
:
mapIdx f l = map (Function.uncurry f) l.enum
@[simp]
theorem
List.head?_mapIdx
{α : Type u_1}
{β : Type u_2}
{l : List α}
{f : Nat → α → β}
:
(mapIdx f l).head? = Option.map (f 0) l.head?
@[simp]
theorem
List.getLast?_mapIdx
{α : Type u_1}
{β : Type u_2}
{l : List α}
{f : Nat → α → β}
:
(mapIdx f l).getLast? = Option.map (f (l.length - 1)) l.getLast?