-
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.
Merge pull request #818 from polyadic/discard-apply-source-gen
Generate an Apply function for partial application
- Loading branch information
Showing
4 changed files
with
229 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,101 @@ | ||
using System.Collections.Immutable; | ||
using Funcky.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Funcky.SourceGenerator; | ||
|
||
[Generator(LanguageNames.CSharp)] | ||
public sealed class ApplyGenerator : IIncrementalGenerator | ||
{ | ||
private const int MaxParameterCount = 4; | ||
private const string ResultTypeParameter = "TResult"; | ||
|
||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
context.RegisterPostInitializationOutput(static context => context.AddSource("FuncExtensions.Apply.g.cs", GenerateApplyExtensions())); | ||
context.RegisterPostInitializationOutput(static context => context.AddSource("Functional.Apply.g.cs", GenerateApplyFunctions())); | ||
} | ||
|
||
private static string GenerateApplyExtensions() | ||
=> $$""" | ||
#nullable enable | ||
namespace Funcky.Extensions; | ||
public static partial class FuncExtensions | ||
{ | ||
{{string.Join("\n\n ", GenerateApplyMethods(extensionMethod: true))}} | ||
} | ||
"""; | ||
|
||
private static string GenerateApplyFunctions() | ||
=> $$""" | ||
#nullable enable | ||
namespace Funcky; | ||
public static partial class Functional | ||
{ | ||
{{string.Join("\n\n ", GenerateApplyMethods(extensionMethod: false))}} | ||
} | ||
"""; | ||
|
||
private static IEnumerable<string> GenerateApplyMethods(bool extensionMethod) | ||
=> (from parameterCount in Enumerable.Range(2, count: MaxParameterCount) | ||
from unitParameters in Enumerable.Range(0, count: parameterCount).PowerSet() | ||
let unitParameters_ = unitParameters.ToImmutableArray() | ||
where unitParameters_.Length != 0 && unitParameters_.Length != parameterCount | ||
select GenerateApplyMethod(parameterCount, unitParameters_, extensionMethod)); | ||
|
||
private static string GenerateApplyMethod(int parameterCount, ImmutableArray<int> unitParameters, bool extensionMethod) | ||
{ | ||
var returnType = ReturnType(unitParameters); | ||
var typeParameters = string.Join(", ", TypeParameters(parameterCount)); | ||
var @this = extensionMethod ? "this " : string.Empty; | ||
var funcType = FuncType(TypeParameters(parameterCount)); | ||
var parameters = string.Join(", ", Parameters(parameterCount, unitParameters)); | ||
var lambdaParameters = string.Join(", ", LambdaParameters(unitParameters)); | ||
var arguments = string.Join(", ", Arguments(parameterCount, unitParameters)); | ||
return $"public static {returnType} Apply<{typeParameters}>({@this}{funcType} func, {parameters}) => ({lambdaParameters}) => func({arguments});"; | ||
} | ||
|
||
private static IEnumerable<string> TypeParameters(int parameterCount) | ||
=> Enumerable.Range(0, count: parameterCount) | ||
.Select(TypeParameter) | ||
.Concat([ResultTypeParameter]); | ||
|
||
private static string ReturnType(ImmutableArray<int> unitParameters) | ||
=> FuncType(unitParameters | ||
.Select(TypeParameter) | ||
.Concat([ResultTypeParameter])); | ||
|
||
private static string FuncType(IEnumerable<string> typeParameters) => $"Func<{string.Join(", ", typeParameters)}>"; | ||
|
||
private static string TypeParameter(int index) => $"T{index + 1}"; | ||
|
||
private static IEnumerable<string> Parameters(int count, ImmutableArray<int> unitParameters) | ||
=> Enumerable.Range(0, count).Select(index => Parameter(index, unitParameters)); | ||
|
||
private static string Parameter(int index, ImmutableArray<int> unitParameters) | ||
=> $"{ParameterType(index, unitParameters)} {ParameterName(index)}"; | ||
|
||
private static string ParameterName(int index) => $"p{index + 1}"; | ||
|
||
private static string ParameterType(int index, ImmutableArray<int> unitParameters) | ||
=> unitParameters.Contains(index) | ||
? "Unit" | ||
: TypeParameter(index); | ||
|
||
private static IEnumerable<string> LambdaParameters(ImmutableArray<int> unitParameters) | ||
=> unitParameters.Select(LambdaParameterName); | ||
|
||
private static string LambdaParameterName(int index) => $"p{index + 1}_"; | ||
|
||
private static IEnumerable<string> Arguments(int count, ImmutableArray<int> unitParameters) | ||
=> Enumerable.Range(0, count).Select(index => Argument(index, unitParameters)); | ||
|
||
private static string Argument(int index, ImmutableArray<int> unitParameters) | ||
=> unitParameters.Contains(index) | ||
? LambdaParameterName(index) | ||
: ParameterName(index); | ||
} |
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
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,21 @@ | ||
using static Funcky.Discard; | ||
|
||
namespace Funcky.Test.FunctionalClass; | ||
|
||
public class ApplyTest | ||
{ | ||
[Fact] | ||
public void CanApplyParametersInAnyOrder() | ||
{ | ||
var func = Linear0; | ||
var f1 = Fn(Linear0).Apply(__, __, 10); | ||
var f2 = func.Apply(2, __, 7); | ||
var f3 = Apply(Fn(Linear0), 42, __, __); | ||
Assert.Equal(Linear0(10, 2, 10), f1(10, 2)); | ||
Assert.Equal(Linear0(2, 10, 7), f2(10)); | ||
Assert.Equal(Linear0(42, 10, 2), f3(10, 2)); | ||
} | ||
|
||
private static int Linear0(int a, int b, int c) | ||
=> a + (b * c); | ||
} |
Oops, something went wrong.