计算表达式与应用函子和什么不是

时间:2016-11-18 16:22:18

标签: f# monads computation-expression fsharpx

不完全确定标题是否正确,但我确实有以下代码:

paket.dependencies:

source https://www.nuget.org/api/v2
nuget fsharpx.extras
nuget mongodb.driver

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

这点&#34; excersise&#34;是写GetDB2,以便它与GetDB1一样,但使用运算符(applicatives?),但我现在无法转过头去管理它。

上面的代码编译,但签名 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中尝试过多个版本和服务订单,但我或多或少总是以与上面相同的签名结束。

我最初的一般想法是编写小函数来做他们应该做的事情,然后添加异常处理(保护),然后&#34; wrap&#34;和&#34;展开&#34;因此。

这当然也不是完全正确的想法。

是否有人能够指点我在这里进行进一步的学习,代码示例或其他什么?任何类型的评论实际上都是欢迎的; - )

FSharpx doc

附录

我认为以下内容与上述内容大致相同,但没有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

1 个答案:

答案 0 :(得分:5)

您在此处获得了类型的复合,因为您使用了<!>运算符,即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 (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>

请注意bindmap非常相似,只是后者将结果提升为Choice1Of2 - 映射函数始终成功。

在FSharpX中,您可以通过bind - 类似运算符|>或类似>>=的运算符<|访问<<=

最后,protect是一种将抛出的异常捕获到Choice2Of2 exn的奇特方式。它类似于map,因为传递的函数的类型为'T -> 'U,但该函数也可以抛出异常而传递的类型是而不是 Choiceprotect定义如下:

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>
  • 因此,{li> map (protect (getDb name))是一个函数Choice<Client, exn> -> Choice<Choice<DataBase, exn>, 'Failure>,因为mapChoice内工作。

但你想要的是

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