我怎样才能避免 fp-ts 中带有链条的厄运金字塔?

How can I avoid the pyramid of doom with chain in fp-ts?

我经常遇到这种情况,我需要完成几个顺序操作。如果每个操作都只使用上一步的数据,那么我可以很高兴地做 pipe(startingData, TE.chain(op1), TE.chain(op2), TE.chain(op3), ...) 之类的事情。当 op2 还需要来自 startingData 的数据时,没有一堆嵌套回调,我找不到写这个的好方法。

如何避免下面示例中的厄运金字塔?

declare const op1: (x: {one: string}) => TE.TaskEither<Error, string>;
declare const op2: (x: {one: string, two: string}) => TE.TaskEither<Error, string>;
declare const op3: (x: {one: string, two: string, three: string}) => TE.TaskEither<Error, string>;

pipe(
  TE.of<Error, string>('one'),
  TE.chain((one) =>
    pipe(
      op1({ one }),
      TE.chain((two) =>
        pipe(
          op2({ one, two }),
          TE.chain((three) => op3({ one, two, three }))
        )
      )
    )
  )
);

问题的解决方案,它被称为“do notation”。它已经在 fp-ts-contrib 中可用了一段时间,但它现在也有一个使用 bind 函数(在所有 monadic 类型上定义)嵌入到 fp-ts 中的版本。基本思想与我在下面所做的类似——我们将计算结果绑定到一个特定的名称,并在进行过程中在“上下文”对象中跟踪这些名称。这是代码:

pipe(
  TE.of<Error, string>('one'),
  TE.bindTo('one'), // start with a simple struct {one: 'one'}
  TE.bind('two', op1), // the payload now contains `one`
  TE.bind('three', op2), // the payload now contains `one` and `two`
  TE.bind('four', op3), // the payload now contains `one` and `two` and `three`
  TE.map(x => x.four)  // we can discharge the payload at any time
)

下面是原始答案

我提出了一个我不太引以为豪的解决方案,但我分享它以征求可能的反馈!

首先,定义一些辅助函数:

function mapS<I, O>(f: (i: I) => O) {
  return <R extends { [k: string]: I }>(vals: R) =>
    Object.fromEntries(Object.entries(vals).map(([k, v]) => [k, f(v)])) as {
      [k in keyof R]: O;
    };
}
const TEofStruct = <R extends { [k: string]: any }>(x: R) =>
  mapS(TE.of)(x) as { [K in keyof R]: TE.TaskEither<unknown, R[K]> };

mapS 允许我将函数应用于对象中的所有值(子问题 1:是否有允许我执行此操作的内置函数?)。 TEofStruct 使用此函数将值结构转换为这些值的 TaskEither 结构。

我的基本想法是使用 TEofStructsequenceS 将新值与以前的值一起累加。到目前为止它看起来像这样:

pipe(
  TE.of({
    one: 'one',
  }),
  TE.chain((x) =>
    sequenceTE({
      two: op1(x),
      ...TEofStruct(x),
    })
  ),
  TE.chain((x) =>
    sequenceTE({
      three: op2(x),
      ...TEofStruct(x),
    })
  ),
  TE.chain((x) =>
    sequenceTE({
      four: op3(x),
      ...TEofStruct(x),
    })
  )
);

感觉我可以写一些辅助函数,结合 sequenceTETEofStruct 来减少这里的样板文件,但我仍然不确定这是否是正确的方法,或者如果有更惯用的模式!

官方 fp-ts DO 符号在这里:https://gcanti.github.io/fp-ts/guides/do-notation.html

但是嵌套逻辑提取到自己的函数没有错