diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs index 75b44569..44638019 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs @@ -26,5 +26,25 @@ public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public async Task Map_Task_Left_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe + .AsTask() + .Map( + (value, ctx) => + { + ctx.Should().Be(context); + return value; + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs index 5938e7fe..d4cc46fc 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs @@ -26,5 +26,23 @@ public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public async Task Map_Task_Right_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.Map( + (value, ctx) => + { + ctx.Should().Be(context); + return value.AsTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs index d582d7f8..8ddf47d7 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs @@ -26,5 +26,25 @@ public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public async Task Map_Task_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe + .AsTask() + .Map( + (value, ctx) => + { + ctx.Should().Be(context); + return value.AsTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs index eb9ffa98..c1391154 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs @@ -27,5 +27,25 @@ public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public async Task Map_ValueTask_Left_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe + .AsValueTask() + .Map( + (value, ctx) => + { + ctx.Should().Be(context); + return value; + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs index f5e86436..1d729ba1 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; -using FluentAssertions; using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; using Xunit; namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions @@ -27,5 +27,23 @@ public async Task Map_ValueTask_Right_returns_no_value_if_initial_maybe_is_null( maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public async Task Map_ValueTask_Right_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe.Map( + valueTask: (value, ctx) => + { + ctx.Should().Be(context); + return value.AsValueTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs index 8939195f..f1f8dc93 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs @@ -12,7 +12,9 @@ public async Task Map_ValueTask_returns_mapped_value() { Maybe maybe = T.Value; - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn_ValueTask(T.Value, T.Value2)); + var maybe2 = await maybe + .AsValueTask() + .Map(ExpectAndReturn_ValueTask(T.Value, T.Value2)); maybe2.HasValue.Should().BeTrue(); maybe2.Value.Should().Be(T.Value2); @@ -27,5 +29,25 @@ public async Task Map_ValueTask_returns_no_value_if_initial_maybe_is_null() maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public async Task Map_ValueTask_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = await maybe + .AsValueTask() + .Map( + (value, ctx) => + { + ctx.Should().Be(context); + return value.AsValueTask(); + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs index d2308596..3d435e39 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs @@ -25,5 +25,23 @@ public void Map_returns_no_value_if_initial_maybe_is_null() maybe2.HasValue.Should().BeFalse(); } + + [Fact] + public void Map_provides_context_to_selector() + { + Maybe maybe = T.Value; + var context = 5; + + var maybe2 = maybe.Map( + (value, ctx) => + { + ctx.Should().Be(context); + return value; + }, + context + ); + + maybe2.HasValue.Should().BeTrue(); + } } } diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs index c98325b1..4c5a6e8b 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs @@ -5,10 +5,23 @@ namespace CSharpFunctionalExtensions { public static partial class MaybeExtensions { - public static async Task> Map(this Task> maybeTask, Func selector) + public static async Task> Map( + this Task> maybeTask, + Func selector + ) { var maybe = await maybeTask.DefaultAwait(); return maybe.Map(selector); } + + public static async Task> Map( + this Task> maybeTask, + Func selector, + TContext context + ) + { + var maybe = await maybeTask.DefaultAwait(); + return maybe.Map(selector, context); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs index 622df978..66a068f3 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs @@ -12,5 +12,17 @@ public static async Task> Map(this Maybe maybe, Func> Map( + this Maybe maybe, + Func> selector, + TContext context + ) + { + if (maybe.HasNoValue) + return Maybe.None; + + return await selector(maybe.GetValueOrThrow(), context).DefaultAwait(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs index 56efba01..10a884de 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs @@ -5,10 +5,23 @@ namespace CSharpFunctionalExtensions { public static partial class MaybeExtensions { - public static async Task> Map(this Task> maybeTask, Func> selector) + public static async Task> Map( + this Task> maybeTask, + Func> selector + ) { var maybe = await maybeTask.DefaultAwait(); return await maybe.Map(selector).DefaultAwait(); } + + public static async Task> Map( + this Task> maybeTask, + Func> selector, + TContext context + ) + { + var maybe = await maybeTask.DefaultAwait(); + return await maybe.Map(selector, context).DefaultAwait(); + } } -} \ No newline at end of file +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs index 4719fc96..d36f0450 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs @@ -6,11 +6,24 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class MaybeExtensions { - public static async ValueTask> Map(this ValueTask> valueTask, Func selector) + public static async ValueTask> Map( + this ValueTask> valueTask, + Func selector + ) { Maybe maybe = await valueTask; return maybe.Map(selector); } + + public static async ValueTask> Map( + this ValueTask> valueTask, + Func selector, + TContext context + ) + { + Maybe maybe = await valueTask; + return maybe.Map(selector, context); + } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs index 40f5983d..f5cb7876 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs @@ -6,13 +6,28 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class MaybeExtensions { - public static async ValueTask> Map(this Maybe maybe, Func> valueTask) + public static async ValueTask> Map( + this Maybe maybe, + Func> valueTask + ) { if (maybe.HasNoValue) return Maybe.None; return await valueTask(maybe.GetValueOrThrow()); } + + public static async ValueTask> Map( + this Maybe maybe, + Func> valueTask, + TContext context + ) + { + if (maybe.HasNoValue) + return Maybe.None; + + return await valueTask(maybe.GetValueOrThrow(), context); + } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs index 7266767c..c77ee867 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs @@ -6,11 +6,24 @@ namespace CSharpFunctionalExtensions.ValueTasks { public static partial class MaybeExtensions { - public static async ValueTask> Map(this ValueTask> maybeTask, Func> valueTask) + public static async ValueTask> Map( + this ValueTask> maybeTask, + Func> valueTask + ) { Maybe maybe = await maybeTask; return await maybe.Map(valueTask); } + + public static async ValueTask> Map( + this ValueTask> maybeTask, + Func> valueTask, + TContext context + ) + { + Maybe maybe = await maybeTask; + return await maybe.Map(valueTask, context); + } } } -#endif \ No newline at end of file +#endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs index f79d79d3..577c3069 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs @@ -11,5 +11,17 @@ public static Maybe Map(in this Maybe maybe, Func selector) return selector(maybe.GetValueOrThrow()); } + + public static Maybe Map( + in this Maybe maybe, + Func selector, + TContext context + ) + { + if (maybe.HasNoValue) + return Maybe.None; + + return selector(maybe.GetValueOrThrow(), context); + } } -} \ No newline at end of file +}