Skip to content

Commit

Permalink
Merge pull request #816 from sys27/use-new-array-init-syntax
Browse files Browse the repository at this point in the history
Use C# 12 collection expressions syntax.
  • Loading branch information
sys27 authored Nov 16, 2024
2 parents 9a25d3b + 1fe49bc commit d0a18c0
Show file tree
Hide file tree
Showing 64 changed files with 1,176 additions and 1,209 deletions.
2 changes: 1 addition & 1 deletion xFunc.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Program
public static void Main(string[] args)
{
if (args is null || args.Length == 0)
args = new[] { "--filter", "*" };
args = ["--filter", "*"];

BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
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 @@ -17,7 +17,7 @@ public class CallExpression : IExpression, IEquatable<CallExpression>
/// <param name="function">The expression that returns function.</param>
/// <param name="argument">The parameter of the function.</param>
public CallExpression(IExpression function, IExpression argument)
: this(function, ImmutableArray.Create(argument))
: this(function, [argument])
{
}

Expand All @@ -28,7 +28,7 @@ public CallExpression(IExpression function, IExpression argument)
/// <param name="argument1">The first parameter of the function.</param>
/// <param name="argument2">The second parameter of the function.</param>
public CallExpression(IExpression function, IExpression argument1, IExpression argument2)
: this(function, ImmutableArray.Create(argument1, argument2))
: this(function, [argument1, argument2])
{
}

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Curry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public object Execute(ExpressionParameters? parameters)

var result = Parameters.Aggregate(
lambda,
(current, parameter) => (Lambda)current.Call(ImmutableArray.Create(parameter), parameters));
(current, parameter) => (Lambda)current.Call([parameter], parameters));

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions xFunc.Maths/Expressions/Derivative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Derivative(
IDifferentiator differentiator,
ISimplifier simplifier,
IExpression expression)
: this(differentiator, simplifier, ImmutableArray.Create(expression))
: this(differentiator, simplifier, [expression])
{
}

Expand All @@ -36,7 +36,7 @@ public Derivative(
ISimplifier simplifier,
IExpression expression,
Variable variable)
: this(differentiator, simplifier, ImmutableArray.Create(expression, variable))
: this(differentiator, simplifier, [expression, variable])
{
}

Expand All @@ -54,7 +54,7 @@ public Derivative(
IExpression expression,
Variable variable,
Number point)
: this(differentiator, simplifier, ImmutableArray.Create(expression, variable, point))
: this(differentiator, simplifier, [expression, variable, point])
{
}

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 @@ -15,7 +15,7 @@ public abstract class DifferentParametersExpression : IExpression
/// </summary>
/// <param name="arguments">The arguments.</param>
protected DifferentParametersExpression(IEnumerable<IExpression> arguments)
: this(arguments.ToImmutableArray())
: this([..arguments])
{
}

Expand Down
8 changes: 4 additions & 4 deletions xFunc.Maths/Expressions/Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace xFunc.Maths.Expressions;
/// </summary>
/// <param name="body">The body of the function.</param>
public Lambda(IExpression body)
: this(ImmutableArray.Create<string>(), body, null)
: this([], body, null)
{
}

Expand All @@ -26,7 +26,7 @@ public Lambda(IExpression body)
/// <param name="parameters">The list of parameters of the function.</param>
/// <param name="body">The body of the function.</param>
public Lambda(IEnumerable<string> parameters, IExpression body)
: this(parameters.ToImmutableArray(), body, null)
: this([..parameters], body, null)
{
}

Expand Down Expand Up @@ -133,9 +133,9 @@ public Lambda Curry()
var body = Body;

for (var i = Parameters.Length - 1; i > 0; i--)
body = new LambdaExpression(new Lambda(ImmutableArray.Create(Parameters[i]), body));
body = new LambdaExpression(new Lambda([Parameters[i]], body));

var lambda = new Lambda(ImmutableArray.Create(Parameters[0]), body);
var lambda = new Lambda([Parameters[0]], body);

return lambda;
}
Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Expressions/Matrices/MatrixValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private MatrixValue(NumberValue[][] values)
public static MatrixValue Create(NumberValue value)
=> new MatrixValue(new NumberValue[][]
{
new NumberValue[] { value },
[value],
});

