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

Usage

import * as fun from "https://raw.githubusercontent.com/baetheus/fun/main/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

ApplicableFnEither
BimappableFnEither

The canonical implementation of Bimappable for FnEither. It contains the methods bimap and mapSecond.

bind
bindTo
ComposableFnEither
FailableFnEither
FlatmappableFnEither

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

MappableFnEither
PremappableFnEither

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

tap
WrappableFnEither

§Functions

alt

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

apply

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

bimap

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

compose

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

dimap

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

fail
flatmap

Chain the right result of one FnEither into another FnEither.

fromEither

Turn an Either into a FnEither.

fromFn

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

fromPredicate

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.

getRightFlatmappable

Create a Flatmappable for FnEither where left values are combined using the supplied Combinable.

id

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.

idLeft

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.

join

Flatten nested FnEithers with the same input and left types.

left

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

map

Map over the right return value of a FnEither.

mapSecond

Map over the left return value of a FnEither.

premap

Map over the input value of a FnEither.

recover

Chain the left result of one FnEither into another FnEither.

right

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

tryCatch

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.

wrap

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

KindFnEither

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

KindRightFnEither

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

AnyFnEither

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

FnEither

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.