是否有可能通过替代标记欺骗表达式树序列化来支持递归?
我们都知道表达式树不支持递归。 我可以创建一个递归 Func,将其包装在表达式树中,然后调用它。
Func<int, int> func1 = null;
func1 = x => (x == 0) ? 1 : x * func1(x - 1);
Expression<Func<int, int>> expression1 = i => func1(i);
var func1 = expression1.Compile();
var z1 = func1.Invoke(5);
但是,当我尝试序列化(例如使用 MetaLINQ)时,我当然会得到一个异常,因为它是一个递归表达式。
Lisp 构建表达式树并支持递归。基因编程使表达树发生突变。有些类型的问题的解决方案本质上是递归的,因为它们需要跟踪先前的状态:树遍历、深度优先搜索和分而治之算法。我的目标是在托管分布式环境中生成有效的算法,在该环境中分支可以传递和重新组合。
我的问题是:如何覆盖序列化/反序列化以使用非递归“模拟”令牌换出/换入调用,在重新加载反序列化树时将递归调用换回?
We all know that Expression Trees do not support recursion.
I can create a recursive Func, wrap it in an expression tree, and invoke it.
Func<int, int> func1 = null;
func1 = x => (x == 0) ? 1 : x * func1(x - 1);
Expression<Func<int, int>> expression1 = i => func1(i);
var func1 = expression1.Compile();
var z1 = func1.Invoke(5);
However, when I try to serialize (eg using MetaLINQ) , I of course get an exception as it is a recursive expression.
Lisp constructs expression trees and supports recursion. Genetic Programming mutates expression trees. There are some types of problems whose solutions are inherently recursive, because of prior state they need to track: tree traversal, depth-first search, and divide-and-conquer algorithms. I am aiming to generate efficient algorithms within a managed distributed environment, where branches can be passed around and recombined.
My question is: How could I override the serialization / deserialization to swap out / in the call with a non recursive "mock" token , swapping the recursive call back in when reloading the deserialized tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这不是您问题的准确答案,但我会应用“用迭代重构替换递归”模式。
递归通常被认为是不好的,有时被认为是懒惰的。然而,我理解需要首先解决问题,然后重构代码,以便可以轻松维护和阅读。
I know this is not an exact answer to your question, but I would apply the Replace Recursion with Iteration Refactoring pattern.
Recursion is usually considered bad, and sometime looked upon as lazy. I however understand the need to solve the problem first, then refactor the code so that is can be maintained and read with ease.