Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Create a generic Apply function which applies any parameters and returns a function requring the discarded parameters. #702

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Funcky.Test/FunctionalClass/ApplyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using static Funcky.Discard;

namespace Funcky.Test.Extensions.FuncExtensions;

public class ApplyTest
{
[Fact]
public void CanApplyParametersInAnyOrder()
{
var func = Linear0;

var f1 = ((Func<int, int, int, int>)Linear0).Apply(__, __, 10);
var f2 = func.Apply(2, __, 7);
var f3 = Funcky.Extensions.FuncExtensions.Apply<int, int, int, int>(Linear0, __, __, 42);

Assert.Equal(30, f1(10, 2));
Assert.Equal(72, f2(10));
Assert.Equal(30, f3(10, 2));
}

[Fact]
public void Test2()
{
}

private static int Linear0(int a, int b, int c)
=> a + (b * c);
}
30 changes: 30 additions & 0 deletions Funcky/Extensions/FuncExtensions/Apply.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Funcky.Extensions;

public static partial class FuncExtensions
{
// All overloads for Fuctions with 2 paramters.
public static Func<T1, TResult> Apply<T1, T2, TResult>(this Func<T1, T2, TResult> function, Unit arg1, T2 arg2)
=> (a1) => function(a1, arg2);

public static Func<T2, TResult> Apply<T1, T2, TResult>(this Func<T1, T2, TResult> function, T1 arg1, Unit arg2)
=> (a2) => function(arg1, a2);

// All overloads for Fuctions with 3 paramters.
public static Func<T2, T3, TResult> Apply<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> function, T1 arg1, Unit arg2, Unit arg3)
=> (a2, a3) => function(arg1, a2, a3);

public static Func<T1, T3, TResult> Apply<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> function, Unit arg1, T2 arg2, Unit arg3)
=> (a1, a3) => function(a1, arg2, a3);

public static Func<T1, T2, TResult> Apply<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> function, Unit arg1, Unit arg2, T3 arg3)
=> (a1, a2) => function(a1, a2, arg3);

public static Func<T3, TResult> Apply<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> function, T1 arg1, T2 arg2, Unit arg3)
=> a3 => function(arg1, arg2, a3);

public static Func<T2, TResult> Apply<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> function, T1 arg1, Unit arg2, T3 arg3)
=> a2 => function(arg1, a2, arg3);

public static Func<T1, TResult> Apply<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> function, Unit arg1, T2 arg2, T3 arg3)
=> a1 => function(a1, arg2, arg3);
}
8 changes: 8 additions & 0 deletions Funcky/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#nullable enable
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, TResult>(this System.Func<T1, T2, T3, TResult>! function, Funcky.Unit arg1, Funcky.Unit arg2, T3 arg3) -> System.Func<T1, T2, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, TResult>(this System.Func<T1, T2, T3, TResult>! function, Funcky.Unit arg1, T2 arg2, Funcky.Unit arg3) -> System.Func<T1, T3, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, TResult>(this System.Func<T1, T2, T3, TResult>! function, Funcky.Unit arg1, T2 arg2, T3 arg3) -> System.Func<T1, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, TResult>(this System.Func<T1, T2, T3, TResult>! function, T1 arg1, Funcky.Unit arg2, Funcky.Unit arg3) -> System.Func<T2, T3, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, TResult>(this System.Func<T1, T2, T3, TResult>! function, T1 arg1, Funcky.Unit arg2, T3 arg3) -> System.Func<T2, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, T3, TResult>(this System.Func<T1, T2, T3, TResult>! function, T1 arg1, T2 arg2, Funcky.Unit arg3) -> System.Func<T3, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, TResult>(this System.Func<T1, T2, TResult>! function, Funcky.Unit arg1, T2 arg2) -> System.Func<T1, TResult>!
static Funcky.Extensions.FuncExtensions.Apply<T1, T2, TResult>(this System.Func<T1, T2, TResult>! function, T1 arg1, Funcky.Unit arg2) -> System.Func<T2, TResult>!