The Either data type represents two exclusive values: an Either<R, L> can be a Right value or a Left value, where R is the type of the Right value, and L is the type of the Left value.
Understanding Either and Exit
Either is primarily used as a simple discriminated union and is not recommended as the main result type for operations requiring detailed error information.
Exit is the preferred result type within Effect for capturing comprehensive details about failures.
It encapsulates the outcomes of effectful computations, distinguishing between success and various failure modes, such as errors, defects and interruptions.
Creating Eithers
You can create an Either using the Either.right and Either.left constructors.
Use Either.right to create a Right value of type R.
Example (Creating a Right Value)
Use Either.left to create a Left value of type L.
Example (Creating a Left Value)
Guards
Use Either.isLeft and Either.isRight to check whether an Either is a Left or Right value.
Example (Using Guards to Check the Type of Either)
Pattern Matching
Use Either.match to handle both cases of an Either by specifying separate callbacks for Left and Right.
Example (Pattern Matching with Either)
Mapping
Mapping over the Right Value
Use Either.map to transform the Right value of an Either. The function you provide will only apply to the Right value, leaving any Left value unchanged.
Example (Transforming the Right Value)
Mapping over the Left Value
Use Either.mapLeft to transform the Left value of an Either. The provided function only applies to the Left value, leaving any Right value unchanged.
Example (Transforming the Left Value)
Mapping over Both Values
Use Either.mapBoth to transform both the Left and Right values of an Either. This function takes two separate transformation functions: one for the Left value and another for the Right value.
Example (Transforming Both Left and Right Values)
Interop with Effect
The Either type works as a subtype of the Effect type, allowing you to use it with functions from the Effect module. While these functions are built to handle Effect values, they can also manage Either values correctly.
How Either Maps to Effect
Either Variant
Mapped to Effect
Description
Left<L>
Effect<never, L>
Represents a failure
Right<R>
Effect<R>
Represents a success
Example (Combining Either with Effect)
Combining Two or More Eithers
zipWith
The Either.zipWith function lets you combine two Either values using a provided function. It creates a new Either that holds the combined value of both original Either values.
Example (Combining Two Eithers into an Object)
If either of the Either values is Left, the result will be Left, holding the first encountered Left value:
Example (Combining Eithers with a Left Value)
all
To combine multiple Either values without transforming their contents, you can use Either.all. This function returns an Either with a structure matching the input:
If you pass a tuple, the result will be a tuple of the same length.
If you pass a struct, the result will be a struct with the same keys.
If you pass an Iterable, the result will be an array.
Example (Combining Multiple Eithers into a Tuple and Struct)
If one or more Either values are Left, the first Left encountered is returned:
Example (Handling Multiple Left Values)
gen
Similar to Effect.gen, Either.gen provides a more readable, generator-based syntax for working with Either values, making code that involves Either easier to write and understand. This approach is similar to using async/await but tailored for Either.
Example (Using Either.gen to Create a Combined Value)
When any of the Either values in the sequence is Left, the generator immediately returns the Left value, skipping further operations:
Example (Handling a Left Value with Either.gen)
In this example, Either.gen halts execution as soon as it encounters the Left value, effectively propagating the error without performing further operations.
The use of console.log in these example is for demonstration purposes only. When using Either.gen, avoid including side effects in your generator functions, as Either should remain a pure data structure.