diff options
author | Sebastian Porto <s@porto5.com> | 2021-05-05 19:23:14 +1000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-05-11 09:46:10 +0100 |
commit | 8efb599e9e97c856f7e3d9c60b5b4cf9df6a1fe0 (patch) | |
tree | 5a11acf95932f9996e17989164fb325165efa47a /src | |
parent | 32b56eb7cd04bdd47a548b021602a018bdf782c6 (diff) | |
download | gleam_stdlib-8efb599e9e97c856f7e3d9c60b5b4cf9df6a1fe0.tar.gz gleam_stdlib-8efb599e9e97c856f7e3d9c60b5b4cf9df6a1fe0.zip |
Add option.all
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/option.gleam | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam index 8045a28..f72f552 100644 --- a/src/gleam/option.gleam +++ b/src/gleam/option.gleam @@ -11,6 +11,33 @@ pub type Option(a) { None } +/// Combines a list of options into a single option. +/// If all elements in the list are Some then returns a Some holding the list of values. +/// If any element is None then returns None. +/// +/// ## Examples +/// +/// ``` +/// > all([Some(1), Some(2)]) +/// Some([1, 2]) +/// +/// > all([Some(1), None]) +/// None +/// ``` +/// +pub fn all(list: List(Option(a))) -> Option(List(a)) { + list.fold_right( + list, + from: Some([]), + with: fn(item, acc) { + case acc, item { + Some(values), Some(value) -> Some([value, ..values]) + _, _ -> None + } + }, + ) +} + /// Checks whether the option is a Some value. /// /// ## Examples |