From 5646ec668cac6b6eb9f3ad374f7e3df115f3dc6b Mon Sep 17 00:00:00 2001 From: Thomas Bruderer Date: Thu, 5 Jan 2023 15:33:08 +0100 Subject: [PATCH] Introducing Discard and using it to make a generic Apply function. --- .../Extensions/FuncExtensions/ApplyTest.cs | 36 +++++++++++++++++++ Funcky/Extensions/FuncExtensions/Apply.cs | 22 ++++++++++++ Funcky/Functional/Discard.cs | 10 ++++++ Funcky/PublicAPI.Unshipped.txt | 10 ++++++ 4 files changed, 78 insertions(+) create mode 100644 Funcky.Test/Extensions/FuncExtensions/ApplyTest.cs create mode 100644 Funcky/Extensions/FuncExtensions/Apply.cs create mode 100644 Funcky/Functional/Discard.cs diff --git a/Funcky.Test/Extensions/FuncExtensions/ApplyTest.cs b/Funcky.Test/Extensions/FuncExtensions/ApplyTest.cs new file mode 100644 index 00000000..435df122 --- /dev/null +++ b/Funcky.Test/Extensions/FuncExtensions/ApplyTest.cs @@ -0,0 +1,36 @@ +using static Funcky.Discard; + +namespace Funcky.Test.Extensions.FuncExtensions; + +public class ApplyTest +{ + [Fact] + public void Test() + { + var func = Linear0; + + var f1 = ((Func)Linear0).Apply(_, _, 10); + var f2 = func.Apply(2, _, 7); + var f3 = Funcky.Extensions.FuncExtensions.Apply(Linear0, _, _, 42); + + Assert.Equal(30, f1(10, 2)); + Assert.Equal(72, f2(10)); + } + + [Fact] + public void Test2() + { + } + + private static int Linear0(int a, int b, int c) + => a + (b * c); + + private int Function(int x) + { + return _ switch + { + _ when x == 3 => 0, + _ => 1, + }; + } +} diff --git a/Funcky/Extensions/FuncExtensions/Apply.cs b/Funcky/Extensions/FuncExtensions/Apply.cs new file mode 100644 index 00000000..4014171e --- /dev/null +++ b/Funcky/Extensions/FuncExtensions/Apply.cs @@ -0,0 +1,22 @@ +namespace Funcky.Extensions; + +public static partial class FuncExtensions +{ + public static Func Apply(this Func function, T1 arg1, Unit arg2, Unit arg3) + => (a2, a3) => function(arg1, a2, a3); + + public static Func Apply(this Func function, Unit arg1, T2 arg2, Unit arg3) + => (a1, a3) => function(a1, arg2, a3); + + public static Func Apply(this Func function, T1 arg1, T2 arg2, Unit arg3) + => a3 => function(arg1, arg2, a3); + + public static Func Apply(this Func function, Unit arg1, Unit arg2, T3 arg3) + => (a1, a2) => function(a1, a2, arg3); + + public static Func Apply(this Func function, T1 arg1, Unit arg2, T3 arg3) + => a2 => function(arg1, a2, arg3); + + public static Func Apply(this Func function, Unit arg1, T2 arg2, T3 arg3) + => a1 => function(a1, arg2, arg3); +} diff --git a/Funcky/Functional/Discard.cs b/Funcky/Functional/Discard.cs new file mode 100644 index 00000000..3d5c8a8e --- /dev/null +++ b/Funcky/Functional/Discard.cs @@ -0,0 +1,10 @@ +#pragma warning disable SA1300 +#pragma warning disable SA1649 + +namespace Funcky; + +public class Discard +{ + public static readonly Unit _ = Unit.Value; + public static readonly Unit __ = Unit.Value; +} diff --git a/Funcky/PublicAPI.Unshipped.txt b/Funcky/PublicAPI.Unshipped.txt index 55580012..9c0479b6 100644 --- a/Funcky/PublicAPI.Unshipped.txt +++ b/Funcky/PublicAPI.Unshipped.txt @@ -1,9 +1,17 @@ #nullable enable +Funcky.Discard +Funcky.Discard.Discard() -> void Funcky.RequireClass Funcky.RequireClass.RequireClass() -> void Funcky.RequireStruct Funcky.RequireStruct.RequireStruct() -> void static Funcky.Extensions.EnumerableExtensions.ElementAtOrNone(this System.Collections.Generic.IEnumerable! source, System.Index index) -> Funcky.Monads.Option +static Funcky.Extensions.FuncExtensions.Apply(this System.Func! function, Funcky.Unit arg1, Funcky.Unit arg2, T3 arg3) -> System.Func! +static Funcky.Extensions.FuncExtensions.Apply(this System.Func! function, Funcky.Unit arg1, T2 arg2, Funcky.Unit arg3) -> System.Func! +static Funcky.Extensions.FuncExtensions.Apply(this System.Func! function, Funcky.Unit arg1, T2 arg2, T3 arg3) -> System.Func! +static Funcky.Extensions.FuncExtensions.Apply(this System.Func! function, T1 arg1, Funcky.Unit arg2, Funcky.Unit arg3) -> System.Func! +static Funcky.Extensions.FuncExtensions.Apply(this System.Func! function, T1 arg1, Funcky.Unit arg2, T3 arg3) -> System.Func! +static Funcky.Extensions.FuncExtensions.Apply(this System.Func! function, T1 arg1, T2 arg2, Funcky.Unit arg3) -> System.Func! static Funcky.Extensions.ParseExtensions.ParseBigIntegerOrNone(this string? candidate, System.IFormatProvider? provider) -> Funcky.Monads.Option static Funcky.Extensions.ParseExtensions.ParseBigIntegerOrNone(this System.ReadOnlySpan candidate, System.IFormatProvider? provider) -> Funcky.Monads.Option static Funcky.Extensions.ParseExtensions.ParseByteOrNone(this string? candidate, System.IFormatProvider? provider) -> Funcky.Monads.Option @@ -43,3 +51,5 @@ static Funcky.Extensions.QueryableExtensions.ElementAtOrNone(this Syste static Funcky.Extensions.StreamExtensions.ReadByteOrNone(this System.IO.Stream! stream) -> Funcky.Monads.Option static Funcky.Monads.OptionExtensions.ToNullable(this Funcky.Monads.Option option, Funcky.RequireClass? ω = null) -> TItem? static Funcky.Monads.OptionExtensions.ToNullable(this Funcky.Monads.Option option, Funcky.RequireStruct? ω = null) -> TItem? +static readonly Funcky.Discard._ -> Funcky.Unit +static readonly Funcky.Discard.__ -> Funcky.Unit