Documentation

CslibFrontier.Foundations.Data.List.FilterMap

filterMap as a filter followed by a map #

l.filterMap f keeps the elements on which f is defined and maps them by f, read with any default (List.filterMap_eq_map_filter). This is the form that list combinators which filter and map separately compute.

Main results #

theorem List.filterMap_eq_map_filter {α : Type u_1} {β : Type u_2} (l : List α) (f : α → Option β) (d : β) :
filterMap f l = map (fun (a : α) => (f a).getD d) (filter (fun (a : α) => (f a).isSome) l)

filterMap is a filter by definedness followed by a map, read with any default.

theorem List.getElem?_filterMap_of_forall_isSome {α : Type u_1} {β : Type u_2} {f : α → Option β} {l : List α} :
(∀ a ∈ l, (f a).isSome = true) → ∀ (i : ℕ), (filterMap f l)[i]? = l[i]?.bind f

When f is defined on every element, indexing l.filterMap f is indexing l and applying f.

theorem List.take_filterMap_of_forall_isSome {α : Type u_1} {β : Type u_2} {f : α → Option β} {l : List α} :
(∀ a ∈ l, (f a).isSome = true) → ∀ (r : ℕ), take r (filterMap f l) = filterMap f (take r l)

When f is defined on every element, filterMap f commutes with take.

theorem List.sum_map_filterMap_le {α : Type u_1} {β : Type u_2} {f : α → Option β} {C : β → ℕ} {D : α → ℕ} (h : ∀ (a : α) (b : β), f a = some b → C b ≤ D a) (l : List α) :
(map C (filterMap f l)).sum ≤ (map D l).sum

A sum over the values of filterMap f is at most a sum over the list when every value b of f a has C b ≤ D a.