feat: add Either type - #600
Open
ghoullier wants to merge 1 commit into
Open
Conversation
Implement Either<Left, Right> as a disjunction type that represents a value that is one of two possible types, without implying error semantics (unlike Result which implies success/failure). Either is Right-biased: map and flatMap operate on the Right value. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an
Either<Left, Right>type to the FP library.What is Either?
Either represents a disjunction — a value that is one of two possible types — without implying error semantics. This complements
Result, which specifically models success/failure.Use Either for general branching logic where both sides are equally valid (e.g.,
Either<string, number>for a value that could be a string or a number).Design
mapandflatMapoperate on the Right value, leaving Left unchangedContainerlike all other types in this librarymapLeftfor transforming the Left sidematch(onLeft, onRight)getOrElse(fallback)API
Either.Left(value)/Either.Right(value)— constructorsisLeft()/isRight()— type checksmap(fn)— transform RightmapLeft(fn)— transform LeftflatMap(fn)— chain on Rightmatch(onLeft, onRight)— pattern matchgetOrElse(fallback)— extract or defaulttoString()—Left(<value>)orRight(<value>)