Skip to content

Commit

Permalink
#653 - Add support of function currying.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Aug 1, 2023
1 parent 277f41f commit 392a46b
Show file tree
Hide file tree
Showing 100 changed files with 180 additions and 325 deletions.
6 changes: 3 additions & 3 deletions xFunc.Maths/Analyzers/DifferentiatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DifferentiatorContext
/// Initializes a new instance of the <see cref="DifferentiatorContext"/> class.
/// </summary>
/// <param name="parameters">The parameters.</param>
public DifferentiatorContext(IExpressionParameters? parameters)
public DifferentiatorContext(ExpressionParameters? parameters)
: this(parameters, Variable.X)
{
}
Expand All @@ -24,7 +24,7 @@ public DifferentiatorContext(IExpressionParameters? parameters)
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <param name="variable">The variable.</param>
public DifferentiatorContext(IExpressionParameters? parameters, Variable variable)
public DifferentiatorContext(ExpressionParameters? parameters, Variable variable)
{
Parameters = parameters;
Variable = variable;
Expand All @@ -43,7 +43,7 @@ public static DifferentiatorContext Default()
/// <value>
/// The parameters.
/// </value>
public IExpressionParameters? Parameters { get; }
public ExpressionParameters? Parameters { get; }

/// <summary>
/// Gets or sets the variable.
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Abs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal Abs(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal Add(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var leftResult = Left.Execute(parameters);
var rightResult = Right.Execute(parameters);
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Assign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override bool Equals(object? obj)
public object Execute() => throw new NotSupportedException();

/// <inheritdoc />
public object Execute(IExpressionParameters? parameters)
public object Execute(ExpressionParameters? parameters)
{
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/BinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override bool Equals(object? obj)
public object Execute() => Execute(null);

/// <inheritdoc />
public abstract object Execute(IExpressionParameters? parameters);
public abstract object Execute(ExpressionParameters? parameters);

/// <inheritdoc />
public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer)
Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Expressions/CallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public object Execute()
=> throw new NotSupportedException();

/// <inheritdoc />
public object Execute(IExpressionParameters? parameters)
public object Execute(ExpressionParameters? parameters)
{
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));

if (Function.Execute(parameters) is not Lambda function)
throw new ResultIsNotSupportedException(this, Function);

var nestedScope = ExpressionParameters.CreateScoped(parameters);
var nestedScope = ExpressionParameters.CreateScoped(function.CapturedScope ?? parameters);
var zip = function.Parameters.Zip(Parameters, (parameter, expression) => (parameter, expression));
foreach (var (parameter, expression) in zip)
nestedScope[parameter] = new ParameterValue(expression.Execute(nestedScope));
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Ceil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal Ceil(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/ComplexNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override bool Equals(object? obj)
public object Execute() => Value;

/// <inheritdoc />
public object Execute(IExpressionParameters? parameters) => Value;
public object Execute(ExpressionParameters? parameters) => Value;

/// <inheritdoc />
public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer)
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/Conjugate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal Conjugate(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/Im.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal Im(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/Phase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal Phase(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/Re.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal Re(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/Reciprocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal Reciprocal(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ComplexNumbers/ToComplex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal ToComplex(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Del.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal Del(
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);
if (result is not Lambda lambda)
Expand Down
6 changes: 3 additions & 3 deletions xFunc.Maths/Expressions/DelegateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace xFunc.Maths.Expressions;
/// </summary>
public class DelegateExpression : IExpression
{
private readonly Func<IExpressionParameters?, object> func;
private readonly Func<ExpressionParameters?, object> func;

/// <summary>
/// Initializes a new instance of the <see cref="DelegateExpression"/> class.
/// </summary>
/// <param name="func">The delegate of function.</param>
public DelegateExpression(Func<IExpressionParameters?, object> func)
public DelegateExpression(Func<ExpressionParameters?, object> func)
=> this.func = func;

/// <inheritdoc />
Expand All @@ -39,7 +39,7 @@ public override bool Equals(object? obj)
public object Execute() => func(null);

/// <inheritdoc />
public object Execute(IExpressionParameters? parameters)
public object Execute(ExpressionParameters? parameters)
=> func(parameters);

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Derivative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal Derivative(
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Expression.Execute(parameters);
if (result is not Lambda lambda)
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/DifferentParametersExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override bool Equals(object? obj)
public virtual object Execute() => Execute(null);

/// <inheritdoc />
public abstract object Execute(IExpressionParameters? parameters);
public abstract object Execute(ExpressionParameters? parameters);

/// <inheritdoc />
public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer)
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Div.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal Div(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var leftResult = Left.Execute(parameters);
var rightResult = Right.Execute(parameters);
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Exp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal Exp(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Fact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal Fact(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Floor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal Floor(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Frac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal Frac(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/GCD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public GCD(IExpression first, IExpression second)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var gcd = new NumberValue(0.0);
foreach (var argument in Arguments)
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Hyperbolic/HyperbolicExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal HyperbolicExpression(ImmutableArray<IExpression> arguments)
protected abstract Complex ExecuteComplex(Complex complex);

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal InverseHyperbolicExpression(ImmutableArray<IExpression> arguments)
protected abstract Complex ExecuteComplex(Complex complex);

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/IExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IExpression
/// <param name="parameters">An object that contains all parameters and functions for expressions.</param>
/// <returns>A result of the execution.</returns>
/// <seealso cref="ExpressionParameters"/>
object Execute(IExpressionParameters? parameters);
object Execute(ExpressionParameters? parameters);

/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/LCM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public LCM(IExpression first, IExpression second)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var lcm = new NumberValue(1.0);
foreach (var argument in Arguments)
Expand Down
17 changes: 10 additions & 7 deletions xFunc.Maths/Expressions/Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace xFunc.Maths.Expressions;
/// </summary>
public readonly struct Lambda : IEquatable<Lambda>
{
private readonly IExpressionParameters? capturedParameters;

/// <summary>
/// Initializes a new instance of the <see cref="Lambda"/> struct.
/// </summary>
Expand Down Expand Up @@ -42,11 +40,11 @@ public Lambda(ImmutableArray<string> parameters, IExpression body)
{
}

private Lambda(ImmutableArray<string> parameters, IExpression body, IExpressionParameters? capturedParameters)
private Lambda(ImmutableArray<string> parameters, IExpression body, ExpressionParameters? capturedScope)
{
Parameters = parameters;
Body = body;
this.capturedParameters = capturedParameters;
CapturedScope = capturedScope;
}

/// <summary>
Expand Down Expand Up @@ -90,15 +88,15 @@ public override string ToString()
/// </summary>
/// <param name="parameters">An object that contains all parameters and functions for expressions.</param>
/// <returns>A result of the execution.</returns>
public object Call(IExpressionParameters parameters)
=> Body.Execute(ExpressionParameters.CreateCombined(parameters, capturedParameters));
public object Call(ExpressionParameters parameters)
=> Body.Execute(parameters);

/// <summary>
/// Returns a new lambda instance with captured parameters.
/// </summary>
/// <param name="parameters">An object that contains all parameters and functions for expressions.</param>
/// <returns>The lambda with captured parameters.</returns>
public Lambda Capture(IExpressionParameters? parameters)
public Lambda Capture(ExpressionParameters? parameters)
=> new Lambda(Parameters, Body, parameters);

/// <summary>
Expand All @@ -117,4 +115,9 @@ public LambdaExpression AsExpression()
/// Gets an expression of the function body.
/// </summary>
public IExpression Body { get; }

/// <summary>
/// Gets the captured scope.
/// </summary>
internal ExpressionParameters? CapturedScope { get; }
}
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/LambdaExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public object Execute()
=> Execute(null);

/// <inheritdoc />
public object Execute(IExpressionParameters? parameters)
public object Execute(ExpressionParameters? parameters)
=> Lambda.Capture(parameters);

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Lb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal Lb(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Lg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal Lg(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Ln.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal Ln(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var result = Argument.Execute(parameters);

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Log(ImmutableArray<IExpression> arguments)
}

/// <inheritdoc />
public override object Execute(IExpressionParameters? parameters)
public override object Execute(ExpressionParameters? parameters)
{
var leftResult = Left.Execute(parameters);
var rightResult = Right.Execute(parameters);
Expand Down
Loading

0 comments on commit 392a46b

Please sign in to comment.