Skip to content

Commit b8000c4

Browse files
sportolpil
authored andcommitted
Add result.replace_error
1 parent ef62cdc commit b8000c4

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- The `int` module gains the `absolute_value`, `sum` and `product` functions.
66
- The `float` module gains the `sum` and `product` functions.
7-
- The `result` module gains the `lazy_or` and `lazy_unwrap` functions.
7+
- The `result` module gains the `lazy_or`, `lazy_unwrap`, and `replace_error` functions.
88
- The `bool` module gains the `nand`, `nor`, `exclusive_nor`, and `exclusive_or` functions.
99
- The `bit_builder` module gains the `from_string_builder` function.
1010
- The `list` modules gains the `permutations` function.

src/gleam/result.gleam

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,8 @@ pub fn lazy_or(
259259
pub fn all(results: List(Result(a, e))) -> Result(List(a), e) {
260260
list.try_map(results, fn(x) { x })
261261
}
262+
263+
pub fn replace_error(result: Result(a, e1), error: e2) -> Result(a, e2) {
264+
result
265+
|> map_error(fn(_) { error })
266+
}

test/gleam/result_test.gleam

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,9 @@ pub fn all_test() {
156156
|> result.all
157157
|> should.equal(Error("a"))
158158
}
159+
160+
pub fn replace_error_test() {
161+
Error(Nil)
162+
|> result.replace_error("Invalid")
163+
|> should.equal(Error("Invalid"))
164+
}

0 commit comments

Comments
 (0)