gpt4 book ai didi

typescript - FP-TS 分支(面向铁路的编程)

转载 作者:行者123 更新时间:2023-12-04 12:27:25 24 4
gpt4 key购买 nike

我在尝试使用 FP-TS 实现事物时不断遇到的一种模式是,当我有涉及分支和合并 TaskEither 分支的管道时。
合并似乎工作得很好,因为我可以使用 sequenceT 创建数组并将它们通过管道传输到函数中,然后使用所有这些值。
似乎效果不佳的是更复杂的依赖图,其中一个函数中需要较早的项目,然后需要该函数的输出以及第一个任务的原始结果。
基本上像这样的函数签名(这可能不是 100% 正确的类型,但请了解它的要点):

function fetchDataA(): TaskEither<Error, TypeA> {
}

function fetchBBasedOnOutputOfA(a: TypeA): TaskEither<Error, TypeB> {
}

function fetchCBasedOnOutputOfAandB(a: TypeA, b: TypeB): TaskEither<Error, TypeC> {
}
因为在管道中,你可以很好地为前两个组合
pipe(
fetchDataA(),
TE.map(fetchBBasedOnOutputOfA)
)
这个管道按预期返回 TaskEither , map 处理错误对我来说很好。
而要执行最后一个操作,我现在需要输入 TypeA 作为参数,但它不可用,因为它已传递给 B。
一种解决方案是让函数 B 同时输出 A 和 B,但这感觉不对,因为创建 B 的函数不应该知道其他一些函数也需要 A。
另一种方法是创建某种中间函数来存储 A 的值,但在我看来,这打破了使用 TaskEither 的全部意义,这是我抽象出所有错误类型并自动处理的。
我会有一些奇怪的功能:
async function buildC(a : TypeA): TaskEither<Error, TypeC> {
const b = await fetchBBasedOnOutputOfA(a);
// NOW DO MY OWN ERROR HANDLING HERE :(
if (isRight(b)) {
return fetchCBasedOnOutputOfAandB(a, b);
}
// etc.
那么有没有更惯用的方法来做到这一点,也许创建树结构并遍历它们?尽管老实说,Traverse 的文档很少包含代码示例,我也不知道如何使用它们。

最佳答案

我想说有两种惯用的写法:

  • 使用嵌套调用 chain :

  • pipe(
    fetchDataA(),
    TE.chain(a => { // capture `a` here
    return pipe(
    fetchBBasedOnOutputOfA(a), // use `a` to get `b`
    TE.chain(b => fetchCBasedOnOutputOfAandB(a, b)) // use `a` and `b` to get `c`
    )
    })
    )
  • 使用 Do符号:fp-ts公开了一个“do”语法,可以用 chain 缓解过度嵌套。 ,尤其是当您需要捕获大量稍后在程序流的不同部分中重用的值时。

  • pipe(
    // begin the `do` notation
    TE.Do,
    // bind the first result to a variable `a`
    TE.bind('a', fetchDataA),
    // use your `a` to get your second result, and bind that to the variable `b`
    TE.bind('b', ({ a }) => fetchBBasedOnOutputOfA(a)),
    // finally, use `a` and `b` to get your third result, and return it
    TE.chain(({ a, b }) => fetchCBasedOnOutputOfAandB(a, b))
    );
    您可以查看 Do 符号的语法 here .

    关于typescript - FP-TS 分支(面向铁路的编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69579430/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com