我对函数式编程非常陌生,并且在数组上运行遍历时遇到了很多困难。
当我阅读这本书时,似乎我应该能够简单地在 Monad 之间遍历,但我无法用fp-ts来理解这个概念。
有人可以使用或任何其他方式解释以下内容array.traverse/sequence
吗?
- 我怎样才能从
TaskEither<Error, string[]>
到TaskEither<Error, Either<Error, string>[]>
; 还是有更好的方法可以从单个错误变为嵌套错误,同时保持打字简单? - 我怎样才能从
TaskEither<Error, Either<Error, string[]>>
类似TaskEither<Error, Option<string[]>>
或类似的东西?还是我们应该将该函数的结果映射到返回Either
?
考虑下面的简化代码,以便更好地了解我们正在使用这些数组做什么:
// helper functions
declare function toItems(input: string): TaskEither<Error, string[]);
declare function toTitle(item: string): Either<Error, string>;
declare function clean(item: string): Option<string>;
// This is what I tried so far
const program = (input: string) => pipe(
toItems(input), // we have TaskEither<Error, string[]>
TE.map(items => array.traverse(either)(items, toTitle)), // now we have TaskEither<Error, Either<Error, string[]>>
TE.map(items => array.traverse(option)(items, clean)), // nothing works with clean() from here
)