Skip to content

Commit

Permalink
add result utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin committed Mar 1, 2024
1 parent 5b1e046 commit 41af3ed
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion option/option.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down
2 changes: 1 addition & 1 deletion result/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"import": [
{ "path": "moonbitlang/core/assertion", "alias": "assertion" }
"moonbitlang/core/assertion"
]
}
76 changes: 76 additions & 0 deletions result/result.mbt
Original file line number Diff line number Diff line change
@@ -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)")?
}

0 comments on commit 41af3ed

Please sign in to comment.