/// <summary>
Expand All @@ -42,7 +42,7 @@ public static MatrixValue Create(NumberValue value)
/// <param name="value">The item of a new matrix.</param>
/// <returns>The matrix.</returns>
public static MatrixValue Create(VectorValue value)
=> new MatrixValue(new[] { value });
=> new MatrixValue([value]);

/// <summary>
/// Creates a new instance of <see cref="MatrixValue"/>.
Expand Down
9 changes: 4 additions & 5 deletions xFunc.Maths/Expressions/Matrices/VectorValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private VectorValue(NumberValue[] array)
/// <param name="value">The item of a new vector.</param>
/// <returns>The vector.</returns>
public static VectorValue Create(NumberValue value)
=> new VectorValue(new[] { value });
=> new VectorValue([value]);

/// <summary>
/// Creates a new instance of <see cref="VectorValue"/>.
Expand Down Expand Up @@ -339,11 +339,10 @@ public static VectorValue Cross(VectorValue left, VectorValue right)
if (left.Size != 3 || right.Size != 3)
throw new ArgumentException(Resource.VectorCrossException);

return new VectorValue(new[]
{
return new VectorValue([
left[1] * right[2] - left[2] * right[1],
left[2] * right[0] - left[0] * right[2],
left[0] * right[1] - left[1] * right[0],
});
left[0] * right[1] - left[1] * right[0]
]);
}
}
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Programming/For.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class For : DifferentParametersExpression
/// <param name="cond">The condition section.</param>
/// <param name="iter">The iterator section.</param>
public For(IExpression body, IExpression init, IExpression cond, IExpression iter)
: this(ImmutableArray.Create(body, init, cond, iter))
: this([body, init, cond, iter])
{
}

Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Expressions/Programming/If.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class If : DifferentParametersExpression
/// <param name="condition">The condition.</param>
/// <param name="then">The "then" statement.</param>
public If(IExpression condition, IExpression then)
: this(ImmutableArray.Create(condition, then))
: this([condition, then])
{
}

Expand All @@ -28,7 +28,7 @@ public If(IExpression condition, IExpression then)
/// <param name="then">The "then" statement.</param>
/// <param name="else">The "else" statement.</param>
public If(IExpression condition, IExpression then, IExpression @else)
: this(ImmutableArray.Create(condition, then, @else))
: this([condition, then, @else])
{
}

Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Expressions/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Round : DifferentParametersExpression
/// </summary>
/// <param name="argument">The expression that represents a double-precision floating-point number to be rounded.</param>
public Round(IExpression argument)
: this(ImmutableArray.Create(argument))
: this([argument])
{
}

Expand All @@ -33,7 +33,7 @@ public Round(IExpression argument)
/// <param name="argument">The expression that represents a double-precision floating-point number to be rounded.</param>
/// <param name="digits">The expression that represents the number of fractional digits in the return value.</param>
public Round(IExpression argument, IExpression digits)
: this(ImmutableArray.Create(argument, digits))
: this([argument, digits])
{
}

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Units/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Convert : IExpression
/// <param name="value">The value to convert.</param>
/// <param name="unit">The target unit.</param>
public Convert(IConverter converter, IExpression value, IExpression unit)
: this(converter, ImmutableArray.Create(value, unit))
: this(converter, [value, unit])
{
}

Expand Down
8 changes: 4 additions & 4 deletions xFunc.Maths/Expressions/Units/Converters/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public class Converter : IConverter
/// Initializes a new instance of the <see cref="Converter"/> class.
/// </summary>
public Converter()
=> converters = new IConverter<object>[]
{
=> converters =
[
new AngleConverter(),
new PowerConverter(),
new TemperatureConverter(),
new MassConverter(),
new AreaConverter(),
new LengthConverter(),
new TimeConverter(),
new VolumeConverter(),
};
new VolumeConverter()
];

/// <inheritdoc />
public object Convert(object value, string unit)
Expand Down
7 changes: 3 additions & 4 deletions xFunc.Tests/Analyzers/DifferentiatorTests/NullArgumentTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Reflection;
using NUnit.Framework.Internal;

