Class Curry
- Namespace
- xFunc.Maths.Expressions
- Assembly
- xFunc.Maths.dll
Represents the "curry" function which allows you to convert any lambda to the list of nested lambdas which you can partially apply.
public class Curry : IExpression
- Inheritance
-
Curry
- Implements
- Inherited Members
- Extension Methods
Examples
Convert the lambda to the list of nested lambdas:
f := (a, b, c) => a + b + c
p := curry(f) // `p` will contain (a) => (b) => (c) => a + b + c
Partially apply the lambda:
f := (a, b, c) => a + b + c
p := curry(f) // `p` will contain (a) => (b) => (c) => a + b + c
add1 := p(1) // partial application, you provided a value for the first parameter only
// so `p(1)` return another lambda which accepts the second parameter.
Partially apply the lambda by using the `curry` function:
f := (a, b, c) => a + b + c
add1 := curry(f, 1) // partial application, the result is equal to the previous example
// but it automatically convert the lambda to the list of nested lambdas
// and tries to apply all provided parameters
Remarks
This function takes an indefinite amount of arguments where:
- the first one is required and should always return a lambda.
- all other arguments are optional and needed only when you want to apply some parameters immediately.
If the provided lambda takes 0 or 1 argument this function does nothing, it returns the same lambda without any modification.
If you provided enough parameters to just call a lambda, then this function works in the same way as CallExpression. It doesn't convert the provided lambda into the list of nested lambdas but instead just calls it directly.
Constructors
Curry(IExpression)
Initializes a new instance of the Curry class.
public Curry(IExpression function)
Parameters
function
IExpressionThe expression that returns a lambda.
Exceptions
- ArgumentNullException
function
is null.
Curry(IExpression, ImmutableArray<IExpression>)
Initializes a new instance of the Curry class.
public Curry(IExpression function, ImmutableArray<IExpression> parameters)
Parameters
function
IExpressionThe expression that returns a lambda.
parameters
ImmutableArray<IExpression>The list of parameters.
Exceptions
- ArgumentNullException
function
is null.
Properties
Function
Gets the expression that returns a lambda to curry.
public IExpression Function { get; }
Property Value
Parameters
Gets the list of parameters.
public ImmutableArray<IExpression> Parameters { get; }
Property Value
Methods
Analyze<TResult>(IAnalyzer<TResult>)
Analyzes the current expression.
public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer)
Parameters
analyzer
IAnalyzer<TResult>The analyzer.
Returns
- TResult
The analysis result.
Type Parameters
TResult
The type of the result.
Analyze<TResult, TContext>(IAnalyzer<TResult, TContext>, TContext)
Analyzes the current expression.
public TResult Analyze<TResult, TContext>(IAnalyzer<TResult, TContext> analyzer, TContext context)
Parameters
analyzer
IAnalyzer<TResult, TContext>The analyzer.
context
TContextThe context.
Returns
- TResult
The analysis result.
Type Parameters
TResult
The type of the result.
TContext
The type of additional parameter for analyzer.
Clone(IExpression?, ImmutableArray<IExpression>?)
Clones this instance of the IExpression.
public IExpression Clone(IExpression? function = null, ImmutableArray<IExpression>? parameters = null)
Parameters
function
IExpressionThe expression that returns function.
parameters
ImmutableArray<IExpression>?The list of parameters.
Returns
- IExpression
Returns the new instance of IExpression that is a clone of this instance.
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current object.
Returns
Execute()
Executes this expression. Don't use this method if your expression has variables or user-functions.
public object Execute()
Returns
- object
A result of the execution.
Execute(ExpressionParameters?)
Executes this expression.
public object Execute(ExpressionParameters? parameters)
Parameters
parameters
ExpressionParametersAn object that contains all parameters and functions for expressions.
Returns
- object
A result of the execution.
- See Also
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
ToString(IFormatter)
Returns a string that represents this instance.
public string ToString(IFormatter formatter)
Parameters
formatter
IFormatterThe formatter.