Skip to content

Commit 7b10d02

Browse files
authored
Add option.values (#203)
1 parent 26f42e8 commit 7b10d02

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- The `option` module gains the `values` function.
56
- The `result` module gains the `values` function.
67
- All modules now use the new `#(a, b, ...)` tuple syntax.
78

src/gleam/option.gleam

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import gleam/list
2+
13
/// Option represents a value that may be present or not. Some means the value is
24
/// present, None means the value is not.
35
///
@@ -180,3 +182,17 @@ pub fn or(first: Option(a), second: Option(a)) -> Option(a) {
180182
None -> second
181183
}
182184
}
185+
186+
/// Given a list of options
187+
/// Return only the values inside Some
188+
///
189+
/// ## Examples
190+
///
191+
/// ```
192+
/// > values([Some(1), None, Some(3)])
193+
/// [1, 3]
194+
/// ```
195+
///
196+
pub fn values(options: List(Option(a))) -> List(a) {
197+
list.filter_map(options, fn(op) { to_result(op, "") })
198+
}

test/gleam/option_test.gleam

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,8 @@ pub fn or_option_test() {
100100
|> option.or(None)
101101
|> should.equal(None)
102102
}
103+
104+
pub fn values_test() {
105+
option.values([Some(1), None, Some(3)])
106+
|> should.equal([1, 3])
107+
}

0 commit comments

Comments
 (0)