[英]Managing array of monads in fp-ts and Functional Programming
I'm very new to Functional Programming and I'm struggling a lot with running traverse on arrays.我对函数式编程非常陌生,并且在 arrays 上运行遍历时遇到了很多困难。
When I read this book it seems that I should be able to simply traverse between Monads but I can't wrap my head around this concept with fp-ts .当我阅读这本书时,似乎我应该能够简单地在 Monad 之间遍历,但我无法用fp-ts来理解这个概念。
Can someone explain the following using array.traverse/sequence
or any other ways please?有人可以使用array.traverse/sequence
或任何其他方式解释以下内容吗?
TaskEither<Error, string[]>
to TaskEither<Error, Either<Error, string>[]>
;我怎样才能 go 从TaskEither<Error, string[]>
到TaskEither<Error, Either<Error, string>[]>
; or is there a better way to go from a single error to nested errors while keeping the typings simple?或者有没有更好的方法将 go 从单个错误变为嵌套错误,同时保持打字简单?TaskEither<Error, Either<Error, string[]>>
to something like TaskEither<Error, Option<string[]>>
or something similar;我怎样才能从TaskEither<Error, Either<Error, string[]>>
go 到TaskEither<Error, Option<string[]>>
或类似的东西; or should we map the result of that function to turn back to Either
?还是我们应该将 map 的结果 function 转回Either
?Consider the following simplified code to have a better idea what we are doing with these arrays:考虑以下简化代码,以便更好地了解我们正在使用这些 arrays 做什么:
// 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
)
Strictly spoken, Applicative is sufficient for traverse
- you don't need a monad.严格来说, Applicative足以traverse
- 你不需要 monad。
TaskEither<Error, string[]>
toTaskEither<Error, Either<Error, string>[]>
?TaskEither<Error, string[]>
到TaskEither<Error, Either<Error, string>[]>
?
const program1 = (input: string) =>
P.pipe(
toItems(input),
TE.map(A.map(toTitle))
);
TaskEither<Error, Either<Error, string[]>>
toTaskEither<Error, Option<string[]>>
?TaskEither<Error, Either<Error, string[]>>
到TaskEither<Error, Option<string[]>>
?
const program2 = (input: string) =>
P.pipe(
toItems(input),
TE.map(items => A.array.traverse(O.option)(items, clean))
);
The concrete chosen structure depends your environment and purpose.具体选择的结构取决于您的环境和目的。 ▶ Option
: emphasis on absence/presence; ▶ Option
:强调缺席/在场; ▶ Either
: permits a more concrete error type in Left
. ▶ Either
是:允许在Left
中使用更具体的错误类型。
Let's look at some programs and imagine, all use a web API with TaskEither
.让我们看一些程序并想象一下,它们都使用带有 TaskEither 的 web TaskEither
。
(input: string) => TE.TaskEither<Error, string[]>
程序 3:( (input: string) => TE.TaskEither<Error, string[]>
▶ will either fail completely with Error
or succeed with string[]
fetched data ▶ 将因Error
而完全失败或因string[]
获取数据而成功
(input: string) => TE.TaskEither<Error, E.Either<Error, string[]>>
程序 4:( (input: string) => TE.TaskEither<Error, E.Either<Error, string[]>>
▶ fetch
results in Error
or succeeds. ▶ fetch
Error
或成功的结果。 If succeeded, process web data further - resulting in Error
or string[]
如果成功,则进一步处理 web 数据 - 导致Error
或string[]
(input: string) => TE.TaskEither<Error, E.Either<Error, string>[]>
程序 5:( (input: string) => TE.TaskEither<Error, E.Either<Error, string>[]>
▶ same as Program 4, but post-processing of web data results in multiple Either
results - each can fail or succeed individually ▶ 与程序 4 相同,但 web 数据的后处理会产生多个Either
结果 - 每个都可以单独失败或成功
Here is implementation of program 4 as some kind of middle ground:这是作为某种中间立场的程序 4 的实现:
const program4 = ( input: string ): TE.TaskEither<Error, E.Either<Error, string[]>> => P.pipe( toItems(input), // TE.TaskEither<Error, string[]> TE.map(items => // TE.TaskEither<E, E.Either<Error, string[]>> A.array.traverse(E.either)( // E.Either<Error, string[]> items, F.flow( // E.Either<Error, string> toTitle, E.chain(s => E.fromOption(() => Error())(clean(s))) ) ) ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.