-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add expression calculator with state sample. * Rev to v1.8.0 * Add v1.8.0 features to readme.
- Loading branch information
Showing
5 changed files
with
109 additions
and
29 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
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
17 changes: 17 additions & 0 deletions
17
samples/ExpressionCalculatorWithState/ExpressionCalculatorWithState.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Dunet" Version="1.7.2-pre1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
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,38 @@ | ||
using Dunet; | ||
using static Expression; | ||
|
||
var environment = new Dictionary<string, int>() | ||
{ | ||
["a"] = 1, | ||
["b"] = 2, | ||
["c"] = 3, | ||
}; | ||
|
||
var expression = new Add(new Variable("a"), new Multiply(new Number(2), new Variable("b"))); | ||
var result = Evaluate(environment, expression); | ||
|
||
Console.WriteLine(result); // "5" | ||
|
||
static int Evaluate(Dictionary<string, int> env, Expression exp) => | ||
exp.Match( | ||
// 1. Pass your state "container" as the first parameter. | ||
state: env, | ||
// 2. Use static lambdas for each variant's match method. | ||
static (_, number) => number.Value, | ||
// 3. Reference the state as the first argument of each lambda. | ||
static (state, add) => Evaluate(state, add.Left) + Evaluate(state, add.Right), | ||
static (state, mul) => Evaluate(state, mul.Left) * Evaluate(state, mul.Right), | ||
static (state, var) => state[var.Value] | ||
); | ||
|
||
[Union] | ||
public partial record Expression | ||
{ | ||
public partial record Number(int Value); | ||
|
||
public partial record Add(Expression Left, Expression Right); | ||
|
||
public partial record Multiply(Expression Left, Expression Right); | ||
|
||
public partial record Variable(string Value); | ||
} |
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