我有一个关于协程实现的问题。我coroutine
首先看到的是 Lua 和 stackless-python。我可以理解它的概念以及如何使用yield
关键字,但我无法弄清楚它是如何实现的。
我可以得到一些关于它们的解释吗?
首先,我是一个LISP新手.
我想得到的是一个合作的微线程功能.这可以通过协程获得.据我所知,Scheme通过continuation支持协同程序.但是,并非所有Scheme实现都可以延续.如果是这样,我可以添加仅具有LISP原语的延续特征吗?
我想用模拟对象编写F#单元测试.我正在使用NUnit.但不幸的是我找不到任何例子.
以下是测试代码的示例:
type ICustomer = interface
abstract Id: int with get
abstract Name: string with get
abstract CalculateBalanceWithDiscount: decimal -> decimal
end
type Customer = class
val id: int
val name: string
val balance: decimal
new(id, name, balance) =
{id = id; name = name; balance = balance}
interface ICustomer with
member this.Id
with get () = this.id
member this.Name
with get () = this.name
member this.CalculateBalanceWithDiscount discount =
this.balance - (discount * this.balance)
end
end
Run Code Online (Sandbox Code Playgroud) 如果我这样做any isUpper "asBsd"
,我会得到的True
.
这里,第二个元素any
是一个字符串.
但是,如果我这样做:
any ("1" `isInfixOf`) ["qas","123","=-0"]
Run Code Online (Sandbox Code Playgroud)
第二个元素any
是一个字符串列表.
如何以及为什么这两个功能之间存在差异?
另一个例子.
如果我写filter isUpper "asdVdf"
,我会得到"V"
.
这里,要过滤的第二个元素是一个字符串.
但是,如果我这样写:
filter (isUpper . head) ["abc","Vdh","12"]
我会得到的["Vdh"]
.
如您所见,要过滤的第二个元素现在是一个字符串列表.
为什么存在差异以及haskell如何知道它在两种情况下都是正确的?
总结一下:
我不明白在同一个函数中,有一次haskell得到第二个元素是一个字符串,而在其他时候,haskell得到一个字符串列表,在第二个元素中.
有一次它发生在any
功能上,另一次发生在filter
功能上.
haskell(和我)怎么知道它在两种情况下都是正确的?
谢谢 :-).
我试图解析F#类型的语法.我开始编写[F] Parsec语法并遇到问题,所以我将语法简化为:
type ::= identifier | type -> type
identifier ::= [A-Za-z0-9.`]+
Run Code Online (Sandbox Code Playgroud)
在遇到FParsec的问题之后,我转到了Parsec,因为我有一本专门解释它的书的完整章节.我的语法代码是
typeP = choice [identP, arrowP]
identP = do
id <- many1 (digit <|> letter <|> char '.' <|> char '`')
-- more complicated code here later
return id
arrowP = do
domain <- typeP
string "->"
range <- typeP
return $ "("++domain++" -> "++range++")"
run = parse (do t <- typeP
eof
return t) "F# type syntax"
Run Code Online (Sandbox Code Playgroud)
问题是Parsec默认情况下没有回溯,所以
> …
Run Code Online (Sandbox Code Playgroud) 我刚刚将原型元组升级为记录.总有一天它可能会变成一个真正的阶级.在此期间,我想翻译这样的代码:
type Example = int * int
let examples = [(1,2); (3,4); (5,6)]
let descs = Seq.map (fst >> sprintf "%d") examples
Run Code Online (Sandbox Code Playgroud)
对此:
type Example = {
Field1 : int
Field2 : int
Description : string
}
let examples = [{Field1 = 1; Field2 = 2; Description = "foo"}
{Field1 = 3; Field2 = 4; Description = "bar"}
{Field1 = 5; Field2 = 6; Description = "baz"}]
let descs = Seq.map Description examples
Run Code Online (Sandbox Code Playgroud)
问题是,Description : Example -> string
当我声明Example记录时,我希望得到一个函数,但我没有.我已经戳了一下并尝试了类的属性,但这也不起作用.我只是遗漏了文档中的内容,还是我必须手动编写高阶访问者?(这就是我现在使用的解决方法.)
f# ×3
coroutine ×2
haskell ×2
scheme ×2
accessor ×1
backtracking ×1
definition ×1
function ×1
lisp ×1
mocking ×1
parsec ×1
properties ×1
record ×1
types ×1
unit-testing ×1