Expand All @@ -17,11 +16,11 @@ private void TestNullExp(Type type, object exp = null)
{
var context = typeof(DifferentiatorContext);
var method = typeof(Differentiator)
.GetMethod(nameof(Differentiator.Analyze), new[] { type, context });
.GetMethod(nameof(Differentiator.Analyze), [type, context]);
if (method is null)
throw new InvalidOperationException("The 'Analyze' method not found.");

method.Invoke(differentiator, new[] { exp, null });
method.Invoke(differentiator, [exp, null]);
}
catch (TargetInvocationException e)
{
Expand Down Expand Up @@ -180,7 +179,7 @@ public void CallExpressionContextNullArgument()
{
var exp = new CallExpression(
Variable.X.ToLambdaExpression(Variable.X.Name),
new IExpression[] { Variable.X }.ToImmutableArray());
[..new IExpression[] { Variable.X }]);

Assert.Throws<NotSupportedException>(() => exp.Analyze(differentiator, null));
}
Expand Down
7 changes: 3 additions & 4 deletions xFunc.Tests/Analyzers/Formatters/CommonFormatterTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Numerics;
using Matrices = xFunc.Maths.Expressions.Matrices;

Expand Down Expand Up @@ -507,7 +506,7 @@ public void CallExpressionToStringArgTest()
{
var exp = new CallExpression(
new Variable("f"),
new IExpression[] { new Number(5), Number.Two }.ToImmutableArray());
[..new IExpression[] { new Number(5), Number.Two }]);

Assert.That(exp.ToString(), Is.EqualTo("f(5, 2)"));
}
Expand All @@ -517,7 +516,7 @@ public void InlineCallExpressionToStringArgTest()
{
var exp = new CallExpression(
Variable.X.ToLambdaExpression(Variable.X.Name),
new IExpression[] { new Number(5) }.ToImmutableArray());
[..new IExpression[] { new Number(5) }]);

Assert.That(exp.ToString(), Is.EqualTo("((x) => x)(5)"));
}
Expand All @@ -535,7 +534,7 @@ public void CurryWithParametersTest()
{
var exp = new Curry(
new Lambda(Number.One).AsExpression(),
new IExpression[] { Number.One, Number.Two }.ToImmutableArray());
[..new IExpression[] { Number.One, Number.Two }]);

Assert.That(exp.ToString(), Is.EqualTo("curry(() => 1, 1, 2)"));
}
Expand Down
4 changes: 2 additions & 2 deletions xFunc.Tests/Analyzers/SimplifierTests/BaseSimplifierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ protected void TestNullExp(Type type)
try
{
var method = typeof(Simplifier)
.GetMethod(nameof(Simplifier.Analyze), new[] { type });
method.Invoke(simplifier, new object[] { null });
.GetMethod(nameof(Simplifier.Analyze), [type]);
method.Invoke(simplifier, [null]);
}
catch (TargetInvocationException e)
{
Expand Down
8 changes: 3 additions & 5 deletions xFunc.Tests/Analyzers/SimplifierTests/SimplifierTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;

namespace xFunc.Tests.Analyzers.SimplifierTests;

public class SimplifierTest : BaseSimplifierTest
Expand Down Expand Up @@ -235,10 +233,10 @@ public void CallExpressionSimplifiedTest()
{
var exp = new CallExpression(
new Lambda(new[] { "x" }, new Add(Variable.X, Number.Zero)).AsExpression(),
new IExpression[] { new Add(Number.One, Number.One) }.ToImmutableArray());
[..new IExpression[] { new Add(Number.One, Number.One) }]);
var expected = new CallExpression(
new Lambda(new[] { "x" }, Variable.X).AsExpression(),
new IExpression[] { Number.Two }.ToImmutableArray());
[..new IExpression[] { Number.Two }]);

SimplifyTest(exp, expected);
}
Expand All @@ -248,7 +246,7 @@ public void CallExpressionNotSimplifiedTest()
{
var exp = new CallExpression(
new Lambda(new[] { "x" }, Variable.X).AsExpression(),
new IExpression[] { Variable.X }.ToImmutableArray());
[..new IExpression[] { Variable.X }]);

SimplifyTest(exp, exp);
}
Expand Down
Loading

0 comments on commit d0a18c0

Please sign in to comment.