aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/result.gleam5
-rw-r--r--test/gleam/result_test.gleam6
3 files changed, 12 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a6fdafa..433f82e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,7 @@
- The `int` module gains the `absolute_value`, `sum` and `product` functions.
- The `float` module gains the `sum` and `product` functions.
-- The `result` module gains the `lazy_or` and `lazy_unwrap` functions.
+- The `result` module gains the `lazy_or`, `lazy_unwrap`, and `replace_error` functions.
- The `bool` module gains the `nand`, `nor`, `exclusive_nor`, and `exclusive_or` functions.
- The `bit_builder` module gains the `from_string_builder` function.
- The `list` modules gains the `permutations` function.
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam
index cf4df3a..efee97a 100644
--- a/src/gleam/result.gleam
+++ b/src/gleam/result.gleam
@@ -259,3 +259,8 @@ pub fn lazy_or(
pub fn all(results: List(Result(a, e))) -> Result(List(a), e) {
list.try_map(results, fn(x) { x })
}
+
+pub fn replace_error(result: Result(a, e1), error: e2) -> Result(a, e2) {
+ result
+ |> map_error(fn(_) { error })
+}
diff --git a/test/gleam/result_test.gleam b/test/gleam/result_test.gleam
index f7e65e7..2036eae 100644
--- a/test/gleam/result_test.gleam
+++ b/test/gleam/result_test.gleam
@@ -156,3 +156,9 @@ pub fn all_test() {
|> result.all
|> should.equal(Error("a"))
}
+
+pub fn replace_error_test() {
+ Error(Nil)
+ |> result.replace_error("Invalid")
+ |> should.equal(Error("Invalid"))
+}