首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在fp中遍历/序列a Array<Either<E,A>>到Either<Array<E>,Array<A>>

在fp中遍历/序列a Array<Either<E,A>>到Either<Array<E>,Array<A>>
EN

Stack Overflow用户
提问于 2022-08-06 11:00:14
回答 1查看 161关注 0票数 0

我有一个实体列表,在这些实体中,即使一个验证失败也会产生错误。但是,我仍然希望迭代整个列表并收集所有的错误,以便进行进一步的记录。

具有默认应用程序的遍历/序列将产生Either<E, A[]> (仅是第一个遇到的错误),而不是需要的Either<E[], A[]>

库中是否存在实现这一目标的默认工具,或者是否可以通过编写自定义应用程序来实现?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-09 01:33:53

没有直接这样做的fp函数,但是您可以使用来自Either模块的Either

代码语言:javascript
复制
import * as E from 'fp-ts/lib/Either'
import * as RA from 'fp-ts/lib/ReadonlyArray'
import {pipe} from 'fp-ts/lib/function'

const collectErrors = <E, A>(
  xs: readonly E.Either<E, A>[]
): E.Either<readonly E[], readonly A[]> =>
  pipe(
    xs,
    RA.traverse(E.getApplicativeValidation(RA.getSemigroup<E>()))(
      E.mapLeft(RA.of)
    )
  )

// Left(['a', 'b'])
collectErrors([E.left('a'), E.right(1), E.left('b'))])
// Right([1, 2])
collectErrors([E.right(1), E.right(2)])
// Right([])
collectErrors([])

这是如何工作的:getApplicativeValidation接受一个Semigroup实例,并返回一个可以与traverse一起使用的Applicative实例。应用实例将使用半群实例组合错误。

代码语言:javascript
复制
RA.traverse(
  // The applicative instance for the either that will collect the errors 
  // into an array
  E.getApplicativeValidation(
    // The semigroup instance for readonly E[], which concatenates the arrays
    RA.getSemigroup<E>()
  )
)(
  // Turn each Either<E, A> into Either<readonly E[], A> by putting each error
  // inside an array so they can be concatenated
  E.mapLeft(RA.of)
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73259163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档