将fp-ts的Either转换为Effect的Either。

huangapple go评论72阅读模式
英文:

Converting an fp-ts Either to an Effect Either

问题

我已经使用fp-ts有一段时间了。最近,我在考虑将我的代码库的一些部分迁移到Effect来使用。我正在寻找这两者之间的桥梁。我遇到的第一个障碍之一是,这两个库使用的Either数据结构略有不同。有什么好的方法可以在它们之间进行转换?

import * as E from '@effect/data/Either'
import * as F from 'fp-ts/Either'

const fe: F.Either<string, number> = F.right(123)
// @ts-expect-error not assignable
const ee: E.Either<string, number> = fe
英文:

I have been using fp-ts for some time. Lately, I have been thinking about migrating some parts of my codebase to using Effect instead. I'm looking looking for bridges between the two. One of the first obstacles I have run into, is that the Either data structures used by two libraries differ slightly from each other. What would be a good way of converting between the two?

import * as E from &#39;@effect/data/Either&#39;
import * as F from &#39;fp-ts/Either&#39;

const fe: F.Either&lt;string, number&gt; = F.right(123)
// @ts-expect-error not assignable
const ee: E.Either&lt;string, number&gt; = fe

答案1

得分: 1

你似乎需要解构 fp-ts 的 Either 并重建 Effect 的 Either。

类似于这样:

pipe(
  fe,
  F.matchW(E.left, E.right)
)

这将把 "fp-ts Left" 转换为 "Effect Left",把 "fp-ts Right" 转换为 "Effect Right"。

英文:

Sounds like you would need to destructure the fp-ts Either and reconstruct the Effect Either.

Something like this:

pipe(
  fe,
  F.matchW(E.left, E.right)
)

This would convert an "fp-ts Left" -> "Effect Left" and an "fp-ts Right" -> "Effect Right"

huangapple
  • 本文由 发表于 2023年6月26日 19:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定