不完全确定标题描述是否正确,但我确实有以下代码:
paket.dependencies:
source https://www.nuget.org/api/v2
nuget fsharpx.extras
nuget mongodb.driver
一些.fsx:
#r @".\packages\MongoDB.Bson\lib\net45\MongoDB.Bson.dll"
#r @".\packages\MongoDB.Driver\lib\net45\MongoDB.Driver.dll"
#r @".\packages\MongoDB.Driver.Core\lib\net45\MongoDB.Driver.Core.dll"
#r @".\packages\FSharpX.Extras\lib\net45\FSharpX.Extras.dll"
open MongoDB
open MongoDB.Driver
open MongoDB.Bson
open MongoDB.Bson.Serialization
open FSharpx.Choice
let private createClient (connectString:string) = MongoClient(connectString)
let CreateClient = protect createClient
let private getDb name (client:IMongoClient) = client.GetDatabase(name)
let GetDB1 name client =
choose {
let! c = client
return! (protect (getDb name) c)
}
let GetDB2 name (client:Choice<IMongoClient, exn>) =
protect (getDb name)
<!> client
此“练习”的目的是编写 GetDB2,使其与 GetDB1 的功能相同,但使用运算符(应用程序?),但我目前无法转过头来管理它。
上面的代码可以编译,但是签名 GetDB1 和 GetDB2 不相等,我显然在做一些不的事情。
val GetDB1 :
name:string ->
client:Choice<#MongoDB.Driver.IMongoClient,exn> ->
Choice<MongoDB.Driver.IMongoDatabase,exn>
val GetDB2 :
name:string ->
client:Choice<MongoDB.Driver.IMongoClient,exn> ->
Choice<Choice<MongoDB.Driver.IMongoDatabase,exn>,exn>
我在 GetDB2 中尝试了多种版本和操作顺序,但或多或少总是以与上述相同的签名结束。
我最初的总体想法是编写一些小函数来完成它们应该做的事情,然后添加异常处理(保护),然后相应地“包装”和“展开”。
这当然也可能不是完全正确的想法。
是否有人可以在这里为我指明进一步研究、代码示例或其他方面的方向?实际上,此时欢迎任何类型的评论 ;-)
附录
我认为下面的应该和上面的差不多,但是没有 mongodb 依赖。
#r @".\packages\FSharpX.Extras\lib\net45\FSharpX.Extras.dll"
type DataBase =
{
Name: string
}
type Client =
{
connectString: string
} with member this.GetDatabase name = {
Name = name
}
open FSharpx.Choice
let private createClient (connectString:string) = {
connectString= connectString
}
let CreateClient = protect createClient
let private getDb name (client:Client) = client.GetDatabase name
let GetDB1 name client =
choose {
let! c = client
return! (protect (getDb name) c)
}
let GetDB2 name client =
protect (getDb name)
<!> client
最佳答案
因为您使用了 <!>
,所以您在这里得到了类型的复合。运算符,即 map
.这是这样定义的:
let map f = function
| Choice1Of2 value = Choice1Of2 (f value)
| Choice2Of2 fail = Choice2Of2 fail
这有签名 ('T -> 'U) -> Choice<'T,'Failure> -> Choice<'U,'Failure>
,即函数 f
用作 choice
的 内部 map 类型。例如:
map (sprintf "%d")
类型为 Choice<int, 'Failure> -> Choice<string, 'Failure>
.这对于应用不使用 Choice
的函数很有用。类型 - 只有一个可能的失败点,并且发生在调用 map
之前.
但是,您的下一个函数会生成一个 Choice
类型,但它需要一个非 Choice
类型。这意味着您希望错误传播 - 如果值中有错误,则选择它。如果值没问题,但函数中有错误,则使用它。如果一切顺利,就使用它。这要求两种错误类型相同,对您来说它们是 ( exn
)。
这是描述 bind
操作,定义如下:
let bind f = function
| Choice1Of2 value = f value
| Choice2Of2 fail = Choice2Of2 fail
带签名('T -> Choice<'U,'Failure>) -> Choice<'T,'Failure> -> Choice<'U,'Failure>
.
请注意 bind
与 map
非常相似, 除了后者将结果提升为 Choice1Of2
- 映射函数总是成功的。
在 FSharpX 中,您可以访问 bind
通过 |>
-like 运算符 >>=
,或 <|
-like 运算符 <<=
.
最后,protect
是一种将抛出的异常捕获到 Choice2Of2 exn
中的奇特方法.它类似于 map
因为传递的函数是 'T -> 'U
类型的, 但函数也可以抛出异常并且传递的类型不是 Choice
. protect
定义如下:
let protect f x =
try
Choice1Of2 (f x)
with
exn -> Choice2Of2 exn
所以它的签名是('T -> 'U) -> 'T -> Choice<'U, exn>
.
有关如何实现所有内容的更多信息,请参阅 the source of this computation expression .
将所有这些放在一起,我们可以看出您的示例出错的原因。
-
getDb name
是一个函数Client -> DataBase
-
protect (getDb name)
是一个函数Client -> Choice<DataBase, exn>
-
map (protect (getDb name))
因此是一个函数Choice<Client, exn> -> Choice<Choice<DataBase, exn>, 'Failure>
,因为map
在里面工作Choice
.
不过,你想要的是
let GetDB name client =
bind (protect (getDb name)) client
或运算符形式,
let GetDB name client = client >>= protect (getDb name)
一般来说,如果你的映射函数有签名 'T -> 'U
你想要map
.如果它有 'T -> Choice<'U, 'Failure>
,你想要bind
.
关于f# - 计算表达式 vs 应用仿函数等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40681754/