Hel*_*olm 3 monads f# computation-expression fsharpx
不完全确定标题描述没问题,但我确实有以下代码:
paket.dependencies:
source https://www.nuget.org/api/v2
nuget fsharpx.extras
nuget mongodb.driver
Run Code Online (Sandbox Code Playgroud)
some.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
Run Code Online (Sandbox Code Playgroud)
这个"excersise"的意思是编写GetDB2,以便它与GetDB1一样,但使用运算符(applicatives?),但我现在无法转过头去管理它.
上面的代码编译,但对于GetDB1和GetDB2签名是不相等的,和Im显然做一些不正确的.
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>
Run Code Online (Sandbox Code Playgroud)
我已经在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
Run Code Online (Sandbox Code Playgroud)
你在这里得到了类型的复合,因为你已经使用了<!>运算符map.这是这样定义的:
let map f = function
| Choice1Of2 value = Choice1Of2 (f value)
| Choice2Of2 fail = Choice2Of2 fail
Run Code Online (Sandbox Code Playgroud)
这具有签名('T -> 'U) -> Choice<'T,'Failure> -> Choice<'U,'Failure>,即该函数f用作类型内的映射choice.例如:
map (sprintf "%d")
Run Code Online (Sandbox Code Playgroud)
有类型Choice<int, 'Failure> -> Choice<string, 'Failure>.这适用于不使用该Choice类型的函数- 只有一个可能的失败点,并且发生在调用之前map.
但是,您的下一个函数会生成一个Choice类型,但它需要一个非Choice类型.这意味着您希望错误传播 - 如果值中存在错误,则选择该错误.如果值很好,但函数中有错误,那么使用它.如果一切顺利,请使用它.这需要两个错误类型相同,对你来说它们是(exn).
这是描述bind操作,定义如下:
let bind f = function
| Choice1Of2 value = f value
| Choice2Of2 fail = Choice2Of2 fail
Run Code Online (Sandbox Code Playgroud)
签名('T -> Choice<'U,'Failure>) -> Choice<'T,'Failure> -> Choice<'U,'Failure>.
注意与之bind非常相似map,只是后者将结果提升为Choice1Of2- 映射函数总是成功的.
在FSharpX,您可以访问bind由|>式的操作>>=,或者<|样操作<<=.
最后,protect是一种将抛出的异常捕获到一个奇特的方式Choice2Of2 exn.它类似于map在传递函数类型'T -> 'U,但功能也可以抛出一个异常,并传递类型是不是一个Choice. protect定义如下:
let protect f x =
try
Choice1Of2 (f x)
with
exn -> Choice2Of2 exn
Run Code Online (Sandbox Code Playgroud)
所以它的签名是('T -> 'U) -> 'T -> Choice<'U, exn>.
有关如何实现所有内容的更多信息,请参阅此计算表达式的来源.
把这一切放在一起,我们可以看出为什么你的例子出了问题.
getDb name 是一个功能 Client -> DataBaseprotect (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
Run Code Online (Sandbox Code Playgroud)
或以运营商形式,
let GetDB name client = client >>= protect (getDb name)
Run Code Online (Sandbox Code Playgroud)
通常,如果您的映射函数具有'T -> 'U您想要的签名map.如果有'T -> Choice<'U, 'Failure>,你想要的bind.