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.
Latest
import * as fun from "https://deno.land/x/fun@v2.0.0/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 Bimappable for FnEither. It contains the methods bimap and mapSecond.

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

The canonical implementation of Premappable for FnEither. It contains the method premap.

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.

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

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

Chain the right result of one FnEither into another FnEither.

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 Flatmappable for FnEither where left values are combined using the supplied Combinable.

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 flatmap.

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 flatmap 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.

Map over the input value of a FnEither.

Chain the left result of one FnEither into another FnEither.

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.

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 flatmap.

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, flatmap, 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 Combinable 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.