【发布时间】:2019-05-31 06:25:07
【问题描述】:
使用 Suave.IO,我有以下单体 WebPart:
let success msg =
Successful.OK <| sprintf "Success: %s" msg
let error msg =
Successful.OK <| sprintf "Error: %s" msg
let monolith arg1 arg2 =
if doFirstThing arg1 then
if doSecondThing arg2 then
success "Everything worked"
else
error "Second thing failed"
else
error "First thing failed"
作为一名优秀的函数式程序员,我想将单体应用分解为不同的组件。最好的方法是什么?
我的第一次尝试使用“延续”Web 部件,如下所示:
let first arg cont =
if doFirstThing arg then cont
else error "First thing failed"
let second arg cont =
if doSecondThing arg then cont
else error "Secong thing failed"
let third : WebPart =
success "Everything worked"
但是,由于嵌套调用,将这些组合在一起看起来很难看:
first 1 (second 2 third)
有没有更好的方法来做到这一点?具体来说,我可以在每个组件之间插入一个运算符以便优雅地组合它们吗?比如:
first 1 >?> second 2 >?> third
我的直觉说这样的事情应该可行,但我没有足够的类别理论来正确地做到这一点。欢迎任何见解。
【问题讨论】:
标签: f# combinators suave