Documentation

CslibFrontier.Foundations.Data.List.KeyedFlatMap

Selecting a unique keyed block in a flat map #

Scanning a list for a unique key and returning that entry's block reads exactly the selected entry. Values and blocks may repeat; only keys must be distinct.

theorem List.flatMap_eq_if_mem_of_eq_nil {α : Type u_1} {β : Type u_2} [DecidableEq α] {l : List α} (hl : l.Nodup) (f : α → List β) (a : α) (hf : ∀ b ∈ l, b ≠ a → f b = []) :
flatMap f l = if a ∈ l then f a else []

If only the entry at a can contribute in a duplicate-free list, the flat map returns its block when present and the empty list otherwise.

theorem List.flatMap_ite_key_eq {α : Type u_1} {β : Type u_2} {ι : Type u_3} [DecidableEq ι] (l : List α) (key : α → ι) (f : α → List β) (hn : (map key l).Nodup) {a : α} (ha : a ∈ l) :
flatMap (fun (b : α) => if key b = key a then f b else []) l = f a

A keyed block scan reads the selected entry exactly when the list has distinct keys.