https://github.com/igor-tkachev/linq.expressions.deconstruct
Pattern matching support for System.Linq.Expressions.
https://github.com/igor-tkachev/linq.expressions.deconstruct
Last synced: over 1 year ago
JSON representation
Pattern matching support for System.Linq.Expressions.
- Host: GitHub
- URL: https://github.com/igor-tkachev/linq.expressions.deconstruct
- Owner: igor-tkachev
- License: mit
- Created: 2019-08-01T01:07:48.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-04-15T19:06:25.000Z (about 2 years ago)
- Last Synced: 2024-10-12T02:48:24.979Z (over 1 year ago)
- Language: C#
- Homepage:
- Size: 143 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.TXT
Awesome Lists containing this project
README
# Linq.Expressions.Deconstruct
[](https://ci.appveyor.com/project/igor-tkachev/linq-expressions-deconstruct/branch/master) [](https://www.nuget.org/packages/Linq.Expressions.Deconstruct/)
```c#
[Test]
public void MatchTest()
{
Expression> f = i => i * 2;
switch (f.ToExpr())
{
case Lambda(Multiply(Parameter("i") p1, Constant(2)), [("i") p2])
when p1 == p2 :
Console.WriteLine("Pattern Matched!");
break;
default:
Console.WriteLine(f);
Assert.Fail();
break;
}
}
```
```c#
[Test]
public void ConstantFoldingTest()
{
Expression> f = i => i * 0 + 0 + i + 10 * (i * 0 + 2);
var f1 = f.TransformEx(ex => ex switch
{
Multiply(Constant(0) e, _) => e, // 0 * e => 0
Multiply(_, Constant(0) e) => e, // e * 0 => 0
Multiply(Constant(1), var e) => e, // 1 * e => e
Multiply(var e, Constant(1)) => e, // e * 1 => e
Divide (Constant(0) e, _) => e, // 0 / e => 0
Divide (var e, Constant(1)) => e, // e / 1 => e
Add (Constant(0), var e) => e, // 0 + e => e
Add (var e, Constant(0)) => e, // e + 0 => e
Subtract(Constant(0), var e) => Negate(e), // 0 - e => -e
Subtract(var e, Constant(0)) => e, // e - 0 => e
Multiply(Constant(int x), Constant(int y)) => Constant(x * y), // x * y => e
Divide (Constant(int x), Constant(int y)) => Constant(x / y), // x / y => e
Add (Constant(int x), Constant(int y)) => Constant(x + y), // x + y => e
Subtract(Constant(int x), Constant(int y)) => Constant(x - y), // x - y => e
_ => ex
});
Assert.IsTrue(f1.EqualsTo(i => i + 20));
}
```