diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.Task.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.Task.cs index 0f975ca2..8f721f40 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.Task.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.Task.cs @@ -29,6 +29,33 @@ await maybe.Match( ); } + [Fact] + public async Task Match_provides_context_to_some_selector() + { + Maybe maybe = T.Value; + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (value, ctx, ct) => + { + ct.Should().Be(cancellationToken); + value.Should().Be(T.Value); + ctx.Should().Be(context); + contextAsserted = true; + + return Task.CompletedTask; + }, + None: (_, _) => + throw new FieldAccessException("Accessed None path while maybe has value"), + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_follows_none_branch_where_is_no_value() { @@ -47,6 +74,32 @@ await maybe.Match( ); } + [Fact] + public async Task Match_provides_context_to_none_selector() + { + Maybe maybe = null; + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (_, _, _) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx, ct) => + { + ctx.Should().Be(context); + contextAsserted = true; + ct.Should().Be(cancellationToken); + + return Task.CompletedTask; + }, + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_no_value() { @@ -68,6 +121,35 @@ await maybe.Match( ); } + [Fact] + public async Task Match_for_deconstruct_kv_provides_context_to_some_selector() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (intValue, stringValue, ctx, ct) => + { + intValue.Should().Be(42); + stringValue.Should().Be("Matrix"); + ctx.Should().Be(context); + contextAsserted = true; + + return Task.CompletedTask; + }, + None: (_, _) => + throw new FieldAccessException("Accessed None path while maybe has value"), + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_no_value() { @@ -86,6 +168,32 @@ await maybe.Match( ); } + [Fact] + public async Task Match_for_deconstruct_kv_provides_context_to_none_selector() + { + Maybe> maybe = Maybe>.None; + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (_, _, _, _) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx, ct) => + { + ctx.Should().Be(context); + contextAsserted = true; + ct.Should().Be(cancellationToken); + + return Task.CompletedTask; + }, + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return_value() { @@ -100,7 +208,7 @@ public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return intValue.Should().Be(42); stringValue.Should().Be("Matrix"); - return Task.FromResult(intValue); + return intValue.AsTask(); }, None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), cancellationToken @@ -109,6 +217,34 @@ public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return 42.Should().Be(returnValue); } + [Fact] + public async Task Match_for_deconstruct_kv_with_return_value_provides_context_to_some_selector() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + var returnValue = await maybe.Match( + Some: (intValue, stringValue, ctx, ct) => + { + ctx.Should().Be(context); + contextAsserted = true; + + return intValue.AsTask(); + }, + None: (_, _) => + throw new FieldAccessException("Accessed None path while maybe has value"), + context: context, + cancellationToken + ); + + 42.Should().Be(returnValue); + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_a_return_value() { @@ -121,12 +257,38 @@ public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_a_return None: (ct) => { ct.Should().Be(cancellationToken); - return Task.FromResult(-1); + return (-1).AsTask(); + }, + cancellationToken + ); + + (-1).Should().Be(returnValue); + } + + [Fact] + public async Task Match_for_deconstruct_kv_with_return_value_provides_context_to_none_selector() + { + Maybe> maybe = Maybe>.None; + var context = 5; + var contextAsserted = false; + var cancellationToken = GetCancellationToken(); + + var returnValue = await maybe.Match( + Some: (intValue, stringValue, ctx, ct) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx, _) => + { + ctx.Should().Be(context); + contextAsserted = true; + + return (-1).AsTask(); }, + context: context, cancellationToken ); (-1).Should().Be(returnValue); + contextAsserted.Should().BeTrue(); } private static CancellationToken GetCancellationToken() => new(canceled: true); diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.ValueTask.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.ValueTask.cs index 673d34c2..8e8e94d6 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.ValueTask.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.ValueTask.cs @@ -29,6 +29,33 @@ await maybe.Match( ); } + [Fact] + public async Task Match_provides_context_to_some_selector() + { + Maybe maybe = T.Value; + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (value, ctx, ct) => + { + ct.Should().Be(cancellationToken); + value.Should().Be(T.Value); + ctx.Should().Be(context); + contextAsserted = true; + + return ValueTask.CompletedTask; + }, + None: (_, _) => + throw new FieldAccessException("Accessed None path while maybe has value"), + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_follows_none_branch_where_is_no_value() { @@ -47,6 +74,32 @@ await maybe.Match( ); } + [Fact] + public async Task Match_provides_context_to_none_selector() + { + Maybe maybe = null; + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (_, _, _) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx, ct) => + { + ctx.Should().Be(context); + contextAsserted = true; + ct.Should().Be(cancellationToken); + + return ValueTask.CompletedTask; + }, + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_no_value() { @@ -68,6 +121,35 @@ await maybe.Match( ); } + [Fact] + public async Task Match_for_deconstruct_kv_provides_context_to_some_selector() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (intValue, stringValue, ctx, ct) => + { + intValue.Should().Be(42); + stringValue.Should().Be("Matrix"); + ctx.Should().Be(context); + contextAsserted = true; + + return ValueTask.CompletedTask; + }, + None: (_, _) => + throw new FieldAccessException("Accessed None path while maybe has value"), + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_no_value() { @@ -86,6 +168,32 @@ await maybe.Match( ); } + [Fact] + public async Task Match_for_deconstruct_kv_provides_context_to_none_selector() + { + Maybe> maybe = Maybe>.None; + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + await maybe.Match( + Some: (_, _, _, _) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx, ct) => + { + ctx.Should().Be(context); + contextAsserted = true; + ct.Should().Be(cancellationToken); + + return ValueTask.CompletedTask; + }, + context: context, + cancellationToken + ); + + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return_value() { @@ -100,7 +208,7 @@ public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return intValue.Should().Be(42); stringValue.Should().Be("Matrix"); - return ValueTask.FromResult(intValue); + return intValue.AsValueTask(); }, None: _ => throw new FieldAccessException("Accessed None path while maybe has value"), cancellationToken @@ -109,6 +217,34 @@ public async Task Match_for_deconstruct_kv_follows_some_branch_where_is_a_return 42.Should().Be(returnValue); } + [Fact] + public async Task Match_for_deconstruct_kv_with_return_value_provides_context_to_some_selector() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + var cancellationToken = GetCancellationToken(); + var context = 5; + var contextAsserted = false; + + var returnValue = await maybe.Match( + Some: (intValue, stringValue, ctx, ct) => + { + ctx.Should().Be(context); + contextAsserted = true; + + return intValue.AsValueTask(); + }, + None: (_, _) => + throw new FieldAccessException("Accessed None path while maybe has value"), + context: context, + cancellationToken + ); + + 42.Should().Be(returnValue); + contextAsserted.Should().BeTrue(); + } + [Fact] public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_a_return_value() { @@ -121,12 +257,38 @@ public async Task Match_for_deconstruct_kv_follows_none_branch_where_is_a_return None: (ct) => { ct.Should().Be(cancellationToken); - return ValueTask.FromResult(-1); + return (-1).AsValueTask(); + }, + cancellationToken + ); + + (-1).Should().Be(returnValue); + } + + [Fact] + public async Task Match_for_deconstruct_kv_with_return_value_provides_context_to_none_selector() + { + Maybe> maybe = Maybe>.None; + var context = 5; + var contextAsserted = false; + var cancellationToken = GetCancellationToken(); + + var returnValue = await maybe.Match( + Some: (intValue, stringValue, ctx, ct) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx, _) => + { + ctx.Should().Be(context); + contextAsserted = true; + + return (-1).AsValueTask(); }, + context: context, cancellationToken ); (-1).Should().Be(returnValue); + contextAsserted.Should().BeTrue(); } private static CancellationToken GetCancellationToken() => new(canceled: true); diff --git a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.cs b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.cs index 7007fcb0..96e3d74f 100644 --- a/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.cs +++ b/CSharpFunctionalExtensions.Tests/MaybeTests/Extensions/MatchTests.cs @@ -1,85 +1,227 @@ -using System; -using System.Collections.Generic; -using CSharpFunctionalExtensions.Tests.ResultTests.Extensions; -using FluentAssertions; -using Xunit; - -namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; - -public class MatchTests : MatchTestsBase -{ - [Fact] - public void Match_follows_some_branch_where_there_is_a_value() - { - Maybe maybe = T.Value; - - maybe.Match( - Some: value => value.Should().Be(T.Value), - None: () => throw new FieldAccessException("Accessed None path while maybe has value") - ); - } - - [Fact] - public void Match_follows_none_branch_where_is_no_value() - { - Maybe maybe = null; - - maybe.Match( - Some: _ => throw new FieldAccessException("Accessed Some path while maybe has no value"), - None: () => Assert.True(true) - ); - } - - [Fact] - public void Match_for_deconstruct_kv_follows_some_branch_where_is_no_value() - { - Maybe> maybe = Maybe>.From(new KeyValuePair(42, "Matrix")); - - maybe.Match( - Some: (intValue, stringValue) => - { - intValue.Should().Be(42); - stringValue.Should().Be("Matrix"); - }, - None: () => throw new FieldAccessException("Accessed None path while maybe has value") - ); - } - - [Fact] - public void Match_for_deconstruct_kv_follows_none_branch_where_is_no_value() - { - Maybe> maybe = Maybe>.None; - - maybe.Match( - Some: (intValue, stringValue) => throw new FieldAccessException("Accessed Some path while maybe has no value"), - None: () => Assert.True(true) - ); - } - - [Fact] - public void Match_for_deconstruct_kv_follows_some_branch_where_is_a_return_value() - { - Maybe> maybe = Maybe>.From(new KeyValuePair(42, "Matrix")); - - var returnValue = maybe.Match( - Some: (intValue, stringValue) => intValue, - None: () => throw new FieldAccessException("Accessed None path while maybe has value") - ); - - 42.Should().Be(returnValue); - } - - [Fact] - public void Match_for_deconstruct_kv_follows_none_branch_where_is_a_return_value() - { - Maybe> maybe = Maybe>.None; - - var returnValue = maybe.Match( - Some: (intValue, stringValue) => throw new FieldAccessException("Accessed Some path while maybe has no value"), - None: () => -1 - ); - - (-1).Should().Be(returnValue); - - } -} \ No newline at end of file +using System; +using System.Collections.Generic; +using CSharpFunctionalExtensions.Tests.ResultTests.Extensions; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions; + +public class MatchTests : MatchTestsBase +{ + [Fact] + public void Match_follows_some_branch_where_there_is_a_value() + { + Maybe maybe = T.Value; + + maybe.Match( + Some: value => value.Should().Be(T.Value), + None: () => throw new FieldAccessException("Accessed None path while maybe has value") + ); + } + + [Fact] + public void Match_provides_context_to_some_selector() + { + Maybe maybe = T.Value; + var context = 5; + var contextAsserted = false; + + maybe.Match( + Some: (value, ctx) => + { + ctx.Should().Be(context); + value.Should().Be(T.Value); + contextAsserted = true; + }, + None: (_) => throw new FieldAccessException("Accessed None path while maybe has value"), + context: context + ); + + contextAsserted.Should().BeTrue(); + } + + [Fact] + public void Match_follows_none_branch_where_is_no_value() + { + Maybe maybe = null; + + maybe.Match( + Some: _ => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: () => Assert.True(true) + ); + } + + [Fact] + public void Match_provides_context_to_none_selector() + { + Maybe maybe = null; + var context = 5; + var contextAsserted = false; + + maybe.Match( + Some: (_, _) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx) => + { + ctx.Should().Be(context); + contextAsserted = true; + Assert.True(true); + }, + context: context + ); + + contextAsserted.Should().BeTrue(); + } + + [Fact] + public void Match_for_deconstruct_kv_follows_some_branch_where_is_no_value() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + + maybe.Match( + Some: (intValue, stringValue) => + { + intValue.Should().Be(42); + stringValue.Should().Be("Matrix"); + }, + None: () => throw new FieldAccessException("Accessed None path while maybe has value") + ); + } + + [Fact] + public void Match_for_deconstruct_kv_provides_context_to_some_selector() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + var context = 5; + var contextAsserted = false; + + maybe.Match( + Some: (intValue, stringValue, ctx) => + { + intValue.Should().Be(42); + stringValue.Should().Be("Matrix"); + ctx.Should().Be(context); + contextAsserted = true; + }, + None: (_) => throw new FieldAccessException("Accessed None path while maybe has value"), + context: context + ); + + contextAsserted.Should().BeTrue(); + } + + [Fact] + public void Match_for_deconstruct_kv_follows_none_branch_where_is_no_value() + { + Maybe> maybe = Maybe>.None; + + maybe.Match( + Some: (intValue, stringValue) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: () => Assert.True(true) + ); + } + + [Fact] + public void Match_for_deconstruct_kv_provides_context_to_none_selector() + { + Maybe> maybe = Maybe>.None; + var context = 5; + var contextAsserted = false; + + maybe.Match( + Some: (intValue, stringValue, ctx) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx) => + { + ctx.Should().Be(context); + contextAsserted = true; + }, + context: context + ); + + contextAsserted.Should().BeTrue(); + } + + [Fact] + public void Match_for_deconstruct_kv_follows_some_branch_where_is_a_return_value() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + + var returnValue = maybe.Match( + Some: (intValue, stringValue) => intValue, + None: () => throw new FieldAccessException("Accessed None path while maybe has value") + ); + + 42.Should().Be(returnValue); + } + + [Fact] + public void Match_for_deconstruct_kv_with_return_value_provides_context_to_some_selector() + { + Maybe> maybe = Maybe>.From( + new KeyValuePair(42, "Matrix") + ); + var context = 5; + var contextAsserted = false; + + var returnValue = maybe.Match( + Some: (intValue, stringValue, ctx) => + { + ctx.Should().Be(context); + contextAsserted = true; + + return intValue; + }, + None: (_) => throw new FieldAccessException("Accessed None path while maybe has value"), + context: context + ); + + 42.Should().Be(returnValue); + contextAsserted.Should().BeTrue(); + } + + [Fact] + public void Match_for_deconstruct_kv_follows_none_branch_where_is_a_return_value() + { + Maybe> maybe = Maybe>.None; + + var returnValue = maybe.Match( + Some: (intValue, stringValue) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: () => -1 + ); + + (-1).Should().Be(returnValue); + } + + [Fact] + public void Match_for_deconstruct_kv_with_return_value_provides_context_to_none_selector() + { + Maybe> maybe = Maybe>.None; + var context = 5; + var contextAsserted = false; + + var returnValue = maybe.Match( + Some: (intValue, stringValue, ctx) => + throw new FieldAccessException("Accessed Some path while maybe has no value"), + None: (ctx) => + { + ctx.Should().Be(context); + contextAsserted = true; + return -1; + }, + context: context + ); + + (-1).Should().Be(returnValue); + contextAsserted.Should().BeTrue(); + } +} diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Match.Task.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Match.Task.cs index 4546054c..0ea4bd1d 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Match.Task.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Match.Task.cs @@ -19,6 +19,19 @@ public static async Task Match( : await None(cancellationToken); } + public static async Task Match( + this Maybe maybe, + Func> Some, + Func> None, + TContext context, + CancellationToken cancellationToken = default + ) + { + return maybe.HasValue + ? await Some(maybe.GetValueOrThrow(), context, cancellationToken) + : await None(context, cancellationToken); + } + public static async Task Match( this Maybe maybe, Func Some, @@ -32,6 +45,20 @@ public static async Task Match( await None(cancellationToken); } + public static async Task Match( + this Maybe maybe, + Func Some, + Func None, + TContext context, + CancellationToken cancellationToken = default + ) + { + if (maybe.HasValue) + await Some(maybe.GetValueOrThrow(), context, cancellationToken); + else + await None(context, cancellationToken); + } + public static async Task Match( this Maybe> maybe, Func> Some, @@ -48,6 +75,24 @@ public static async Task Match( : await None.Invoke(cancellationToken); } + public static async Task Match( + this Maybe> maybe, + Func> Some, + Func> None, + TContext context, + CancellationToken cancellationToken = default + ) + { + return maybe.HasValue + ? await Some.Invoke( + maybe.GetValueOrThrow().Key, + maybe.GetValueOrThrow().Value, + context, + cancellationToken + ) + : await None.Invoke(context, cancellationToken); + } + public static async Task Match( this Maybe> maybe, Func Some, @@ -68,5 +113,28 @@ await Some.Invoke( await None.Invoke(cancellationToken); } } + + public static async Task Match( + this Maybe> maybe, + Func Some, + Func None, + TContext context, + CancellationToken cancellationToken = default + ) + { + if (maybe.HasValue) + { + await Some.Invoke( + maybe.GetValueOrThrow().Key, + maybe.GetValueOrThrow().Value, + context, + cancellationToken + ); + } + else + { + await None.Invoke(context, cancellationToken); + } + } } } diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Match.ValueTask.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Match.ValueTask.cs index 78c35b63..d3c95259 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Match.ValueTask.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Match.ValueTask.cs @@ -20,6 +20,19 @@ public static async ValueTask Match( : await None(cancellationToken); } + public static async ValueTask Match( + this Maybe maybe, + Func> Some, + Func> None, + TContext context, + CancellationToken cancellationToken = default + ) + { + return maybe.HasValue + ? await Some(maybe.GetValueOrThrow(), context, cancellationToken) + : await None(context, cancellationToken); + } + public static async ValueTask Match( this Maybe maybe, Func Some, @@ -33,6 +46,20 @@ public static async ValueTask Match( await None(cancellationToken); } + public static async ValueTask Match( + this Maybe maybe, + Func Some, + Func None, + TContext context, + CancellationToken cancellationToken = default + ) + { + if (maybe.HasValue) + await Some(maybe.GetValueOrThrow(), context, cancellationToken); + else + await None(context, cancellationToken); + } + public static async ValueTask Match( this Maybe> maybe, Func> Some, @@ -49,6 +76,24 @@ public static async ValueTask Match( : await None.Invoke(cancellationToken); } + public static async ValueTask Match( + this Maybe> maybe, + Func> Some, + Func> None, + TContext context, + CancellationToken cancellationToken = default + ) + { + return maybe.HasValue + ? await Some.Invoke( + maybe.GetValueOrThrow().Key, + maybe.GetValueOrThrow().Value, + context, + cancellationToken + ) + : await None.Invoke(context, cancellationToken); + } + public static async ValueTask Match( this Maybe> maybe, Func Some, @@ -69,6 +114,29 @@ await Some.Invoke( await None.Invoke(cancellationToken); } } + + public static async ValueTask Match( + this Maybe> maybe, + Func Some, + Func None, + TContext context, + CancellationToken cancellationToken = default + ) + { + if (maybe.HasValue) + { + await Some.Invoke( + maybe.GetValueOrThrow().Key, + maybe.GetValueOrThrow().Value, + context, + cancellationToken + ); + } + else + { + await None.Invoke(context, cancellationToken); + } + } } } #endif diff --git a/CSharpFunctionalExtensions/Maybe/Extensions/Match.cs b/CSharpFunctionalExtensions/Maybe/Extensions/Match.cs index 65c8d6c3..7896188f 100644 --- a/CSharpFunctionalExtensions/Maybe/Extensions/Match.cs +++ b/CSharpFunctionalExtensions/Maybe/Extensions/Match.cs @@ -1,40 +1,92 @@ -using System; -using System.Collections.Generic; - -namespace CSharpFunctionalExtensions -{ - public static partial class MaybeExtensions - { - public static TE Match(in this Maybe maybe, Func Some, Func None) - { - return maybe.HasValue - ? Some(maybe.GetValueOrThrow()) - : None(); - } - - public static void Match(in this Maybe maybe, Action Some, Action None) - { - if (maybe.HasValue) - Some(maybe.GetValueOrThrow()); - else - None(); - } - - public static TE Match(in this Maybe> maybe, - Func Some, Func None) - { - return maybe.HasValue - ? Some.Invoke(maybe.GetValueOrThrow().Key, maybe.GetValueOrThrow().Value) - : None.Invoke(); - } - - public static void Match(in this Maybe> maybe, - Action Some, Action None) - { - if (maybe.HasValue) - Some.Invoke(maybe.GetValueOrThrow().Key, maybe.GetValueOrThrow().Value); - else - None.Invoke(); - } - } -} \ No newline at end of file +using System; +using System.Collections.Generic; + +namespace CSharpFunctionalExtensions +{ + public static partial class MaybeExtensions + { + public static TE Match(in this Maybe maybe, Func Some, Func None) + { + return maybe.HasValue ? Some(maybe.GetValueOrThrow()) : None(); + } + + public static TE Match( + in this Maybe maybe, + Func Some, + Func None, + TContext context + ) + { + return maybe.HasValue ? Some(maybe.GetValueOrThrow(), context) : None(context); + } + + public static void Match(in this Maybe maybe, Action Some, Action None) + { + if (maybe.HasValue) + Some(maybe.GetValueOrThrow()); + else + None(); + } + + public static void Match( + in this Maybe maybe, + Action Some, + Action None, + TContext context + ) + { + if (maybe.HasValue) + Some(maybe.GetValueOrThrow(), context); + else + None(context); + } + + public static TE Match( + in this Maybe> maybe, + Func Some, + Func None + ) + { + return maybe.HasValue + ? Some.Invoke(maybe.GetValueOrThrow().Key, maybe.GetValueOrThrow().Value) + : None.Invoke(); + } + + public static TE Match( + in this Maybe> maybe, + Func Some, + Func None, + TContext context + ) + { + return maybe.HasValue + ? Some.Invoke(maybe.GetValueOrThrow().Key, maybe.GetValueOrThrow().Value, context) + : None.Invoke(context); + } + + public static void Match( + in this Maybe> maybe, + Action Some, + Action None + ) + { + if (maybe.HasValue) + Some.Invoke(maybe.GetValueOrThrow().Key, maybe.GetValueOrThrow().Value); + else + None.Invoke(); + } + + public static void Match( + in this Maybe> maybe, + Action Some, + Action None, + TContext context + ) + { + if (maybe.HasValue) + Some.Invoke(maybe.GetValueOrThrow().Key, maybe.GetValueOrThrow().Value, context); + else + None.Invoke(context); + } + } +}