diff --git a/option/option.mbt b/option/option.mbt index 5aafb07f9..fcfafbd94 100644 --- a/option/option.mbt +++ b/option/option.mbt @@ -107,7 +107,7 @@ pub fn flatten[T](self : Option[Option[T]]) -> Option[T] { } test "flatten" { - let a : Option[Option[Int]] = Some(Some(42)); + let a : Option[Option[Int]] = Some(Some(42)) @assertion.assert_eq(flatten(a),Some(42))? let b : Option[Option[Int]] = Some(None) @assertion.assert_eq(flatten(b),None)? diff --git a/result/moon.pkg.json b/result/moon.pkg.json index 461766b4f..4035e72bf 100644 --- a/result/moon.pkg.json +++ b/result/moon.pkg.json @@ -1,5 +1,5 @@ { "import": [ - { "path": "moonbitlang/core/assertion", "alias": "assertion" } + "moonbitlang/core/assertion" ] } \ No newline at end of file diff --git a/result/result.mbt b/result/result.mbt index e69de29bb..a65b2c320 100644 --- a/result/result.mbt +++ b/result/result.mbt @@ -0,0 +1,76 @@ + + +/// Returns `true` if the result is an Ok variant, otherwise `false`. +pub fn is_ok[T,E](self : Result[T,E]) -> Bool { + match self { + Ok(_) => true + Err(_) => false + } +} + +/// Returns `true` if the result is an Err variant, otherwise `false`. +pub fn is_err[T,E](self : Result[T,E]) -> Bool { + match self { + Ok(_) => false + Err(_) => true + } +} + +test "is_err_or_ok" { + let a : Result[_,Unit] = Ok(1) + let b : Result[Unit,_] = Err("") + @assertion.assert_false(is_err(a))? + @assertion.assert_true(is_err(b))? + @assertion.assert_true(is_ok(a))? + @assertion.assert_false(is_ok(b))? +} + + +/// Maps the value of a `Result` using the provided function. +/// +/// If the `Result` is `Ok`, the function `f` is applied to the inner value +/// and a new `Result` is returned with the transformed value. +/// If the `Result` is `Err`, the original `Err` value is returned. +/// +pub fn map[A,B,E](self : Result[A,E], f : (A) -> B) -> Result[B,E] { + match self { + Ok(x) => Ok(f(x)) + Err(x) => Err(x) + } +} + +test "map" { + let a : Result[Int,Unit] = Ok(1) + let b : Result[Int,Int] = Err(1) + @assertion.assert_eq(a.map(fn(x){ x + 1 }), Ok(2))? + @assertion.assert_eq(b.map(fn(x){ x + 1 }), Err(1))? +} + +/// Unwraps a result, yielding the content of an `Err`. +pub fn unwrap_err[T,E](self : Result[T,E]) -> E { + match self { + Ok(_) => abort("called `Result::unwrap_err()` on an `Ok` value") + Err(e) => e + } +} + +test "unwrap" { + let b : Result[Int,Int] = Err(5) + @assertion.assert_eq(b.unwrap_err(), 5)? +} + + +pub fn to_string[T : Show, E : Show](self : Result[T,E]) -> String { + match self { + Ok(x) => "Ok(\(x))" + Err(x) => "Err(\(x))" + } +} + +test "to_string" { + let a : Result[Int,Unit] = Ok(8) + let b : Result[Int,Int] = Err(5) + a.unwrap() + @assertion.assert_eq(a.to_string(), "Ok(8)")? + @assertion.assert_eq(b.to_string(), "Err(5)")? +}