aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/option.gleam27
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