fp-ts 返回 Either[] 序列中的所有左值

aaa*_*aaa 5 fp-ts

我有一个字符串列表,string[]

我映射了一个返回的验证函数Either<Error, string>[]

我想要[Error[], string[]]所有验证错误和所有经过验证的字符串。

可以sequence(Either.Applicative)traverse(Either.Applicative)返回遇到的所有错误吗?我只收到Either<Error, string[]>,只是返回第一个错误。我是否需要编写自己的应用程序,带有合并左右半群的东西?

我可以通过更改为来获得所有map错误。reducefold

我还考虑反转验证,并运行两次。一个函数返回有效字符串,一个函数返回错误。

Den*_*ato 6

traverse返回 an Either,但您想要累加Lefts 和Rights。您可以map覆盖输入,然后分离元素。

import * as A from 'fp-ts/lib/Array';
import * as E from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/function';

declare const input: string[];
declare function validate(input: string): E.Either<Error, string>;

const result = pipe(input, A.map(validate), A.separate);
// result.left: Error[]
// result.right: string[]
Run Code Online (Sandbox Code Playgroud)