ExpressionToCode icon indicating copy to clipboard operation
ExpressionToCode copied to clipboard

You also know https://github.com/jbevain/mono.linq.expressions

Open jogibear9988 opened this issue 5 years ago • 4 comments

There exists also https://github.com/jbevain/mono.linq.expressions

Maybe it will useful for you?

jogibear9988 avatar Aug 22 '18 07:08 jogibear9988

The generated code appears to be fairly hard to use; e.g.

var x=1;
var a=2;
var b=3;
  
DumpExpr(() => x - (a - b) + x * (a + b));

prints () => x - (a - b) + x * (a + b) when implemented with ExpressionToCode, but when using Mono.Linq.Expressions it prints...

int ()
{
  return (UserQuery+<>c__DisplayClass0_0.x - (UserQuery+<>c__DisplayClass0_0.a - UserQuery+<>c__DisplayClass0_0.b)) + (UserQuery+<>c__DisplayClass0_0.x * (UserQuery+<>c__DisplayClass0_0.a + UserQuery+<>c__DisplayClass0_0.b));
}

Another example:

DumpExpr(() => Enumerable.Range(2, 1000).Where(n => !Enumerable.Range(2, n - 2).Any(p => n % p == 0)).Select(prime => $"Number {prime} is prime!"));

results in

() => Enumerable.Range(2, 1000).Where(n => !Enumerable.Range(2, n - 2).Any(p => n % p == 0)).Select(prime => $"Number {prime} is prime!")

vs.

IEnumerable<string> ()
{
  return Enumerable.Select<int, string>(Enumerable.Where<int>(Enumerable.Range(2, 1000), (int n) =>
  {
    return !Enumerable.Any<int>(Enumerable.Range(2, n - 2), (int p) =>
    {
      return (n % p) == 0;
    });
  }), (int prime) =>
  {
    return string.Format("Number {0} is prime!", (object)prime);
  });
}

EamonNerbonne avatar Aug 22 '18 09:08 EamonNerbonne

ExpressionToCode also intrinsically needs more detail than merely a string-output from an expression, because it tries to annotate sub-expressions with values. That may be possible with Mono.Linq.Expressions, but it isn't at first glance at any rate.

I like the inliner (is it an inliner? I'm thinking of Combine); and the fluent API is a nice livability enhancement. A combination of the two libraries might make sense; but it would be quite the undertaking (and I wouldn't want to do that unless the maintainers of Mono.Linq.Expressions want that too ;-))

EamonNerbonne avatar Aug 22 '18 09:08 EamonNerbonne

Only wanted to let you know. Didn't know if you heard of it. Don't think @jbevain does maintain it very much

jogibear9988 avatar Aug 22 '18 13:08 jogibear9988

I hadn't, and thanks for the heads up! It's always useful to have those kind of pointers for future changes 👍 .

EamonNerbonne avatar Aug 22 '18 14:08 EamonNerbonne