Skip to content

Commit c6f14db

Browse files
authored
option.lazy_unwrap and option.lazy_or (#261)
1 parent 1d9085e commit c6f14db

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- The `dynamic` module gains the `decode2`, `decode3`, `decode4`, `decode5`,
2121
`decode6`, `decode7`, and `decode8` functions.
2222
- The `int` module gains the `digits` and `undigits` functions.
23+
- The `option` module gains the `lazy_or` and `lazy_unwrap` functions.
2324

2425
## v0.18.1 - 2021-12-19
2526

src/gleam/option.gleam

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ pub fn unwrap(option: Option(a), or default: a) -> a {
115115
}
116116
}
117117

118+
/// Extracts the value from an `Option`, evaluating the default function if the option is `None`.
119+
///
120+
/// ## Examples
121+
///
122+
/// > lazy_unwrap(Some(1), fn() { 0 })
123+
/// 1
124+
///
125+
/// > lazy_unwrap(None, fn() { 0 })
126+
/// 0
127+
///
128+
pub fn lazy_unwrap(option: Option(a), or default: fn() -> a) -> a {
129+
case option {
130+
Some(x) -> x
131+
None -> default()
132+
}
133+
}
134+
118135
/// Updates a value held within the `Some` of an `Option` by calling a given function
119136
/// on it.
120137
///
@@ -210,6 +227,29 @@ pub fn or(first: Option(a), second: Option(a)) -> Option(a) {
210227
}
211228
}
212229

230+
/// Returns the first value if it is `Some`, otherwise evaluates the given function for a fallback value.
231+
///
232+
/// ## Examples
233+
///
234+
/// > lazy_or(Some(1), fn() { Some(2) })
235+
/// Some(1)
236+
///
237+
/// > lazy_or(Some(1), fn() { None })
238+
/// Some(1)
239+
///
240+
/// > lazy_or(None, fn() { Some(2) })
241+
/// Some(2)
242+
///
243+
/// > lazy_or(None, fn() { None })
244+
/// None
245+
///
246+
pub fn lazy_or(first: Option(a), second: fn() -> Option(a)) -> Option(a) {
247+
case first {
248+
Some(_) -> first
249+
None -> second()
250+
}
251+
}
252+
213253
/// Given a list of `Option`s,
214254
/// returns only the values inside `Some`.
215255
///

test/gleam/option_test.gleam

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ pub fn unwrap_option_test() {
4949
|> should.equal(0)
5050
}
5151

52+
pub fn lazy_unwrap_option_test() {
53+
option.lazy_unwrap(Some(1), fn() { 0 })
54+
|> should.equal(1)
55+
56+
option.lazy_unwrap(None, fn() { 0 })
57+
|> should.equal(0)
58+
}
59+
5260
pub fn map_option_test() {
5361
Some(1)
5462
|> option.map(fn(x) { x + 1 })
@@ -109,6 +117,24 @@ pub fn or_option_test() {
109117
|> should.equal(None)
110118
}
111119

120+
pub fn lazy_or_option_test() {
121+
Some(1)
122+
|> option.lazy_or(fn() { Some(2) })
123+
|> should.equal(Some(1))
124+
125+
Some(1)
126+
|> option.lazy_or(fn() { None })
127+
|> should.equal(Some(1))
128+
129+
None
130+
|> option.lazy_or(fn() { Some(2) })
131+
|> should.equal(Some(2))
132+
133+
None
134+
|> option.lazy_or(fn() { None })
135+
|> should.equal(None)
136+
}
137+
112138
pub fn values_test() {
113139
option.values([Some(1), None, Some(3)])
114140
|> should.equal([1, 3])

0 commit comments

Comments
 (0)