Skip to content

Commit

Permalink
Implement void Match; closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
aggieben committed Jan 9, 2019
1 parent 5f71bff commit 0ace247
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/TrueMyth/Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ private Maybe(TValue value)
/// </example>
public UValue Match<UValue>(Func<TValue,UValue> just, Func<UValue> nothing) => this._isJust ? just(this._value) : nothing();

/// <summary>
/// Provides a similar functionality as <see cref="Match{UValue}(Func{TValue,UValue}, Func{UValue})"/>, but without return types.
/// </summary>
public void Match(Action<TValue> just, Action nothing)
{
if (this._isJust)
{
just(this._value);
}
else
{
nothing();
}
}

/// <summary>
/// Static factory method for creation of new <b>Just</b> <c>Maybe&lt;TValue&gt;</c>s. If <c>value</c> is a
/// reference type and is <c>null</c>, this returns a <b>Nothing</b>.
Expand Down
15 changes: 15 additions & 0 deletions src/TrueMyth/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,21 @@ public Result<TValue, UError> MapErr<UError>(Func<TError,UError> mapFn) => !this
/// </example>
public T Match<T>(Func<TValue,T> ok, Func<TError,T> err) => this._isOk ? ok(this._value) : err(this._error);

/// <summary>
/// Provides similar functionality as <see cref="Match{T}(Func{TValue,T}, Func{TError,T})"/>, but with no return type.
/// </summary>
public void Match(Action<TValue> ok, Action<TError> err)
{
if (this.IsOk)
{
ok(this._value);
}
else
{
err(this._error);
}
}

/// <summary>
/// Provide a fallback for a given <c>Result</c>. Behaves like a logical or: if the result value is an Ok, returns that result; otherwise,
/// returns the <c>defaultResult</c> value.
Expand Down
14 changes: 14 additions & 0 deletions test/TrueMyth.Test/MaybeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ public void Match_Theory(Maybe<int> m1)
if (m1.IsNothing) Assert.Equal(2, result);
}

[Theory]
[MemberData(nameof(GetEqualityTheoryData1Param))]
public void Match_Void_Theory(Maybe<int> m1)
{
m1.Match(
just: _ => {
Assert.True(m1.IsJust);
},
nothing: () => {
Assert.True(m1.IsNothing);
}
);
}

[Theory]
[MemberData(nameof(GetOrTheoryData))]
public void Or_Theory(Maybe<int> m1, Maybe<int> m2, Maybe<int> expectation)
Expand Down
6 changes: 6 additions & 0 deletions test/TrueMyth.Test/ResultTests.MemberData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ public static IEnumerable<object[]> GetToStringTests()
yield return new object[] { SimpleResult.Ok(7), $"Ok<{7.GetType().Name}>[7]"};
yield return new object[] { SimpleResult.Err("error"), $"Err<{"error".GetType().Name}>[error]"};
}

public static IEnumerable<object[]> GetResultTheoryData1()
{
yield return new object[] { SimpleResult.Ok(7) };
yield return new object[] { SimpleResult.Err("test") };
}
}
}
10 changes: 10 additions & 0 deletions test/TrueMyth.Test/ResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ public void Match_OkCallsOk_Ok()
Assert.Equal("ok", functionCalled);
}

[Theory]
[MemberData(nameof(GetResultTheoryData1))]
public void Match_Void_Theory(SimpleResult r)
{
r.Match(
ok: _ => Assert.True(r.IsOk),
err: _ => Assert.True(r.IsErr)
);
}

// Or
[Fact]
public void Or_OkReturnsThis_Ok()
Expand Down

0 comments on commit 0ace247

Please sign in to comment.