diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/option.gleam | 16 | ||||
-rw-r--r-- | test/gleam/option_test.gleam | 5 |
3 files changed, 22 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index bcbb957..f27a2eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- The `option` module gains the `values` function. - The `result` module gains the `values` function. - All modules now use the new `#(a, b, ...)` tuple syntax. diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam index f74abbd..8045a28 100644 --- a/src/gleam/option.gleam +++ b/src/gleam/option.gleam @@ -1,3 +1,5 @@ +import gleam/list + /// Option represents a value that may be present or not. Some means the value is /// present, None means the value is not. /// @@ -180,3 +182,17 @@ pub fn or(first: Option(a), second: Option(a)) -> Option(a) { None -> second } } + +/// Given a list of options +/// Return only the values inside Some +/// +/// ## Examples +/// +/// ``` +/// > values([Some(1), None, Some(3)]) +/// [1, 3] +/// ``` +/// +pub fn values(options: List(Option(a))) -> List(a) { + list.filter_map(options, fn(op) { to_result(op, "") }) +} diff --git a/test/gleam/option_test.gleam b/test/gleam/option_test.gleam index 2fb808e..2a31a75 100644 --- a/test/gleam/option_test.gleam +++ b/test/gleam/option_test.gleam @@ -100,3 +100,8 @@ pub fn or_option_test() { |> option.or(None) |> should.equal(None) } + +pub fn values_test() { + option.values([Some(1), None, Some(3)]) + |> should.equal([1, 3]) +} |