@[inline]
Case analysis on Option (Haskell's maybe).
$$\text{maybe}(b, f, \text{none}) = b, \quad \text{maybe}(b, f, \text{some}\;a) = f(a)$$
Equations
- Data.Maybe.maybe b f none = b
- Data.Maybe.maybe b f (some a) = f a
Instances For
@[inline]
Extract from Option with a default (Haskell's fromMaybe).
$$\text{fromMaybe}(d, \text{none}) = d, \quad \text{fromMaybe}(d, \text{some}\;a) = a$$
Equations
- Data.Maybe.fromMaybe d none = d
- Data.Maybe.fromMaybe d (some a) = a
Instances For
Collect some values from a list (Haskell's catMaybes).
$$\text{catMaybes}([x_1, \ldots, x_n]) = [a \mid x_i = \text{some}\;a]$$
Equations
- Data.Maybe.catMaybes [] = []
- Data.Maybe.catMaybes (none :: rest) = Data.Maybe.catMaybes rest
- Data.Maybe.catMaybes (some a :: rest) = a :: Data.Maybe.catMaybes rest
Instances For
Map and filter in one pass (Haskell's mapMaybe).
$$\text{mapMaybe}(f, xs) = \text{catMaybes}(\text{map}\;f\;xs)$$
Equations
- Data.Maybe.mapMaybe f [] = []
- Data.Maybe.mapMaybe f (x_1 :: rest) = match f x_1 with | none => Data.Maybe.mapMaybe f rest | some b => b :: Data.Maybe.mapMaybe f rest
Instances For
@[inline]
First element or none (Haskell's listToMaybe).
Equations
- Data.Maybe.listToMaybe [] = none
- Data.Maybe.listToMaybe (x_1 :: rest) = some x_1
Instances For
maybeToList / listToMaybe roundtrip.