英文:
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 '@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
答案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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论