fp-ts Return 来自 Either[] 序列的所有左值
fp-ts Return all left values from an Either[] sequence
我有一个字符串列表,string[]
我映射了一个验证函数 returns Either<Error, string>[]
我想要 [Error[], string[]]
,所有验证错误和所有验证字符串。
能否遇到sequence(Either.Applicative)
或traverse(Either.Applicative)
return所有错误?我只收到 Either<Error, string[]>
,第一个错误 returned。我是否需要编写自己的 Applicative,带有合并左右的 Semigroup 的东西?
我可以通过使用 fold
将 map
更改为 reduce
来解决所有错误。
我还考虑过反转验证,运行 两次。一个函数 returns 有效字符串,一个 returns 错误。
traverse
returns 和 Either
,但您想同时累积 Left
和 Right
。您可以 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[]
我有一个字符串列表,string[]
我映射了一个验证函数 returns Either<Error, string>[]
我想要 [Error[], string[]]
,所有验证错误和所有验证字符串。
能否遇到sequence(Either.Applicative)
或traverse(Either.Applicative)
return所有错误?我只收到 Either<Error, string[]>
,第一个错误 returned。我是否需要编写自己的 Applicative,带有合并左右的 Semigroup 的东西?
我可以通过使用 fold
将 map
更改为 reduce
来解决所有错误。
我还考虑过反转验证,运行 两次。一个函数 returns 有效字符串,一个 returns 错误。
traverse
returns 和 Either
,但您想同时累积 Left
和 Right
。您可以 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[]