我希望创建一个构建器,用于构建表达式,每个步骤后返回类似续延的东西。像这样:
```html
```html
我想创建一个构建器,构建表达式,每一步之后都会返回类似续延的东西。
例如:
```module TwoSteps =
let x = stepwise {
let! y = "foo"
printfn "got: %A" y
let! z = y + "bar"
printfn "got: %A" z
return z
}
printfn "two steps"
let a = x()
printfn "something inbetween"
let b = a()
'let a'这行返回包含待稍后评估的其余表达式的内容。
为每个步骤使用单独的类型来完成此操作很简单,但显然不是特别有用:
type Stepwise() =
let bnd (v: 'a) rest = fun () -> rest v
let rtn v = fun () -> Some v
member x.Bind(v, rest) =
bnd v rest
member x.Return v = rtn v
let stepwise = Stepwise()
module TwoSteps =
let x = stepwise {
let! y = "foo"
printfn "got: %A" y
let! z = y + "bar"
printfn "got: %A" z
return z
}
printfn "two steps"
let a = x()
printfn "something inbetween"
let b = a()
module ThreeSteps =
let x = stepwise {
let! y = "foo"
printfn "got: %A" y
let! z = y + "bar"
printfn "got: %A" z
let! z' = z + "third"
printfn "got: %A" z'
return z
}
printfn "three steps"
let a = x()
printfn "something inbetween"
let b = a()
printfn "something inbetween"
let c = b()
结果符合我的预期:
two steps
got: "foo"
something inbetween
got: "foobar"
three steps
got: "foo"
something inbetween
got: "foobar"
something inbetween
got: "foobarthird"
但我无法确定这种情况的一般性。
我想要的是能够将事件输入到此工作流中,这样你可以编写类似以下的内容:
let someHandler = Stepwise<someMergedEventStream>() {
let! touchLocation = swallowEverythingUntilYouGetATouch()
startSomeSound()
let! nextTouchLocation = swallowEverythingUntilYouGetATouch()
stopSomeSound()
}
并且使事件触发工作流程的下一步。(特别是,我想在MonoTouch - F# on the iPhone中尝试这种操作。传递objc选择器让我感到疯狂。)