评估 F# 中引用的表达式
我希望我没有错过一些明显的东西,但我一直在使用 F# 表达式,并且我想即时评估引用的表达式。 例如,我想编写如下内容:
let x = <@ 2 * 5 @>
let y = transform x // replaces op_Multiply with op_Addition, or <@ 2 + 5 @>
let z = eval y // dynamically evaluates y, returns 7
是否有内置的 F# 方法可以计算引用的表达式,或者我必须编写自己的方法吗?
I hope I haven't missed something obvious, but I've been playing with F# expressions and I want to evaluate quoted expressions on the fly. For example, I want write something like this:
let x = <@ 2 * 5 @>
let y = transform x // replaces op_Multiply with op_Addition, or <@ 2 + 5 @>
let z = eval y // dynamically evaluates y, returns 7
Is there a built-in F# method which can evaluate quoted expressions, or do I have to write my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,没有内置的方法来编译 F# 引用。 使用 PowerPack LINQ,您可以将一些引用转换为 .NET System.Linq.Expressions.Expression,并使用它来编译它们。
进行引用是为了允许对代码进行其他解释,例如针对 SQL 或 GPU 卡。
然而,在 hubfs 上的帖子中,有人暗示这是一个常见的请求,将会受到关注。
No, there's no built-in way to compile F# quotations. With the PowerPack LINQ you can convert SOME quotations to .NET System.Linq.Expressions.Expression, and use that to compile them.
Quotations were made to allow other interpretations of code, such as targeting SQL or a GPU card.
However, in posts on hubfs, it's been hinted at that this is a common request and will be looked at.
您可以使用
FSharp.PowerPack.Linq
DLL 提供的Eval
扩展成员来计算 F# 引用,如下所示:请注意,您必须
打开
Linq.QuotationEvaluation
命名空间使此扩展成员可用。还有一个
Compile
扩展成员返回暂停,但它似乎并没有提高性能。You can evaluate an F# quotation using the
Eval
extension member provided by theFSharp.PowerPack.Linq
DLL as follows:Note that you must
open
theLinq.QuotationEvaluation
namespace to make this extension member available.There is also a
Compile
extension member that returns a suspension but it does not appear to improve performance.2016 年更新
现在可以在 NuGet 包
FSharp.Quotations.Evaluator
中找到Evaluate
扩展方法Updated in 2016
The
Evaluate
extension method can now be found in NuGet packageFSharp.Quotations.Evaluator
我认为引用有一个
.eval()
方法。I think the quotations has got a
.eval()
-method.我已经实现了一个基于反射的引用计算器,作为取消引用的一部分(这是 2.0 版的一项新功能。 0)。
我测得它比 PowerPack 评估器快 50 倍。 当然,这会因情况而异。 但 Unquote 在解释表达式方面通常比 PowerPack 快很多。
它还支持比 PowerPack 求值器更多的表达式,包括 VarSet、PropertySet、FieldSet、WhileLoop、ForIntegerRangeLoop 和 Quote。 事实上,Unquote 的求值器支持除 NewDelegate、AddressSet 和 AddressOf 之外的所有引用表达式,我计划最终支持所有这些表达式。
I've implemented a reflection-based Quotation evaluator as part of Unquote (this is a new feature as of version 2.0.0).
I've measured it to be up to 50 times faster than PowerPack's evaluator. This will, of course, vary by scenario. But Unquote is generally magnitudes faster than PowerPack at interpreting expressions.
It also supports many more expressions than PowerPack's evaluator, including VarSet, PropertySet, FieldSet, WhileLoop, ForIntegerRangeLoop, and Quote. In fact, Unquote's evaluator supports all quotation expressions except NewDelegate, AddressSet, and AddressOf all of which I plan on eventually supporting.