Skip to content

Commit

Permalink
Introducing Discard and using it to make a generic Apply function.
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeApophis committed Jan 5, 2023
1 parent 766fc4d commit 5646ec6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Funcky.Test/Extensions/FuncExtensions/ApplyTest.cs
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,
};
}
}
22 changes: 22 additions & 0 deletions Funcky/Extensions/FuncExtensions/Apply.cs
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);
}
10 changes: 10 additions & 0 deletions Funcky/Functional/Discard.cs
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;
}
10 changes: 10 additions & 0 deletions Funcky/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#nullable enable
Funcky.Discard
Funcky.Discard.Discard() -> void
Funcky.RequireClass<T>
Funcky.RequireClass<T>.RequireClass() -> void
Funcky.RequireStruct<T>
Funcky.RequireStruct<T>.RequireStruct() -> void
static Funcky.Extensions.EnumerableExtensions.ElementAtOrNone<TSource>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Index index) -> Funcky.Monads.Option<TSource>
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.ParseExtensions.ParseBigIntegerOrNone(this string? candidate, System.IFormatProvider? provider) -> Funcky.Monads.Option<System.Numerics.BigInteger>
static Funcky.Extensions.ParseExtensions.ParseBigIntegerOrNone(this System.ReadOnlySpan<char> candidate, System.IFormatProvider? provider) -> Funcky.Monads.Option<System.Numerics.BigInteger>
static Funcky.Extensions.ParseExtensions.ParseByteOrNone(this string? candidate, System.IFormatProvider? provider) -> Funcky.Monads.Option<byte>
Expand Down Expand Up @@ -43,3 +51,5 @@ static Funcky.Extensions.QueryableExtensions.ElementAtOrNone<TSource>(this Syste
static Funcky.Extensions.StreamExtensions.ReadByteOrNone(this System.IO.Stream! stream) -> Funcky.Monads.Option<byte>
static Funcky.Monads.OptionExtensions.ToNullable<TItem>(this Funcky.Monads.Option<TItem!> option, Funcky.RequireClass<TItem!>? ω = null) -> TItem?
static Funcky.Monads.OptionExtensions.ToNullable<TItem>(this Funcky.Monads.Option<TItem> option, Funcky.RequireStruct<TItem>? ω = null) -> TItem?
static readonly Funcky.Discard._ -> Funcky.Unit
static readonly Funcky.Discard.__ -> Funcky.Unit

0 comments on commit 5646ec6

Please sign in to comment.