Skip to content

Commit

Permalink
Add more optional extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
rungwiroon committed Jan 2, 2024
1 parent 49a03c0 commit 5c83f6a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,68 @@ public void IfSomeEff_ReturnsUnitEff_WhenOptionIsEmpty()
// Assert
Assert.Equal(expected, result);
}

[Fact]
public void MatchAsEff_WhenOptionHasValue_ShouldReturnCorrectResult()
{
// Arrange
var option = Some("Test");
var ifSome = (string s) => s.ToUpper();
var ifNone = () => "None";

// Act
var result = option.MatchAsEff(ifSome, ifNone).Run();

// Assert
Assert.Equal("TEST", result);
}

[Fact]
public void MatchAsEff_WhenOptionHasNoValue_ShouldReturnCorrectResult()
{
// Arrange
var option = Option<string>.None;
var ifSome = (string s) => s.ToUpper();
var ifNone = () => "None";

// Act
var result = option.MatchAsEff(ifSome, ifNone).Run();

// Assert
Assert.Equal("None", result);
}

[Fact]
public async Task MatchAsAff_WhenOptionHasValue_ShouldReturnCorrectResult()
{
// Arrange
var option = Some("Test");
var ifSomeAsync = (string s) => Task.FromResult(s.ToUpper());
var ifNoneAsync = () => Task.FromResult("None");

// Act
var result =
await option.MatchAsAff(ifSomeAsync, ifNoneAsync)
.Run();

// Assert
Assert.Equal("TEST", result);
}

[Fact]
public async Task MatchAsAff_WhenOptionHasNoValue_ShouldReturnCorrectResult()
{
// Arrange
var option = Option<string>.None;
var ifSomeAsync = (string s) => Task.FromResult(s.ToUpper());
var ifNoneAsync = () => Task.FromResult("None");

// Act
var result =
await option.MatchAsAff(ifSomeAsync, ifNoneAsync)
.Run();

// Assert
Assert.Equal("None", result);
}
}
29 changes: 29 additions & 0 deletions src/Codehard.Functional/Codehard.Functional/OptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,33 @@ public static Eff<Unit> IfSomeAsEff<T>(
=> optional.Match(
Some: val => Eff(() => ifSome(val)),
None: unitEff);

/// <summary>
/// Matches the given Option&lt;T&gt; and executes the corresponding function.
/// </summary>
/// <typeparam name="T">The type of the value contained in the Option.</typeparam>
/// <param name="optional">The Option&lt;T&gt; to match.</param>
/// <param name="ifSome">The function to execute if the Option contains a value. The function takes the value as a parameter and returns a value of type T.</param>
/// <param name="ifNone">The function to execute if the Option does not contain a value. The function returns a value of type T.</param>
public static Eff<T> MatchAsEff<T>(
this Option<T> optional, Func<T, T> ifSome, Func<T> ifNone)
=> optional.Match(
Some: val => Eff(() => ifSome(val)),
None: Eff(ifNone));

/// <summary>
/// Matches the given Option&lt;T&gt; and executes the corresponding asynchronous function.
/// </summary>
/// <typeparam name="T">The type of the value contained in the Option.</typeparam>
/// <param name="optional">The Option&lt;T&gt; to match.</param>
/// <param name="ifSomeAsync">The asynchronous function to execute if the Option contains a value. The function takes the value as a parameter and returns a Task of type T.</param>
/// <param name="ifNoneAsync">The asynchronous function to execute if the Option does not contain a value. The function returns a Task of type T.</param>
/// <returns>An Aff&lt;T&gt; monad representing the result of the executed function.</returns>
public static Aff<T> MatchAsAff<T>(
this Option<T> optional,
Func<T, Task<T>> ifSomeAsync,
Func<Task<T>> ifNoneAsync)
=> optional.Match(
Some: val => Aff(async () => await ifSomeAsync(val)),
None: Aff(async () => await ifNoneAsync()));
}

0 comments on commit 5c83f6a

Please sign in to comment.