Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FlatMap() #31

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading