-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Discard and using it to make a generic Apply function.
- Loading branch information
1 parent
766fc4d
commit 5646ec6
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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)); | ||
} | ||
|
||
[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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Funcky.Extensions; | ||
|
||
public static partial class FuncExtensions | ||
{ | ||
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<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<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<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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters