使用直接外部函数的版本(calc1)大约需要1秒.
但是带有传递函数作为函数参数的版本(calc2)大约需要2s,即慢2倍.为什么?
open System.Diagnostics
open System.Numerics
let width = 1920
let height = 1200
let xMin = -2.0
let xMax = 1.0
let yMin = -1.0
let yMax = 1.0
let scaleX x = float x * (xMax - xMin) / float width + xMin
let scaleY y = float y * (yMax - yMin) / float height - yMax
let fn (z:Complex) (c:Complex) = z * z + c
let calc1 width height =
let iterFn z c = …Run Code Online (Sandbox Code Playgroud) 我正在为类似于SQL的语言开发解析器,我遇到了创建一些语言规则的问题,例如:expression IS NULL和expression IN (expression1, expression2, ...)逻辑和数学运算符之间的优先级.
我上传了一个GitHub测试项目https://github.com/anpv/SpracheTest/,但这个变种并不好.
我试图使用以下规则:
private static readonly Parser<AstNode> InOperator =
from expr in Parse.Ref(() => Expression)
from inKeyword in Parse.IgnoreCase("in").Token()
from values in Parse
.Ref(() => Expression)
.DelimitedBy(Comma)
.Contained(OpenParenthesis, CloseParenthesis)
select new InOperator(expr, values);
private static readonly Parser<AstNode> IsNullOperator =
from expr in Parse.Ref(() => Expression)
from isNullKeyword in Parse
.IgnoreCase("is")
.Then(_ => Parse.WhiteSpace.AtLeastOnce())
.Then(_ => Parse.IgnoreCase("null"))
select new IsNullOperator(expr);
private static readonly Parser<AstNode> Equality =
Parse
.ChainOperator(Eq, IsNullOperator.Or(InOperator).Or(Additive), …Run Code Online (Sandbox Code Playgroud)