From 0d26cdd4ccf801f7e0afb076a08fae19f5467348 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:33:29 +0100 Subject: [PATCH 1/2] feat: add allocation-free overloads of Maybe.Map --- .../Extensions/MapTests.Task.Left.cs | 80 +++++++++++------- .../Extensions/MapTests.Task.Right.cs | 78 ++++++++++------- .../MaybeTests/Extensions/MapTests.Task.cs | 80 +++++++++++------- .../Extensions/MapTests.ValueTask.Left.cs | 82 +++++++++++------- .../Extensions/MapTests.ValueTask.Right.cs | 80 +++++++++++------- .../Extensions/MapTests.ValueTask.cs | 84 ++++++++++++------- .../MaybeTests/Extensions/MapTests.cs | 76 ++++++++++------- .../Maybe/Extensions/Map.Task.Left.cs | 41 +++++---- .../Maybe/Extensions/Map.Task.Right.cs | 44 ++++++---- .../Maybe/Extensions/Map.Task.cs | 41 +++++---- .../Maybe/Extensions/Map.ValueTask.Left.cs | 45 ++++++---- .../Maybe/Extensions/Map.ValueTask.Right.cs | 51 +++++++---- .../Maybe/Extensions/Map.ValueTask.cs | 45 ++++++---- .../Maybe/Extensions/Map.cs | 42 ++++++---- 14 files changed, 548 insertions(+), 321 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs index 75b44569..5d8b4e33 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs @@ -1,30 +1,50 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_Task_Left : MaybeTestBase - { - [Fact] - public async Task Map_Task_Left_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_Task_Left : MaybeTestBase + { + [Fact] + public async Task Map_Task_Left_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs index 5938e7fe..0ae8f63a 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs @@ -1,30 +1,48 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_Task_Right : MaybeTestBase - { - [Fact] - public async Task Map_Task_Right_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.Map(ExpectAndReturn_Task(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.Map(ExpectAndReturn_Task(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_Task_Right : MaybeTestBase + { + [Fact] + public async Task Map_Task_Right_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Map(ExpectAndReturn_Task(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.Map(ExpectAndReturn_Task(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs index d582d7f8..4f2fc8b4 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs @@ -1,30 +1,50 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_Task : MaybeTestBase - { - [Fact] - public async Task Map_Task_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_Task : MaybeTestBase + { + [Fact] + public async Task Map_Task_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs index eb9ffa98..4f41991b 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs @@ -1,31 +1,51 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_ValueTask_Left : MaybeTestBase - { - [Fact] - public async Task Map_ValueTask_Left_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_ValueTask_Left : MaybeTestBase + { + [Fact] + public async Task Map_ValueTask_Left_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs index f5e86436..5628a5a7 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs @@ -1,31 +1,49 @@ -using System.Threading.Tasks; -using FluentAssertions; -using CSharpFunctionalExtensions.ValueTasks; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_ValueTask_Right : MaybeTestBase - { - [Fact] - public async Task Map_ValueTask_Right_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_ValueTask_Right_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_ValueTask_Right : MaybeTestBase + { + [Fact] + public async Task Map_ValueTask_Right_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_ValueTask_Right_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs index 8939195f..2689d01f 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs @@ -1,31 +1,53 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_ValueTask : MaybeTestBase - { - [Fact] - public async Task Map_ValueTask_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn_ValueTask(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_ValueTask_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn_ValueTask(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} \ No newline at end of file +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_ValueTask : MaybeTestBase + { + [Fact] + public async Task Map_ValueTask_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe + .AsValueTask() + .Map(ExpectAndReturn_ValueTask(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_ValueTask_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn_ValueTask(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs index d2308596..05e46875 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs @@ -1,29 +1,47 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests : MaybeTestBase - { - [Fact] - public void Map_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = maybe.Map(ExpectAndReturn(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public void Map_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = maybe.Map(ExpectAndReturn(null, T.Value2)); - - maybe2.HasValue.Should().BeFalse(); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests : MaybeTestBase + { + [Fact] + public void Map_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = maybe.Map(ExpectAndReturn(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public void Map_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = maybe.Map(ExpectAndReturn(null, T.Value2)); + + 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..ea0dc134 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs @@ -1,14 +1,27 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static async Task> Map(this Task> maybeTask, Func selector) - { - var maybe = await maybeTask.DefaultAwait(); - return maybe.Map(selector); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + 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); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs index 622df978..8e1ae7b3 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs @@ -1,16 +1,28 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static async Task> Map(this Maybe maybe, Func> selector) - { - if (maybe.HasNoValue) - return Maybe.None; - - return await selector(maybe.GetValueOrThrow()).DefaultAwait(); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static async Task> Map(this Maybe maybe, Func> selector) + { + if (maybe.HasNoValue) + return Maybe.None; + + return await selector(maybe.GetValueOrThrow()).DefaultAwait(); + } + + public static async Task> Map( + this Maybe maybe, + Func> selector, + TContext context + ) + { + if (maybe.HasNoValue) + return Maybe.None; + + return await selector(maybe.GetValueOrThrow(), context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs index 56efba01..65dbec0f 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs @@ -1,14 +1,27 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static async Task> Map(this Task> maybeTask, Func> selector) - { - var maybe = await maybeTask.DefaultAwait(); - return await maybe.Map(selector).DefaultAwait(); - } - } -} \ No newline at end of file +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs index 4719fc96..f006db2e 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs @@ -1,16 +1,29 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - public static async ValueTask> Map(this ValueTask> valueTask, Func selector) - { - Maybe maybe = await valueTask; - return maybe.Map(selector); - } - } -} -#endif \ No newline at end of file +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + 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 diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs index 40f5983d..fa520c92 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs @@ -1,18 +1,33 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - public static async ValueTask> Map(this Maybe maybe, Func> valueTask) - { - if (maybe.HasNoValue) - return Maybe.None; - - return await valueTask(maybe.GetValueOrThrow()); - } - } -} -#endif \ No newline at end of file +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + 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 diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs index 7266767c..33f19fcb 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs @@ -1,16 +1,29 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - public static async ValueTask> Map(this ValueTask> maybeTask, Func> valueTask) - { - Maybe maybe = await maybeTask; - return await maybe.Map(valueTask); - } - } -} -#endif \ No newline at end of file +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + 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 diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs index f79d79d3..1834a7f7 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs @@ -1,15 +1,27 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static Maybe Map(in this Maybe maybe, Func selector) - { - if (maybe.HasNoValue) - return Maybe.None; - - return selector(maybe.GetValueOrThrow()); - } - } -} \ No newline at end of file +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static Maybe Map(in this Maybe maybe, Func selector) + { + if (maybe.HasNoValue) + return Maybe.None; + + 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); + } + } +} From 9cfd241e0b4ef42610ca899ff8be20ebe0c1ec46 Mon Sep 17 00:00:00 2001 From: Marcin Jahn <10273406+marcinjahn@users.noreply.github.com> Date: Sat, 23 Nov 2024 08:35:23 +0100 Subject: [PATCH 2/2] Restore line endings --- .../Extensions/MapTests.Task.Left.cs | 100 ++++++++--------- .../Extensions/MapTests.Task.Right.cs | 96 ++++++++-------- .../MaybeTests/Extensions/MapTests.Task.cs | 100 ++++++++--------- .../Extensions/MapTests.ValueTask.Left.cs | 102 ++++++++--------- .../Extensions/MapTests.ValueTask.Right.cs | 98 ++++++++-------- .../Extensions/MapTests.ValueTask.cs | 106 +++++++++--------- .../MaybeTests/Extensions/MapTests.cs | 94 ++++++++-------- .../Maybe/Extensions/Map.Task.Left.cs | 54 ++++----- .../Maybe/Extensions/Map.Task.Right.cs | 56 ++++----- .../Maybe/Extensions/Map.Task.cs | 54 ++++----- .../Maybe/Extensions/Map.ValueTask.Left.cs | 58 +++++----- .../Maybe/Extensions/Map.ValueTask.Right.cs | 66 +++++------ .../Maybe/Extensions/Map.ValueTask.cs | 58 +++++----- .../Maybe/Extensions/Map.cs | 54 ++++----- 14 files changed, 548 insertions(+), 548 deletions(-) diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs index 5d8b4e33..44638019 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Left.cs @@ -1,50 +1,50 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_Task_Left : MaybeTestBase - { - [Fact] - public async Task Map_Task_Left_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(null, T.Value2)); - - 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(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_Task_Left : MaybeTestBase + { + [Fact] + public async Task Map_Task_Left_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_Task_Left_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs index 0ae8f63a..d4cc46fc 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.Right.cs @@ -1,48 +1,48 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_Task_Right : MaybeTestBase - { - [Fact] - public async Task Map_Task_Right_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.Map(ExpectAndReturn_Task(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.Map(ExpectAndReturn_Task(null, T.Value2)); - - 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(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_Task_Right : MaybeTestBase + { + [Fact] + public async Task Map_Task_Right_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Map(ExpectAndReturn_Task(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_Task_Right_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.Map(ExpectAndReturn_Task(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs index 4f2fc8b4..8ddf47d7 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.Task.cs @@ -1,50 +1,50 @@ -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_Task : MaybeTestBase - { - [Fact] - public async Task Map_Task_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(null, T.Value2)); - - 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(); - } - } -} +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_Task : MaybeTestBase + { + [Fact] + public async Task Map_Task_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_Task_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsTask().Map(ExpectAndReturn_Task(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs index 4f41991b..c1391154 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Left.cs @@ -1,51 +1,51 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_ValueTask_Left : MaybeTestBase - { - [Fact] - public async Task Map_ValueTask_Left_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(null, T.Value2)); - - 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(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_ValueTask_Left : MaybeTestBase + { + [Fact] + public async Task Map_ValueTask_Left_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_ValueTask_Left_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs index 5628a5a7..1d729ba1 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.Right.cs @@ -1,49 +1,49 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_ValueTask_Right : MaybeTestBase - { - [Fact] - public async Task Map_ValueTask_Right_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_ValueTask_Right_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(null, T.Value2)); - - 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(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_ValueTask_Right : MaybeTestBase + { + [Fact] + public async Task Map_ValueTask_Right_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_ValueTask_Right_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.Map(valueTask: ExpectAndReturn_ValueTask(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs index 2689d01f..f1f8dc93 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.ValueTask.cs @@ -1,53 +1,53 @@ -using System.Threading.Tasks; -using CSharpFunctionalExtensions.ValueTasks; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests_ValueTask : MaybeTestBase - { - [Fact] - public async Task Map_ValueTask_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = await maybe - .AsValueTask() - .Map(ExpectAndReturn_ValueTask(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public async Task Map_ValueTask_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn_ValueTask(null, T.Value2)); - - 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(); - } - } -} +using System.Threading.Tasks; +using CSharpFunctionalExtensions.ValueTasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests_ValueTask : MaybeTestBase + { + [Fact] + public async Task Map_ValueTask_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = await maybe + .AsValueTask() + .Map(ExpectAndReturn_ValueTask(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public async Task Map_ValueTask_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = await maybe.AsValueTask().Map(ExpectAndReturn_ValueTask(null, T.Value2)); + + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs index 05e46875..3d435e39 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MapTests.cs @@ -1,47 +1,47 @@ -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions -{ - public class MapTests : MaybeTestBase - { - [Fact] - public void Map_returns_mapped_value() - { - Maybe maybe = T.Value; - - var maybe2 = maybe.Map(ExpectAndReturn(T.Value, T.Value2)); - - maybe2.HasValue.Should().BeTrue(); - maybe2.Value.Should().Be(T.Value2); - } - - [Fact] - public void Map_returns_no_value_if_initial_maybe_is_null() - { - Maybe maybe = null; - - var maybe2 = maybe.Map(ExpectAndReturn(null, T.Value2)); - - 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(); - } - } -} +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions +{ + public class MapTests : MaybeTestBase + { + [Fact] + public void Map_returns_mapped_value() + { + Maybe maybe = T.Value; + + var maybe2 = maybe.Map(ExpectAndReturn(T.Value, T.Value2)); + + maybe2.HasValue.Should().BeTrue(); + maybe2.Value.Should().Be(T.Value2); + } + + [Fact] + public void Map_returns_no_value_if_initial_maybe_is_null() + { + Maybe maybe = null; + + var maybe2 = maybe.Map(ExpectAndReturn(null, T.Value2)); + + 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 ea0dc134..4c5a6e8b 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Left.cs @@ -1,27 +1,27 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - 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); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + 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); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs index 8e1ae7b3..66a068f3 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.Right.cs @@ -1,28 +1,28 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static async Task> Map(this Maybe maybe, Func> selector) - { - if (maybe.HasNoValue) - return Maybe.None; - - return await selector(maybe.GetValueOrThrow()).DefaultAwait(); - } - - public static async Task> Map( - this Maybe maybe, - Func> selector, - TContext context - ) - { - if (maybe.HasNoValue) - return Maybe.None; - - return await selector(maybe.GetValueOrThrow(), context).DefaultAwait(); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static async Task> Map(this Maybe maybe, Func> selector) + { + if (maybe.HasNoValue) + return Maybe.None; + + return await selector(maybe.GetValueOrThrow()).DefaultAwait(); + } + + public static async Task> Map( + this Maybe maybe, + Func> selector, + TContext context + ) + { + if (maybe.HasNoValue) + return Maybe.None; + + return await selector(maybe.GetValueOrThrow(), context).DefaultAwait(); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs index 65dbec0f..10a884de 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.Task.cs @@ -1,27 +1,27 @@ -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - 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(); - } - } -} +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + 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(); + } + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs index f006db2e..d36f0450 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Left.cs @@ -1,29 +1,29 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - 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 +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + 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 diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs index fa520c92..f5cb7876 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.Right.cs @@ -1,33 +1,33 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - 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 +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + 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 diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs index 33f19fcb..c77ee867 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.ValueTask.cs @@ -1,29 +1,29 @@ -#if NET5_0_OR_GREATER -using System; -using System.Threading.Tasks; - -namespace CSharpFunctionalExtensions.ValueTasks -{ - public static partial class MaybeExtensions - { - 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 +#if NET5_0_OR_GREATER +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions.ValueTasks +{ + public static partial class MaybeExtensions + { + 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 diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs index 1834a7f7..577c3069 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Map.cs @@ -1,27 +1,27 @@ -using System; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static Maybe Map(in this Maybe maybe, Func selector) - { - if (maybe.HasNoValue) - return Maybe.None; - - 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); - } - } -} +using System; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static Maybe Map(in this Maybe maybe, Func selector) + { + if (maybe.HasNoValue) + return Maybe.None; + + 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); + } + } +}