question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing Lambda expression pre-evalutes it with given arguments

See original GitHub issue

When Parsing lambda expression, it evaluates the predicate and cannot be reused (e.g. from cache) with another parameter value. See an example that demonstrates it.

var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
var list = new Parameter("list", new[] { 1, 2, 3 });
var value1 = new Parameter("value", 1);
var value2 = new Parameter("value", 2);
var expression = "list.Where(x => x > value)";
var lambda = target.Parse(expression, list, value1);
var result = lambda.Invoke(list, value2);
Assert.AreEqual(new[] { 3 }, result);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
metoulecommented, Dec 13, 2021

I have an idea that might work, but I’m not yet sure it will. The idea is to create a dictionary of all the captured variables, and access it when needed:

  • create a body (instead of a single expression) for inner lambda expressions
  • create an expression that sets the dictionary’s value for all parent parameters on enter
  • parse the expression, and when an identifier is encountered, check whether it’s a captured variable

Proof of concept of such a body’s creation:

var dict = new Dictionary<string, object>();
var captured = Expression.Constant(dict);

// create an expression for x + 5
var param = Expression.Parameter(typeof(int), "x");
var five = Expression.Constant(5);
var add = Expression.Add(param, five);

// create the expression that sets the captured variable's value
// => dict["x"] = 5
var indexer = Expression.Property(captured, dict.GetType().GetProperty("Item"), new Expression[] { Expression.Constant(param.Name) });
var set = Expression.Assign(indexer, Expression.Convert(param, typeof(object)));

// create the delegate with a body
var block = Expression.Block(set, add);
var lambda = Expression.Lambda(block, param);
var del = lambda.Compile();

del.DynamicInvoke(2).Dump();
dict.Dump(); // contains x == 2

del.DynamicInvoke(3).Dump();
dict.Dump(); // contains x == 3
0reactions
metoulecommented, Dec 14, 2021

I have a working prototype, that also fixes the limitation I highlighted in #200. It still needs some work, but I might wait until your PR is merged because it impacts the Lambda class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there an easy way to parse a (lambda expression) string ...
There is no general way to parse a string into a lambda expression without a full compilation, because lambda expressions can reference ...
Read more >
Lambda expressions and anonymous functions
To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator and an expression or...
Read more >
Make Your Own Language 6: Parsing Lambda Functions
This is the sixth episode of a series where you build your first programming language, using these videos as a guide.
Read more >
C# Lambda Expressions Simplified | Syntax & Practical ...
The lambda operator =>, which may be read as “goes to” or “becomes,” is used in all lambda expressions. The Input Parameters are...
Read more >
How to Use Python Lambda Functions
Arguments. Like a normal function object defined with def , Python lambda expressions support all the different ways of passing arguments. This includes:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found