Skip to content

Commit

Permalink
Merge pull request #31 from Foxtrek64/master
Browse files Browse the repository at this point in the history
Implement FlatMap()
  • Loading branch information
Nihlus authored May 17, 2024
2 parents 4cd8c7f + 646f868 commit 799baf1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Remora.Rest.Core/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public bool TryGet([MaybeNullWhen(false)] out TValue value)
/// Applies a mapping function to the value of the <see cref="Optional{TValue}"/> if it has one; otherwise, returns
/// a new <see cref="Optional{TValue}"/> of the resulting type with no value.
/// </summary>
/// <param name="mappingFunc">The mapping function.</param>
/// <param name="mappingFunc">The mapping function, returning <typeparamref name="TResult" />.</param>
/// <typeparam name="TResult">The value type for the output of the mapping result.</typeparam>
/// <returns>
/// A new optional with the mapping result if <see cref="Optional{TValue}"/> has a value; an optional with no value
Expand All @@ -165,6 +165,23 @@ public Optional<TResult> Map<TResult>(Func<TValue, TResult> mappingFunc)
: default(Optional<TResult>);
}

/// <summary>
/// Applies a mapping function to the value of the <see cref="Optional{TValue}"/> if it has one; otherwise, returns
/// a new <see cref="Optional{TValue}"/> of the resulting type with no value.
/// </summary>
/// <param name="mappingFunc">The mapping function, returning <see cref="Optional{TValue}"/>.</param>
/// <typeparam name="TResult">The value type for the output of the mapping result.</typeparam>
/// <returns>
/// A new optional with the mapping result if <see cref="Optional{TValue}"/> has a value; an optional with no value
/// otherwise.
/// </returns>
public Optional<TResult> FlatMap<TResult>(Func<TValue, Optional<TResult>> mappingFunc)
{
return this.HasValue
? mappingFunc(_value)
: default;
}

/// <summary>
/// Implicitly converts actual values into an optional.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Tests/Remora.Rest.Core.Tests/Optional/OptionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,27 @@ public static void ReturnsOptionalWithNoValueIfInputHasNoValue()
}
}

public static class FlatMap
{
private static Optional<int> ParseInt(string s) => int.TryParse(s, out int i) ? i : default(Optional<int>);

[Fact]
public static void CanFlatMap()
{
Optional<string> one = "1";
var flatMapped = one.FlatMap(ParseInt);
Assert.Equal(new Optional<int>(1), flatMapped);
}

[Fact]
public static void ReturnsOptionalWithNoValueIfInputHasNoValue()
{
var a = default(Optional<string>);
var flatMapped = a.FlatMap(ParseInt);
Assert.False(flatMapped.HasValue);
}
}

public static class ConvertNullToEmpty
{
public static class Struct
Expand Down

0 comments on commit 799baf1

Please sign in to comment.