aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2023-04-21 12:07:17 +0100
committerLouis Pilfold <louis@lpil.uk>2023-04-21 12:07:17 +0100
commit13f8dc62e08fb499e600a4bcacfae7a29008dbfa (patch)
treefd89fca7d173901c2f71fcd15496f123f6610946
parent5a72c6977ab5a0bf0a4a78adb404d7f4783c51a8 (diff)
downloadgleam_stdlib-13f8dc62e08fb499e600a4bcacfae7a29008dbfa.tar.gz
gleam_stdlib-13f8dc62e08fb499e600a4bcacfae7a29008dbfa.zip
list.try_each returns the error
-rw-r--r--src/gleam/list.gleam42
-rw-r--r--test/gleam/list_test.gleam35
2 files changed, 31 insertions, 46 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 4a6d778..e643976 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -1565,50 +1565,32 @@ pub fn each(list: List(a), f: fn(a) -> b) -> Nil {
}
}
-/// Used to iterate through a list of items and execute a side effect per item as long as the
-/// previous side effect execution returned an `Ok(_)`.
+/// Calls a `Result` returning function for each element in a list, discarding
+/// the return value. If the function returns `Error` then the iteration is
+/// stopped and the error is returned.
///
-/// Takes a function that returns a `Result`. The function is applied to each list item.
-/// If the function application on an item returns `Ok(_)`, then `try_each` will go on and apply the
-/// function to the next item.
-/// However, if any application returns an `Error(_)`, at that point the function will stop executing.
-///
-/// In any case the function returns `Nil`, even if it passes through all items or incurs in an
-/// `Error`.
+/// Useful for calling a side effect for every item of a list.
///
/// ## Examples
///
/// ```gleam
/// > try_each(
/// > over: [1, 2, 3],
-/// > with: fn(x) {
-/// > x |> int.to_string |> io.print // prints "1" "2" "3" as single side effects.
-/// > Ok(Nil)
-/// > },
+/// > with: function_that_might_fail,
/// > )
-/// Nil
+/// Ok(Nil)
/// ```
///
-/// ```gleam
-/// > try_each(
-/// > over: [1, 2, 3],
-/// > with: fn(x) {
-/// > x |> int.to_string |> io.print // prints "1" "2" as single side effects.
-/// > case x {
-/// > 2 -> Error(Nil)
-/// > _ -> Ok(Nil)
-/// > }
-/// > },
-/// > )
-/// Nil
-/// ```
-pub fn try_each(over list: List(a), with fun: fn(a) -> Result(b, c)) -> Nil {
+pub fn try_each(
+ over list: List(a),
+ with fun: fn(a) -> Result(b, e),
+) -> Result(Nil, e) {
case list {
- [] -> Nil
+ [] -> Ok(Nil)
[x, ..xs] ->
case fun(x) {
Ok(_) -> try_each(over: xs, with: fun)
- Error(_) -> Nil
+ Error(e) -> Error(e)
}
}
}
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index 07b2bae..e32c1a9 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -864,26 +864,29 @@ pub fn each_test() {
}
pub fn try_each_test() {
- list.try_each(
- over: [1, 1, 1],
- with: fn(x) {
- should.equal(x, 1)
- Ok(Nil)
- },
- )
+ let assert Ok(Nil) =
+ list.try_each(
+ over: [1, 1, 1],
+ with: fn(x) {
+ should.equal(x, 1)
+ Ok(Nil)
+ },
+ )
// `try_each` actually stops when `fun` returns error
- list.try_each(
- over: [1, 2, 3],
- with: fn(x) {
- should.equal(x, 1)
- Error(Nil)
- },
- )
+ let assert Error(1) =
+ list.try_each(
+ over: [1, 2, 3],
+ with: fn(x) {
+ should.equal(x, 1)
+ Error(x)
+ },
+ )
// TCO test
- list.repeat(1, recursion_test_cycles)
- |> list.try_each(with: fn(_) { Ok(Nil) })
+ let assert Ok(Nil) =
+ list.repeat(1, recursion_test_cycles)
+ |> list.try_each(with: fn(_) { Ok(Nil) })
}
pub fn partition_test() {