3

我目前正在学习 F# 并遇到了一些绊脚石;我认为其中很多是在学习功能性思考。

我目前正在学习的一件事是计算表达式,我希望能够定义一个处理某些跟踪状态的计算表达式,例如:

let myOptions = optionListBuilder {
    let! opt1 = {name="a";value=10}
    let! opt2 = {name="b";value=12}
}

我希望能够拥有它myOptionsOption<'T> list因此每个let!绑定操作都会有效地使构建器在执行过程中“跟踪”定义的选项。

我不想使用可变状态来做这件事——例如,有一个由构建器维护的列表,并随着每次bind调用而更新。

有没有办法让它成为可能?


更新:结果Option<'T> list类型只是代表性的,实际上我可能会有一个OptionGroup<'T>类型来包含一个列表以及一些附加信息 - 所以正如 Daniel 下面提到的,我可以使用一个简单列表的列表推导。

4

3 回答 3

5

我在这里写了一个字符串生成器计算表达式。

open System.Text

type StringBuilderUnion =
| Builder of StringBuilder
| StringItem of string

let build sb =
    sb.ToString()

type StringBuilderCE () =
    member __.Yield (txt : string) = StringItem(txt)
    member __.Yield (c : char) = StringItem(c.ToString())
    member __.Combine(f,g) = Builder(match f,g with
                                     | Builder(F),   Builder(G)   ->F.Append(G.ToString())
                                     | Builder(F),   StringItem(G)->F.Append(G)
                                     | StringItem(F),Builder(G)   ->G.Append(F)
                                     | StringItem(F),StringItem(G)->StringBuilder(F).Append(G))
    member __.Delay f = f()
    member __.Zero () = StringItem("")
    member __.For (xs : 'a seq, f : 'a -> StringBuilderUnion) =
                    let sb = StringBuilder()
                    for item in xs do
                        match f item with
                        | StringItem(s)-> sb.Append(s)|>ignore
                        | Builder(b)-> sb.Append(b.ToString())|>ignore
                    Builder(sb)

let builder1 = new StringBuilderCE ()

注意底层类型是不可变的(包含的类型StringBuilder是可变的,但不一定是)。每个 yield 不是更新现有数据,而是结合当前状态和传入输入,从而产生一个新实例StringBuilderUnion You can do this with F# list 因为将元素添加到列表头部仅仅是构造一个新值而不是而不是改变现有的值。

使用如下StringBuilderCE外观:

//Create a function which builds a string from an list of bytes
let bytes2hex (bytes : byte []) =
    string {
        for byte in bytes -> sprintf "%02x" byte
    } |> build

//builds a string from four strings
string {
        yield "one"
        yield "two"
        yield "three"
        yield "four"
    } |> build

注意到yield而不是let!因为我实际上不想使用计算表达式中的值。

于 2014-12-12T17:05:48.793 回答
3

解决方案

使用 mydogisbox 提供的基线 StringBuilder CE 构建器,我能够生成以下具有魅力的解决方案:

type Option<'T> = {Name:string;Item:'T}

type OptionBuilderUnion<'T> =
    | OptionItems of Option<'T> list
    | OptionItem of Option<'T>

type OptionBuilder () =
    member this.Yield (opt: Option<'t>) = OptionItem(opt)
    member this.Yield (tup: string * 't) = OptionItem({Name=fst tup;Item=snd tup})
    member this.Combine (f,g) = 
        OptionItems(
            match f,g with
            | OptionItem(F), OptionItem(G) -> [F;G]
            | OptionItems(F), OptionItem(G) -> G :: F
            | OptionItem(F), OptionItems(G) -> F :: G
            | OptionItems(F), OptionItems(G) -> F @ G
        )
    member this.Delay f = f()
    member this.Run (f) = match f with |OptionItems items -> items |OptionItem item -> [item]

let options = OptionBuilder()

let opts = options {
        yield ("a",12)
        yield ("b",10)
        yield {Name = "k"; Item = 20}
    }

opts |> Dump
于 2014-12-12T17:48:44.943 回答
2

F# 支持开箱即用的列表推导。

let myOptions =
    [
        yield computeOptionValue()
        yield computeOptionValue()
    ]
于 2014-12-12T17:11:28.027 回答