Skip to main content

Either<TLeft, TRight> (class)

Either represents values with two possibilities: TLeft value of type Either TLeft/TRight is either Left TLeft or Right TRight.

Status: stable

Factory Methods​

Either.left<T>(value)​

Creates a left Either instance with the left value filled.

Status: stable
Returns: Either<T, unknown>

Parameters​

NameTypeDescription
valueTThe value to fill the left slot

Example usage​

const { Either } = Melon.std.functional;

const myValue: Either<string, number> = condition
? Either.left("Hello") : Either.right(100);

Either.right<T>(value)​

Creates a right Either instance with the right value filled.

Status: stable
Returns: Either<unknown, T>

Parameters​

NameTypeDescription
valueTThe value to fill the left slot

Example usage​

const { Either } = Melon.std.functional;

const myValue: Either<string, number> = condition
? Either.left("Hello") : Either.right(100);

Instance Methods​

Either.fold<T>(actionLeft, actionRight)​

Create an a pattern-matching based operation chain to the Either values.

Status: stable
Returns: T

Parameters​

NameTypeDescription
actionLeftFunctionThe operation to be called in case of the left value not being null
actionRightFunctionThe operation to be called in case of the right value not being null

Example usage​

const { Either } = Melon.std.functional;

const myValue: Either<string, number> = condition
? Either.left("Hello") : Either.right(100);

const myDerivedValue = myValue.fold<string[]>(
left => left.split(),
right => right.toString().split()
);