diff options
-rw-r--r-- | src/gleam/result.gleam | 7 | ||||
-rw-r--r-- | test/gleam/result_test.gleam | 6 |
2 files changed, 7 insertions, 6 deletions
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index 6d65459..3833add 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -368,13 +368,14 @@ pub fn all(results: List(Result(a, e))) -> Result(List(a), e) { /// Given a list of results, returns a pair where the first element is a list /// of all the values inside `Ok` and the second element is a list with all the -/// values inside `Error`. +/// values inside `Error`. The values in both lists appear in reverse order with +/// respect to their position in the original list of results. /// /// ## Examples /// /// ```gleam /// > partition([Ok(1), Error("a"), Error("b"), Ok(2)]) -/// #([1, 2], ["a", "b"]) +/// #([2, 1], ["b", "a"]) /// ``` /// pub fn partition(results: List(Result(a, e))) -> #(List(a), List(e)) { @@ -383,7 +384,7 @@ pub fn partition(results: List(Result(a, e))) -> #(List(a), List(e)) { fn do_partition(results: List(Result(a, e)), oks: List(a), errors: List(e)) { case results { - [] -> #(list.reverse(oks), list.reverse(errors)) + [] -> #(oks, errors) [Ok(a), ..rest] -> do_partition(rest, [a, ..oks], errors) [Error(e), ..rest] -> do_partition(rest, oks, [e, ..errors]) } diff --git a/test/gleam/result_test.gleam b/test/gleam/result_test.gleam index 951ef2a..8b4b829 100644 --- a/test/gleam/result_test.gleam +++ b/test/gleam/result_test.gleam @@ -203,15 +203,15 @@ pub fn partition_test() { [Ok(1), Ok(2), Ok(3)] |> result.partition - |> should.equal(#([1, 2, 3], [])) + |> should.equal(#([3, 2, 1], [])) [Error("a"), Error("b"), Error("c")] |> result.partition - |> should.equal(#([], ["a", "b", "c"])) + |> should.equal(#([], ["c", "b", "a"])) [Ok(1), Error("a"), Ok(2), Error("b"), Error("c")] |> result.partition - |> should.equal(#([1, 2], ["a", "b", "c"])) + |> should.equal(#([2, 1], ["c", "b", "a"])) // TCO test list.repeat(Ok(1), 1_000_000) |