评估 F# 中引用的表达式

发布于 2024-07-18 18:28:32 字数 320 浏览 5 评论 0原文

我希望我没有错过一些明显的东西,但我一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

胡渣熟男 2024-07-25 18:28:33

不,没有内置的方法来编译 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.

錯遇了你 2024-07-25 18:28:33

您可以使用 FSharp.PowerPack.Linq DLL 提供的 Eval 扩展成员来计算 F# 引用,如下所示:

#r "FSharp.PowerPack.Linq.dll"

open Linq.QuotationEvaluation
let f = <@2 + 3@>
f.Eval()

请注意,您必须打开 Linq.QuotationEvaluation 命名空间使此扩展成员可用。

还有一个 Compile 扩展成员返回暂停,但它似乎并没有提高性能。

You can evaluate an F# quotation using the Eval extension member provided by the FSharp.PowerPack.Linq DLL as follows:

#r "FSharp.PowerPack.Linq.dll"

open Linq.QuotationEvaluation
let f = <@2 + 3@>
f.Eval()

Note that you must open the Linq.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.

慵挽 2024-07-25 18:28:33

2016 年更新

现在可以在 NuGet 包 FSharp.Quotations.Evaluator 中找到 Evaluate 扩展方法

#r "../packages/FSharp.Quotations.Evaluator.1.0.7/lib/net40/FSharp.Quotations.Evaluator.dll"

open FSharp.Quotations.Evaluator

let f = <@ 2 + 3 @>
f.Evaluate()

Updated in 2016

The Evaluate extension method can now be found in NuGet package FSharp.Quotations.Evaluator

#r "../packages/FSharp.Quotations.Evaluator.1.0.7/lib/net40/FSharp.Quotations.Evaluator.dll"

open FSharp.Quotations.Evaluator

let f = <@ 2 + 3 @>
f.Evaluate()
久光 2024-07-25 18:28:33

我认为引用有一个 .eval() 方法。

I think the quotations has got a .eval()-method.

人事已非 2024-07-25 18:28:32

我已经实现了一个基于反射的引用计算器,作为取消引用的一部分(这是 2.0 版的一项新功能。 0)。

> #r @"..\packages\Unquote.2.2.2\lib\net40\Unquote.dll"

--> Referenced '..\packages\Unquote.2.2.2\lib\net40\Unquote.dll'

> Swensen.Unquote.Operators.eval <@ sprintf "%A" (1,2) @>;;
val it : string = "(1, 2)"

我测得它比 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).

> #r @"..\packages\Unquote.2.2.2\lib\net40\Unquote.dll"

--> Referenced '..\packages\Unquote.2.2.2\lib\net40\Unquote.dll'

> Swensen.Unquote.Operators.eval <@ sprintf "%A" (1,2) @>;;
val it : string = "(1, 2)"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文