File tree Expand file tree Collapse file tree 3 files changed +36
-1
lines changed
Expand file tree Collapse file tree 3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 33## Unreleased
44
55- The ` list ` module gains the ` flat_map ` function.
6- - The ` option ` module gains the ` values ` function .
6+ - The ` option ` module gains the ` all ` and ` values ` functions .
77- The ` result ` module gains the ` values ` function.
88- All modules now use the new ` #(a, b, ...) ` tuple syntax.
99
Original file line number Diff line number Diff line change @@ -11,6 +11,33 @@ pub type Option(a) {
1111 None
1212}
1313
14+ /// Combines a list of options into a single option.
15+ /// If all elements in the list are Some then returns a Some holding the list of values.
16+ /// If any element is None then returns None.
17+ ///
18+ /// ## Examples
19+ ///
20+ /// ```
21+ /// > all([Some(1), Some(2)])
22+ /// Some([1, 2])
23+ ///
24+ /// > all([Some(1), None])
25+ /// None
26+ /// ```
27+ ///
28+ pub fn all ( list : List ( Option ( a) ) ) -> Option ( List ( a) ) {
29+ list . fold_right (
30+ list ,
31+ from : Some ( [ ] ) ,
32+ with : fn ( item , acc ) {
33+ case acc , item {
34+ Some ( values ) , Some ( value ) -> Some ( [ value , .. values ] )
35+ _ , _ -> None
36+ }
37+ } ,
38+ )
39+ }
40+
1441/// Checks whether the option is a Some value.
1542///
1643/// ## Examples
Original file line number Diff line number Diff line change 11import gleam/should
22import gleam/option . { None , Some }
33
4+ pub fn all_test ( ) {
5+ option . all ( [ Some ( 1 ) , Some ( 2 ) , Some ( 3 ) ] )
6+ |> should . equal ( Some ( [ 1 , 2 , 3 ] ) )
7+
8+ option . all ( [ Some ( 1 ) , None , Some ( 3 ) ] )
9+ |> should . equal ( None )
10+ }
11+
412pub fn is_some_test ( ) {
513 option . is_some ( Some ( 1 ) )
614 |> should . be_true
You can’t perform that action at this time.
0 commit comments