Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

Usage

import * as mod from "https://github.gh-proxy.cn/raw.githubusercontent.com/baetheus/fun/main/either.ts";

The Either module contains the Either algebraic data type, which represents two exclusive types. Either is commonly used to represent either a successful computation or a failed computation, with the result of the failed computation being kept in Left and successful results in Right.

Either provides a type-safe way to handle operations that can fail, allowing you to explicitly handle both success and error cases without throwing exceptions. This makes error handling more predictable and composable.

§Variables

ApplicableEither

The canonical implementation of Applicable for Either. It contains the methods wrap, apply, and map.

BimappableEither

The canonical implementation of Bimappable for Either. It contains the methods map and mapSecond.

bind

Bind a value from an Either to a name for use in subsequent computations. This is useful for chaining multiple operations that depend on previous results.

bindTo

Bind a value to a specific name in an Either computation. This is useful for creating named intermediate values.

FailableEither

The canonical implementation of Failable for Either. It contains the methods wrap, fail, map, flatmap, apply, alt, and recover.

FlatmappableEither

The canonical implementation of Flatmappable for Either. It contains the methods wrap, apply, map, and flatmap.

FoldableEither

The canonical implementation of Foldable for Either. It contains the method fold.

MappableEither

The canonical implementation of Mappable for Either. It contains the method map.

tap

Execute a side effect on the Right value of an Either and return the original Either unchanged.

TraversableEither

The canonical implementation of Traversable for Either. It contains the methods map, fold, and traverse.

WrappableEither

The canonical implementation of Wrappable for Either. It contains the method wrap.

§Functions

alt

Provide an alternative Either value if the first one is Left. If the first is Right, it's returned unchanged.

apply

Apply a function wrapped in an Either to a value wrapped in an Either. If either is Left, the result is Left. If both are Right, the function is applied to the value.

bimap

Apply functions to both sides of an Either. The first function is applied to Left values, the second to Right values.

fail

Construct a Left value from an error. This is an alias for left and is commonly used in contexts where you want to emphasize failure.

flatmap

Chain computations by applying a function that returns an Either to the Right value of an Either. Left values are unchanged.

flatmapFirst

Chain computations by applying a function that returns an Either to the Right value, but keep the original Right value if the function succeeds. This is useful for validation or side effects.

fold

Fold an Either into a single value by providing functions for both Left and Right cases, along with an initial value.

fromNullable

Convert a nullable value to an Either. If the value is null or undefined, it becomes a Left with the result of calling the error function. Otherwise, it becomes a Right with the non-null value.

fromPredicate

Create an Either from a predicate function. If the predicate returns true for the input value, the result is Right with the value. If false, the result is Left with the original value.

getCombinableEither

Create a Combinable instance for Either given Combinable instances for both Left and Right types. The combine operation combines Left values or Right values depending on which side the Either values are on.

getComparableEither

Create a Comparable instance for Either given Comparable instances for both Left and Right types. Left values are compared before Right values.

getFilterableEither

Create a Filterable instance for Either with a fixed left type. This allows filtering Right values and converting them to Left values when they don't meet certain criteria.

getFlatmappableRight

Create a Flatmappable instance for Either with a fixed left type. This is useful when you want to maintain a consistent error type throughout a computation chain.

getInitializableEither

Create an Initializable instance for Either given Initializable instances for both Left and Right types. The init value is a Right containing the initial value of the Right type.

getLeft

Extract the Left value as an Option. Returns Some(error) for Left values and None for Right values.

getOrElse

Extract the value from an Either, providing a default value for Left cases.

getRight

Extract the Right value as an Option. Returns Some(value) for Right values and None for Left values.

getShowableEither

Create a Showable instance for Either given Showable instances for both Left and Right types.

getSortableEither

Create a Sortable instance for Either given Sortable instances for both Left and Right types. Left values are sorted before Right values.

isLeft

Type guard that checks if an Either is a Left value.

isRight

Type guard that checks if an Either is a Right value.

left

Construct a Left value from an error or failure value.

map

Apply a function to the Right value of an Either. Left values are unchanged.

mapSecond

Apply a function to the Left value of an Either. Right values are unchanged.

match

Pattern match on an Either value. Provides a function for handling Left values and a function for handling Right values.

recover

Recover from a Left value by applying a function that returns an Either. Right values are unchanged.

right

Construct a Right value from a successful result.

swap

Swap the Left and Right values of an Either.

traverse

Traverse an Either with an Applicative. If the Either is Left, it's wrapped in the Applicative. If it's Right, the function is applied and the result is wrapped.

tryCatch

Execute a function that might throw an exception and convert the result to an Either. If the function throws, the error is caught and converted to a Left using the onError function. If successful, the result is wrapped in Right.

wrap

Construct a Right value from a value. This is an alias for right and is commonly used in contexts where you want to emphasize wrapping a value.

§Interfaces

KindEither

Specifies Either as a Higher Kinded Type, with covariant parameter R corresponding to the 0th index and covariant parameter L corresponding to the 1st index of any substitutions.

KindRightEither

Specifies Either as a Higher Kinded Type with a fixed left type B, with covariant parameter A corresponding to the 0th index of any substitutions.

§Type Aliases

Either

The Either type is a discriminated union of Left and Right, representing two exclusive possibilities. Either<L, R> can be either Left or Right.

Left

The Left type represents the left side of an Either, typically used to hold error values or failed computation results.

Right

The Right type represents the right side of an Either, typically used to hold successful computation results.