diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-07-18 22:35:47 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-07-18 22:35:47 +0100 |
commit | e657d78fbadad6e02e2cd3e2427e79a54ddb343e (patch) | |
tree | 4a659896bd8a1beb5d7e110a04cb366b3eb84633 | |
parent | eb2f87a814ba3235a4e98c201e8e82e25da2e9d0 (diff) | |
download | gleam_stdlib-e657d78fbadad6e02e2cd3e2427e79a54ddb343e.tar.gz gleam_stdlib-e657d78fbadad6e02e2cd3e2427e79a54ddb343e.zip |
Result functions on JS
-rw-r--r-- | src/gleam/result.gleam | 508 | ||||
-rw-r--r-- | test/gleam/function_test.gleam | 183 | ||||
-rw-r--r-- | test/gleam/result_test.gleam | 338 |
3 files changed, 514 insertions, 515 deletions
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index 4527516..d0601bf 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -1,282 +1,280 @@ -if erlang { - import gleam/list +import gleam/list - /// Result represents the result of something that may succeed or not. - /// `Ok` means it was successful, `Error` means it was not successful. - /// - pub type Result(success, error) = - Result(success, error) +/// Result represents the result of something that may succeed or not. +/// `Ok` means it was successful, `Error` means it was not successful. +/// +pub type Result(success, error) = + Result(success, error) - /// Nil is a type used to represent the absence of something, similar to null - /// or undefined in other languages. - /// - /// Unlike some other languages values cannot be implicitly nil. - /// - pub type Nil = - Nil +/// Nil is a type used to represent the absence of something, similar to null +/// or undefined in other languages. +/// +/// Unlike some other languages values cannot be implicitly nil. +/// +pub type Nil = + Nil - /// Checks whether the result is an Ok value. - /// - /// ## Examples - /// - /// > is_ok(Ok(1)) - /// True - /// - /// > is_ok(Error(Nil)) - /// False - /// - pub fn is_ok(result: Result(a, e)) -> Bool { - case result { - Error(_) -> False - Ok(_) -> True - } +/// Checks whether the result is an Ok value. +/// +/// ## Examples +/// +/// > is_ok(Ok(1)) +/// True +/// +/// > is_ok(Error(Nil)) +/// False +/// +pub fn is_ok(result: Result(a, e)) -> Bool { + case result { + Error(_) -> False + Ok(_) -> True } +} - /// Checks whether the result is an Error value. - /// - /// ## Examples - /// - /// > is_error(Ok(1)) - /// False - /// - /// > is_error(Error(Nil)) - /// True - /// - pub fn is_error(result: Result(a, e)) -> Bool { - case result { - Ok(_) -> False - Error(_) -> True - } +/// Checks whether the result is an Error value. +/// +/// ## Examples +/// +/// > is_error(Ok(1)) +/// False +/// +/// > is_error(Error(Nil)) +/// True +/// +pub fn is_error(result: Result(a, e)) -> Bool { + case result { + Ok(_) -> False + Error(_) -> True } +} - /// Updates a value held within the Ok of a result by calling a given function - /// on it. - /// - /// If the result is an Error rather than OK the function is not called and the - /// result stays the same. - /// - /// ## Examples - /// - /// > map(over: Ok(1), with: fn(x) { x + 1 }) - /// Ok(2) - /// - /// > map(over: Error(1), with: fn(x) { x + 1 }) - /// Error(1) - /// - pub fn map(over result: Result(a, e), with fun: fn(a) -> b) -> Result(b, e) { - case result { - Ok(x) -> Ok(fun(x)) - Error(e) -> Error(e) - } +/// Updates a value held within the Ok of a result by calling a given function +/// on it. +/// +/// If the result is an Error rather than OK the function is not called and the +/// result stays the same. +/// +/// ## Examples +/// +/// > map(over: Ok(1), with: fn(x) { x + 1 }) +/// Ok(2) +/// +/// > map(over: Error(1), with: fn(x) { x + 1 }) +/// Error(1) +/// +pub fn map(over result: Result(a, e), with fun: fn(a) -> b) -> Result(b, e) { + case result { + Ok(x) -> Ok(fun(x)) + Error(e) -> Error(e) } +} - /// Updates a value held within the Error of a result by calling a given function - /// on it. - /// - /// If the result is Ok rather than Error the function is not called and the - /// result stays the same. - /// - /// ## Examples - /// - /// > map_error(over: Error(1), with: fn(x) { x + 1 }) - /// Error(2) - /// - /// > map_error(over: Ok(1), with: fn(x) { x + 1 }) - /// Ok(1) - /// - pub fn map_error( - over result: Result(a, e), - with fun: fn(e) -> f, - ) -> Result(a, f) { - case result { - Ok(x) -> Ok(x) - Error(error) -> Error(fun(error)) - } +/// Updates a value held within the Error of a result by calling a given function +/// on it. +/// +/// If the result is Ok rather than Error the function is not called and the +/// result stays the same. +/// +/// ## Examples +/// +/// > map_error(over: Error(1), with: fn(x) { x + 1 }) +/// Error(2) +/// +/// > map_error(over: Ok(1), with: fn(x) { x + 1 }) +/// Ok(1) +/// +pub fn map_error( + over result: Result(a, e), + with fun: fn(e) -> f, +) -> Result(a, f) { + case result { + Ok(x) -> Ok(x) + Error(error) -> Error(fun(error)) } +} - /// Merges a nested Result into a single layer. - /// - /// ## Examples - /// - /// > flatten(Ok(Ok(1))) - /// Ok(1) - /// - /// > flatten(Ok(Error("")) - /// Error("") - /// - /// > flatten(Error(Nil)) - /// Error(Nil) - /// - pub fn flatten(result: Result(Result(a, e), e)) -> Result(a, e) { - case result { - Ok(x) -> x - Error(error) -> Error(error) - } +/// Merges a nested Result into a single layer. +/// +/// ## Examples +/// +/// > flatten(Ok(Ok(1))) +/// Ok(1) +/// +/// > flatten(Ok(Error("")) +/// Error("") +/// +/// > flatten(Error(Nil)) +/// Error(Nil) +/// +pub fn flatten(result: Result(Result(a, e), e)) -> Result(a, e) { + case result { + Ok(x) -> x + Error(error) -> Error(error) } +} - /// Updates a value held within the Ok of a result by calling a given function - /// on it, where the given function also returns a result. The two results are - /// then merged together into one result. - /// - /// If the result is an Error rather than OK the function is not called and the - /// result stays the same. - /// - /// This function is the equivalent of calling `map` followed by `flatten`, and - /// it is useful for chaining together multiple functions that may fail. - /// - /// ## Examples - /// - /// > then(Ok(1), fn(x) { Ok(x + 1) }) - /// Ok(2) - /// - /// > then(Ok(1), fn(x) { Ok(#("a", x)) }) - /// Ok(#("a", 1)) - /// - /// > then(Ok(1), fn(x) { Error("Oh no") }) - /// Error("Oh no") - /// - /// > then(Error(Nil), fn(x) { Ok(x + 1) }) - /// Error(Nil) - /// - pub fn then( - result: Result(a, e), - apply fun: fn(a) -> Result(b, e), - ) -> Result(b, e) { - case result { - Ok(x) -> fun(x) - Error(e) -> Error(e) - } +/// Updates a value held within the Ok of a result by calling a given function +/// on it, where the given function also returns a result. The two results are +/// then merged together into one result. +/// +/// If the result is an Error rather than OK the function is not called and the +/// result stays the same. +/// +/// This function is the equivalent of calling `map` followed by `flatten`, and +/// it is useful for chaining together multiple functions that may fail. +/// +/// ## Examples +/// +/// > then(Ok(1), fn(x) { Ok(x + 1) }) +/// Ok(2) +/// +/// > then(Ok(1), fn(x) { Ok(#("a", x)) }) +/// Ok(#("a", 1)) +/// +/// > then(Ok(1), fn(x) { Error("Oh no") }) +/// Error("Oh no") +/// +/// > then(Error(Nil), fn(x) { Ok(x + 1) }) +/// Error(Nil) +/// +pub fn then( + result: Result(a, e), + apply fun: fn(a) -> Result(b, e), +) -> Result(b, e) { + case result { + Ok(x) -> fun(x) + Error(e) -> Error(e) } +} - /// Extracts the Ok value from a result, returning a default value if the result - /// is an Error. - /// - /// ## Examples - /// - /// > unwrap(Ok(1), 0) - /// 1 - /// - /// > unwrap(Error(""), 0) - /// 0 - /// - pub fn unwrap(result: Result(a, e), or default: a) -> a { - case result { - Ok(v) -> v - Error(_) -> default - } +/// Extracts the Ok value from a result, returning a default value if the result +/// is an Error. +/// +/// ## Examples +/// +/// > unwrap(Ok(1), 0) +/// 1 +/// +/// > unwrap(Error(""), 0) +/// 0 +/// +pub fn unwrap(result: Result(a, e), or default: a) -> a { + case result { + Ok(v) -> v + Error(_) -> default } +} - /// Extracts the Ok value from a result, evaluating the default function if the result - /// is an Error. - /// - /// ## Examples - /// - /// > lazy_unwrap(Ok(1), fn() { 0 }) - /// 1 - /// - /// > lazy_unwrap(Error(""), fn() { 0 }) - /// 0 - /// - pub fn lazy_unwrap(result: Result(a, e), or default: fn() -> a) -> a { - case result { - Ok(v) -> v - Error(_) -> default() - } +/// Extracts the Ok value from a result, evaluating the default function if the result +/// is an Error. +/// +/// ## Examples +/// +/// > lazy_unwrap(Ok(1), fn() { 0 }) +/// 1 +/// +/// > lazy_unwrap(Error(""), fn() { 0 }) +/// 0 +/// +pub fn lazy_unwrap(result: Result(a, e), or default: fn() -> a) -> a { + case result { + Ok(v) -> v + Error(_) -> default() } +} - /// Transforms any error into Error(Nil) - /// - /// ## Examples - /// - /// > nil_error(Error(1)) - /// Error(Nil) - /// - /// > nil_error(Ok(1)) - /// Ok(1) - /// - pub fn nil_error(result: Result(a, e)) -> Result(a, Nil) { - map_error(result, fn(_) { Nil }) - } +/// Transforms any error into Error(Nil) +/// +/// ## Examples +/// +/// > nil_error(Error(1)) +/// Error(Nil) +/// +/// > nil_error(Ok(1)) +/// Ok(1) +/// +pub fn nil_error(result: Result(a, e)) -> Result(a, Nil) { + map_error(result, fn(_) { Nil }) +} - /// Returns the first value if it is Ok, otherwise return the second value. - /// - /// ## Examples - /// - /// > or(Ok(1), Ok(2)) - /// Ok(1) - /// - /// > or(Ok(1), Error("Error 2")) - /// Ok(1) - /// - /// > or(Error("Error 1"), Ok(2)) - /// Ok(2) - /// - /// > or(Error("Error 1"), Error("Error 2")) - /// Error("Error 2") - /// - pub fn or(first: Result(a, e), second: Result(a, e)) -> Result(a, e) { - case first { - Ok(_) -> first - Error(_) -> second - } +/// Returns the first value if it is Ok, otherwise return the second value. +/// +/// ## Examples +/// +/// > or(Ok(1), Ok(2)) +/// Ok(1) +/// +/// > or(Ok(1), Error("Error 2")) +/// Ok(1) +/// +/// > or(Error("Error 1"), Ok(2)) +/// Ok(2) +/// +/// > or(Error("Error 1"), Error("Error 2")) +/// Error("Error 2") +/// +pub fn or(first: Result(a, e), second: Result(a, e)) -> Result(a, e) { + case first { + Ok(_) -> first + Error(_) -> second } +} - /// Returns the first value if it is Ok, otherwise evaluates the given function for a fallback value. - /// - /// ## Examples - /// - /// > or(Ok(1), Ok(2)) - /// Ok(1) - /// - /// > or(Ok(1), Error("Error 2")) - /// Ok(1) - /// - /// > or(Error("Error 1"), Ok(2)) - /// Ok(2) - /// - /// > or(Error("Error 1"), Error("Error 2")) - /// Error("Error 2") - /// - pub fn lazy_or( - first: Result(a, e), - second: fn() -> Result(a, e), - ) -> Result(a, e) { - case first { - Ok(_) -> first - Error(_) -> second() - } +/// Returns the first value if it is Ok, otherwise evaluates the given function for a fallback value. +/// +/// ## Examples +/// +/// > or(Ok(1), Ok(2)) +/// Ok(1) +/// +/// > or(Ok(1), Error("Error 2")) +/// Ok(1) +/// +/// > or(Error("Error 1"), Ok(2)) +/// Ok(2) +/// +/// > or(Error("Error 1"), Error("Error 2")) +/// Error("Error 2") +/// +pub fn lazy_or( + first: Result(a, e), + second: fn() -> Result(a, e), +) -> Result(a, e) { + case first { + Ok(_) -> first + Error(_) -> second() } +} - /// Combines a list of results into a single result. - /// If all elements in the list are Ok then returns an Ok holding the list of values. - /// If any element is Error then returns the first error. - /// - /// ## Examples - /// > all([Ok(1), Ok(2)]) - /// Ok([1, 2]) - /// - /// > all([Ok(1), Error("e")]) - /// Error("e") - pub fn all(results: List(Result(a, e))) -> Result(List(a), e) { - list.try_map(results, fn(x) { x }) - } +/// Combines a list of results into a single result. +/// If all elements in the list are Ok then returns an Ok holding the list of values. +/// If any element is Error then returns the first error. +/// +/// ## Examples +/// > all([Ok(1), Ok(2)]) +/// Ok([1, 2]) +/// +/// > all([Ok(1), Error("e")]) +/// Error("e") +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 }) - } +pub fn replace_error(result: Result(a, e1), error: e2) -> Result(a, e2) { + result + |> map_error(fn(_) { error }) +} - /// Given a list of results - /// Return only the values inside Ok - /// - /// ## Examples - /// - /// ``` - /// > values([Ok(1), None, Ok(3)]) - /// [1, 3] - /// ``` - /// - pub fn values(results: List(Result(a, e))) -> List(a) { - list.filter_map(results, fn(r) { r }) - } +/// Given a list of results +/// Return only the values inside Ok +/// +/// ## Examples +/// +/// ``` +/// > values([Ok(1), None, Ok(3)]) +/// [1, 3] +/// ``` +/// +pub fn values(results: List(Result(a, e))) -> List(a) { + list.filter_map(results, fn(r) { r }) } diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index 0b3bd8c..e4a368c 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -1,109 +1,112 @@ -import gleam/should -import gleam/dynamic -import gleam/function -import gleam/int -import gleam/list -import gleam/result -import gleam/string - -pub fn compose_test() { - let add_two = fn(int: Int) { int + 2 } - let add_three = fn(int: Int) { int + 3 } - - let add_five = function.compose(add_two, add_three) - - 1 - |> add_five - |> should.equal(6) - - // Takes a list of ints and returns the head as a string (if there is one, or - // else "0" if there is not) - let head_to_string = - list.head - |> function.compose(result.unwrap(_, 0)) - |> function.compose(int.to_string) - - [1] - |> head_to_string - |> should.equal("1") - - [] - |> head_to_string - |> should.equal("0") -} - -pub fn curry2_test() { - let fun = fn(a, b) { a + b } - let curried = function.curry2(fun) +if erlang { + // TODO: JavaScript + import gleam/should + import gleam/dynamic + import gleam/function + import gleam/int + import gleam/list + import gleam/result + import gleam/string + + pub fn compose_test() { + let add_two = fn(int: Int) { int + 2 } + let add_three = fn(int: Int) { int + 3 } + + let add_five = function.compose(add_two, add_three) + + 1 + |> add_five + |> should.equal(6) + + // Takes a list of ints and returns the head as a string (if there is one, or + // else "0" if there is not) + let head_to_string = + list.head + |> function.compose(result.unwrap(_, 0)) + |> function.compose(int.to_string) + + [1] + |> head_to_string + |> should.equal("1") + + [] + |> head_to_string + |> should.equal("0") + } - curried(1)(2) - |> should.equal(3) -} + pub fn curry2_test() { + let fun = fn(a, b) { a + b } + let curried = function.curry2(fun) -pub fn curry3_test() { - let fun = fn(a, b, c) { a + b + c } - let curried = function.curry3(fun) + curried(1)(2) + |> should.equal(3) + } - curried(1)(2)(4) - |> should.equal(7) -} + pub fn curry3_test() { + let fun = fn(a, b, c) { a + b + c } + let curried = function.curry3(fun) -pub fn curry4_test() { - let fun = fn(a, b, c, d) { a + b + c + d } - let curried = function.curry4(fun) + curried(1)(2)(4) + |> should.equal(7) + } - curried(1)(2)(4)(8) - |> should.equal(15) -} + pub fn curry4_test() { + let fun = fn(a, b, c, d) { a + b + c + d } + let curried = function.curry4(fun) -pub fn curry5_test() { - let fun = fn(a, b, c, d, e) { a + b + c + d + e } - let curried = function.curry5(fun) + curried(1)(2)(4)(8) + |> should.equal(15) + } - curried(1)(2)(4)(8)(16) - |> should.equal(31) -} + pub fn curry5_test() { + let fun = fn(a, b, c, d, e) { a + b + c + d + e } + let curried = function.curry5(fun) -pub fn curry6_test() { - let fun = fn(a, b, c, d, e, f) { a + b + c + d + e + f } - let curried = function.curry6(fun) + curried(1)(2)(4)(8)(16) + |> should.equal(31) + } - curried(1)(2)(4)(8)(16)(32) - |> should.equal(63) -} + pub fn curry6_test() { + let fun = fn(a, b, c, d, e, f) { a + b + c + d + e + f } + let curried = function.curry6(fun) -pub fn flip_test() { - let fun = fn(s: String, i: Int) { - s - |> string.append("String: '", _) - |> string.append("', Int: '") - |> string.append(int.to_string(i)) - |> string.append("'") + curried(1)(2)(4)(8)(16)(32) + |> should.equal(63) } - let flipped_fun = function.flip(fun) + pub fn flip_test() { + let fun = fn(s: String, i: Int) { + s + |> string.append("String: '", _) + |> string.append("', Int: '") + |> string.append(int.to_string(i)) + |> string.append("'") + } - fun("Bob", 1) - |> should.equal("String: 'Bob', Int: '1'") + let flipped_fun = function.flip(fun) - flipped_fun(2, "Alice") - |> should.equal("String: 'Alice', Int: '2'") -} + fun("Bob", 1) + |> should.equal("String: 'Bob', Int: '1'") + + flipped_fun(2, "Alice") + |> should.equal("String: 'Alice', Int: '2'") + } -pub fn identity_test() { - 1 - |> function.identity - |> should.equal(1) + pub fn identity_test() { + 1 + |> function.identity + |> should.equal(1) - "" - |> function.identity - |> should.equal("") + "" + |> function.identity + |> should.equal("") - [] - |> function.identity - |> should.equal([]) + [] + |> function.identity + |> should.equal([]) - #(1, 2.0) - |> function.identity - |> should.equal(#(1, 2.0)) + #(1, 2.0) + |> function.identity + |> should.equal(#(1, 2.0)) + } } diff --git a/test/gleam/result_test.gleam b/test/gleam/result_test.gleam index 464aa2b..a2bd6ff 100644 --- a/test/gleam/result_test.gleam +++ b/test/gleam/result_test.gleam @@ -1,171 +1,169 @@ -if erlang { - import gleam/should - import gleam/result - - pub fn is_ok_test() { - result.is_ok(Ok(1)) - |> should.be_true - - result.is_ok(Error(1)) - |> should.be_false - } - - pub fn is_error_test() { - result.is_error(Ok(1)) - |> should.be_false - - result.is_error(Error(1)) - |> should.be_true - } - - pub fn map_test() { - Ok(1) - |> result.map(fn(x) { x + 1 }) - |> should.equal(Ok(2)) - - Ok(1) - |> result.map(fn(_) { "2" }) - |> should.equal(Ok("2")) - - Error(1) - |> result.map(fn(x) { x + 1 }) - |> should.equal(Error(1)) - } - - pub fn map_error_test() { - Ok(1) - |> result.map_error(fn(x) { x + 1 }) - |> should.equal(Ok(1)) - - Error(1) - |> result.map_error(fn(x) { #("ok", x + 1) }) - |> should.equal(Error(#("ok", 2))) - } - - pub fn flatten_test() { - Ok(Ok(1)) - |> result.flatten - |> should.equal(Ok(1)) - - Ok(Error(1)) - |> result.flatten - |> should.equal(Error(1)) - - Error(1) - |> result.flatten - |> should.equal(Error(1)) - - Error(Error(1)) - |> result.flatten - |> should.equal(Error(Error(1))) - } - - pub fn then_test() { - Error(1) - |> result.then(fn(x) { Ok(x + 1) }) - |> should.equal(Error(1)) - - Ok(1) - |> result.then(fn(x) { Ok(x + 1) }) - |> should.equal(Ok(2)) - - Ok(1) - |> result.then(fn(_) { Ok("type change") }) - |> should.equal(Ok("type change")) - - Ok(1) - |> result.then(fn(_) { Error(1) }) - |> should.equal(Error(1)) - } - - pub fn unwrap_test() { - Ok(1) - |> result.unwrap(50) - |> should.equal(1) - - Error("nope") - |> result.unwrap(50) - |> should.equal(50) - } - - pub fn lazy_unwrap_test() { - Ok(1) - |> result.lazy_unwrap(fn() { 50 }) - |> should.equal(1) - - Error("nope") - |> result.lazy_unwrap(fn() { 50 }) - |> should.equal(50) - } - - pub fn nil_error_test() { - Error("error_string") - |> result.nil_error - |> should.equal(Error(Nil)) - - Error(123) - |> result.nil_error - |> should.equal(Error(Nil)) - - Ok(1) - |> result.nil_error - |> should.equal(Ok(1)) - } - - pub fn or_test() { - Ok(1) - |> result.or(Ok(2)) - |> should.equal(Ok(1)) - - Ok(1) - |> result.or(Error("Error 2")) - |> should.equal(Ok(1)) - - Error("Error 1") - |> result.or(Ok(2)) - |> should.equal(Ok(2)) - - Error("Error 1") - |> result.or(Error("Error 2")) - |> should.equal(Error("Error 2")) - } - - pub fn lazy_or_test() { - Ok(1) - |> result.lazy_or(fn() { Ok(2) }) - |> should.equal(Ok(1)) - - Ok(1) - |> result.lazy_or(fn() { Error("Error 2") }) - |> should.equal(Ok(1)) - - Error("Error 1") - |> result.lazy_or(fn() { Ok(2) }) - |> should.equal(Ok(2)) - - Error("Error 1") - |> result.lazy_or(fn() { Error("Error 2") }) - |> should.equal(Error("Error 2")) - } - - pub fn all_test() { - [Ok(1), Ok(2), Ok(3)] - |> result.all - |> should.equal(Ok([1, 2, 3])) - - [Ok(1), Error("a"), Error("b"), Ok(3)] - |> result.all - |> should.equal(Error("a")) - } - - pub fn replace_error_test() { - Error(Nil) - |> result.replace_error("Invalid") - |> should.equal(Error("Invalid")) - } - - pub fn values_test() { - result.values([Ok(1), Error(""), Ok(3)]) - |> should.equal([1, 3]) - } +import gleam/should +import gleam/result + +pub fn is_ok_test() { + result.is_ok(Ok(1)) + |> should.be_true + + result.is_ok(Error(1)) + |> should.be_false +} + +pub fn is_error_test() { + result.is_error(Ok(1)) + |> should.be_false + + result.is_error(Error(1)) + |> should.be_true +} + +pub fn map_test() { + Ok(1) + |> result.map(fn(x) { x + 1 }) + |> should.equal(Ok(2)) + + Ok(1) + |> result.map(fn(_) { "2" }) + |> should.equal(Ok("2")) + + Error(1) + |> result.map(fn(x) { x + 1 }) + |> should.equal(Error(1)) +} + +pub fn map_error_test() { + Ok(1) + |> result.map_error(fn(x) { x + 1 }) + |> should.equal(Ok(1)) + + Error(1) + |> result.map_error(fn(x) { #("ok", x + 1) }) + |> should.equal(Error(#("ok", 2))) +} + +pub fn flatten_test() { + Ok(Ok(1)) + |> result.flatten + |> should.equal(Ok(1)) + + Ok(Error(1)) + |> result.flatten + |> should.equal(Error(1)) + + Error(1) + |> result.flatten + |> should.equal(Error(1)) + + Error(Error(1)) + |> result.flatten + |> should.equal(Error(Error(1))) +} + +pub fn then_test() { + Error(1) + |> result.then(fn(x) { Ok(x + 1) }) + |> should.equal(Error(1)) + + Ok(1) + |> result.then(fn(x) { Ok(x + 1) }) + |> should.equal(Ok(2)) + + Ok(1) + |> result.then(fn(_) { Ok("type change") }) + |> should.equal(Ok("type change")) + + Ok(1) + |> result.then(fn(_) { Error(1) }) + |> should.equal(Error(1)) +} + +pub fn unwrap_test() { + Ok(1) + |> result.unwrap(50) + |> should.equal(1) + + Error("nope") + |> result.unwrap(50) + |> should.equal(50) +} + +pub fn lazy_unwrap_test() { + Ok(1) + |> result.lazy_unwrap(fn() { 50 }) + |> should.equal(1) + + Error("nope") + |> result.lazy_unwrap(fn() { 50 }) + |> should.equal(50) +} + +pub fn nil_error_test() { + Error("error_string") + |> result.nil_error + |> should.equal(Error(Nil)) + + Error(123) + |> result.nil_error + |> should.equal(Error(Nil)) + + Ok(1) + |> result.nil_error + |> should.equal(Ok(1)) +} + +pub fn or_test() { + Ok(1) + |> result.or(Ok(2)) + |> should.equal(Ok(1)) + + Ok(1) + |> result.or(Error("Error 2")) + |> should.equal(Ok(1)) + + Error("Error 1") + |> result.or(Ok(2)) + |> should.equal(Ok(2)) + + Error("Error 1") + |> result.or(Error("Error 2")) + |> should.equal(Error("Error 2")) +} + +pub fn lazy_or_test() { + Ok(1) + |> result.lazy_or(fn() { Ok(2) }) + |> should.equal(Ok(1)) + + Ok(1) + |> result.lazy_or(fn() { Error("Error 2") }) + |> should.equal(Ok(1)) + + Error("Error 1") + |> result.lazy_or(fn() { Ok(2) }) + |> should.equal(Ok(2)) + + Error("Error 1") + |> result.lazy_or(fn() { Error("Error 2") }) + |> should.equal(Error("Error 2")) +} + +pub fn all_test() { + [Ok(1), Ok(2), Ok(3)] + |> result.all + |> should.equal(Ok([1, 2, 3])) + + [Ok(1), Error("a"), Error("b"), Ok(3)] + |> result.all + |> should.equal(Error("a")) +} + +pub fn replace_error_test() { + Error(Nil) + |> result.replace_error("Invalid") + |> should.equal(Error("Invalid")) +} + +pub fn values_test() { + result.values([Ok(1), Error(""), Ok(3)]) + |> should.equal([1, 3]) } |