aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Porto <s@porto5.com>2021-05-08 00:57:56 +1000
committerGitHub <noreply@github.com>2021-05-07 14:57:56 +0000
commit7b10d02d88af84560855337e45c755686a396943 (patch)
treed5a62c86418a92753578594c3e915bb3748dabbe
parent26f42e83dd5a842e4463b1613729c16f0251cef4 (diff)
downloadgleam_stdlib-7b10d02d88af84560855337e45c755686a396943.tar.gz
gleam_stdlib-7b10d02d88af84560855337e45c755686a396943.zip
Add option.values (#203)
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam/option.gleam16
-rw-r--r--test/gleam/option_test.gleam5
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])
+}