Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bykiev committed Aug 6, 2024
2 parents bcbb05c + c7c4340 commit 9fe410e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/NCalc.Async/AsyncExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,24 @@ public AsyncExpression(LogicalExpression logicalExpression, ExpressionOptions op
{
var parameterEnumerators = ParametersHelper.GetEnumerators(Parameters, out var size);

var results = new List<object?>();

for (int i = 0; i < size; i++)
if (size != null)
{
foreach (var kvp in parameterEnumerators)
var results = new List<object?>();

for (int i = 0; i < size; i++)
{
kvp.Value.MoveNext();
Parameters[kvp.Key] = kvp.Value.Current;
foreach (var kvp in parameterEnumerators)
{
kvp.Value.MoveNext();
Parameters[kvp.Key] = kvp.Value.Current;
}

results.Add(await EvaluationService.EvaluateAsync(LogicalExpression!, Context));
}

results.Add(await EvaluationService.EvaluateAsync(LogicalExpression!, Context));
return results;
}

return results;
return await EvaluationService.EvaluateAsync(LogicalExpression!, Context);
}
}
23 changes: 14 additions & 9 deletions src/NCalc.Sync/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,28 @@ public Expression(LogicalExpression logicalExpression, ExpressionOptions options
return EvaluationService.Evaluate(LogicalExpression!, Context);
}

private List<object?> IterateParameters()
private object? IterateParameters()
{
var parameterEnumerators = ParametersHelper.GetEnumerators(Parameters, out var size);

var results = new List<object?>();

for (int i = 0; i < size; i++)
if (size != null)
{
foreach (var kvp in parameterEnumerators)
var results = new List<object?>();

for (int i = 0; i < size; i++)
{
kvp.Value.MoveNext();
Parameters[kvp.Key] = kvp.Value.Current;
foreach (var kvp in parameterEnumerators)
{
kvp.Value.MoveNext();
Parameters[kvp.Key] = kvp.Value.Current;
}

results.Add(EvaluationService.Evaluate(LogicalExpression!, Context));
}

results.Add(EvaluationService.Evaluate(LogicalExpression!, Context));
return results;
}

return results;
return EvaluationService.Evaluate(LogicalExpression!, Context);
}
}
2 changes: 1 addition & 1 deletion test/NCalc.Benchmarks/NCalc.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions test/NCalc.Tests/EvaluationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ public void ShouldEvaluateArrayParameters()
Assert.Equal(16, result[4]);
}

[Fact]
public void ShouldEvaluateArrayParametersWithFunctions()
{
var e = new Expression("Round(x, 2)", ExpressionOptions.IterateParameters)
{
Parameters =
{
["x"] = new [] { 0.51, 1.671, 2.237, 3.568, 4.11 }
}
};

var result = (IList<object>)e.Evaluate();

Assert.NotNull(result);
Assert.Equal(0.51, result[0]);
Assert.Equal(1.67, result[1]);
Assert.Equal(2.24, result[2]);
Assert.Equal(3.57, result[3]);
Assert.Equal(4.11, result[4]);
}

[Fact]
public void ShouldEvaluateInOperatorWithList()
{
Expand Down

0 comments on commit 9fe410e

Please sign in to comment.