Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/fn_either.ts

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
import * as fun from "https://deno.land/x/fun@v.2.0.0-alpha.11/fn_either.ts";

FnEither is also known as ReaderEither. In essence a FnEither is a function that returns an either. This pattern can be used as a validation, a failable computation, a computation resulting in a "Choice", and many other things.

Variables

The canonical implementation of Alt for FnEither. It contains the methods alt and map

The canonical implementation of Bifunctor for FnEither. It contains the methods bimap and mapLeft.

The canonical implementation of Category for FnEither. It contains the methods of and compose.

The canonical implementation of Contravariant for FnEither. It contains the method contramap.

The canonical implementation of Monad for FnEither. It contains the methods of, ap, map, join, and chain.

The canonical implementation of Profunctor for FnEither. It contains the method dimap.

Functions

Provide an alternative FnEither in the event that an original FnEither returns Left.

Given a FnEither returning a function A => I and a FnEither returning a value A, combine them into a FnEither returning an I.

Map over the left and right return values of a FnEither.

Chain the right result of one FnEither into another FnEither.

Chain the left result of one FnEither into another FnEither.

Compose two FnEithers, passing the right value of the first into the second.

Map over the input value of a FnEither.

Map over the input of a FnEither contravariantly and the right result of a FnEither covariantly.

Turn an Either into a FnEither.

Lift a Fn<D, A> into FnEither<[D], never, A>

Create a FnEither from a Predicate or a Refinement. If the Predicate or Refinement returns true then the FnEither returns Right, otherwise it returns Left.

Create a Monad for FnEither where left values are combined using the supplied Semigroup.

Perform the same function as Reader ask. Given a type A (and optionally a type B), return a FnEither<[A], B, A>. This is useful for starting a FnEither chain.

Perform the same function as Reader askLeft. Given a type B (and optionally a type A), return a FnEither<[B], B, A>. This is useful for starting a FnEither chain with a left value.

Flatten nested FnEithers with the same input and left types.

Create a FnEither that always returns a Left(B).

Map over the right return value of a FnEither.

Map over the left return value of a FnEither.

An alias for right. Creates a FnEither from a value. The created FnEither does not require any arguments, but can widen when used in a chain.

Create a FnEither that always returns a Right(A).

Wrap any function in a try catch block. The returned function will lazily call and handle any throwing of the wrapped function. Non-throwing calls will be returned in a Right, and throwing calls will have their error and arguments passed to the onThrow function before being returned in a Left.

Interfaces

Specifies FnEither as a Higher Kinded Type, with covariant parameter A and B corresponding to the 0th and 1st indices of any Substitutions and a contravariant parameter D corresponding to the 0th index of any Substititions. The FnEither KindFnEither is unique in that it constrains the FnEither type to taking a single argument for the purposes of type substitution while the implementations of FnEither combinators such as map, chain, etc are mostly variadic (multiple arguments).

Specifies FnEither as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any Substitutions and a contravariant parameter D corresponding to the 0th index of any Substititions. KindRightFnEither curries the Left parameter of the output Either. This is useful when one needs to Fix the Left output with a Semigroup or some other collection algebraic structure.

Type Aliases

A FnEither type over any, useful for constraining generics that take or return FnEithers.

A FnEither, also known as ReaderEither, is a type over a variadic javascript function that returns an Either. ie. (a: number, b: string) => Either<Error, string> can be a FnEither. As an algebraic data type, the associated type class instances for Fn are limited to single variable inputs so they will look like (a: number) => Either<Error, string>, with only one argument. The purposes of a FnEither are many and varied, some common purposes are: failable computation, reading values from a shared environment, and sub-computations in a modified environment.