首页 > 解决方案 > FP-TS 分支(面向铁路的编程)

问题描述

我在尝试使用 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<Error, TypeB> ,地图处理错误对我来说很好。

而要执行最后一个操作,我现在需要输入 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 的文档很少包含代码示例,而且我不知道如何使用它们。

标签: typescriptfp-ts

解决方案


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

  1. 使用嵌套调用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`
    )
  })
)
  1. Using Donotation: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 notation 的语法。


推荐阅读