Skip to main content
Module

x/fun/mod.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@v2.0.0-alpha.6/mod.ts";

Namespaces

This file contains a collection of utilities and algebraic structure implemenuations for ReadonlyArray in JavaScript.

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.

Newtype presents a type level "rebranding" of an existing type.

An Optic is at heart two functions, view and modify. The view function is used to view some structure A that is contained in some structure S. The value that the view function tries to return is called its Focus, thus the name Optic. The Focus of the view function can be the value at a struct property, in an Option, or it can even reference all the values in a homogenous array. Thus the view function will return either 0, 1, or many of its Focus. Optics in this library are built to be composable. Let's look at some examples.

Semigroup is an algebra over associativity and totality. It's basic purpose can be understood as "a way to merge, concatenate, or combine" two of a thing into one thing.

ReadonlySet is a readonly product structure over objects and it operates on object equality for deduplication.

Variables

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

The canonical implementation of Applicative for ReadonlyArray. It contains the methods of, ap, and map.

The canonical implementation of Apply for ReadonlyArray. It contains the methods ap and map.

The canonical implementation of Chain for ReadonlyArray. It contains the methods of, map, and chain.

The canonical implementation of Filterable for ReadonlyArray. It contains the methods filter, filterMap, partition, and partitionMap.

The canonical implementation of Foldable for ReadonlyArray. It contains the method reduce.

The canonical implementation of Functor for ReadonlyArray. It contains the method map.

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

The canonical implementation of Traversable for ReadonlyArray. It contains the methods map, reduce, and traverse.

A function that always returns false.

A function that always returns true.

The canonical implementation of Eq for boolean. It contains the method equals.

The canonical implementation of Monoid for boolean that combines using the logical and operator. It contains the methods concat and empty(constTrue).

The canonical implementation of Monoid for boolean that combines using the logical or operator. It contains the methods concat and equal(constFalse).

The canonical implementation of Ord for boolean. It contains the method lt, lte, equals, gte, gt, min, max, clamp, between, and compare.

The canonical implementation of Semigroup for boolean that combines using the logical and operator. It contains the method concat.

The canonical implementation of Semigroup for boolean that combines using the logical or operator. It contains the method concat.

The canoncial implementation of Show for boolean. It uses JSON.stringify to turn a boolean into a string. It contains the method show.

A Eq that compares booleans using strict equality.

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

A Eq that compares number using strict equality.

The canonical implementation of Schemable for a Eq. It contains the methods unknown, string, number, boolean, literal, nullable, undefinable, record, array, tuple, struct, partial, intersect, union, and lazy.

A Eq that compares strings using strict equality.

A Eq that can compare any unknown values (and thus can compare any values). Underneath it uses strict equality for the comparison.

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.

The canonical implementation of Applicative for Fn. It contains the methods of, ap, and map.

The canonical implementation of Apply for Fn. It contains the methods ap and map.

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

The canonical implementation of Chain for Fn. It contains the methods ap, map, and chain.

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

The canonical implementation of Functor for Fn. It contains the method map.

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

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

An instance of Schemable for use with a Schema.

Construct an Optic over the values of a ReadonlyArray

Given an optic focused on an Pair<A, B>, construct an Optic focused on the First value of the Pair.

Given an optic focused on an Either<B, A>, construct an Optic focused on the Left value of the Either.

Wrap an Optic that focuses on a value that can be null or undefined such that it focuses only on non-null values

Construct an Optic over the values of a ReadonlyRecord

Given an optic focused on an Either<B, A>, construct an Optic focused on the Right value of the Either.

Given an optic focused on an Pair<A, B>, construct an Optic focused on the Second value of the Pair.

Construct an Optic over the values of a ReadonlySet

Given an optic focused on an Option, construct an Optic focused within the Option.

Construct an Optic over the values of a Tree

The cannonical implementation of the None type. Since all None values are equivalent there is no reason to construct more than one object instance.

The canonical Bifunctor instance for Pair. Contains the bimap and mapLeft methods.

The canonical Comonad instance for Pair. Contains the extract, extend, and map methods.

The canonical Extend instance for Pair. Contains the extend and map methods.

The canonical Foldable instance for Pair. Contains the reduce method.

The canonical Functor instance for Pair. Contains the map method.

The canonical Traversable instance for Pair. Contains the map, reduce, and traverse methods.

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

The canonical implementation of Applicative for Promise. It contains the methods of, ap, and map.

The canonical implementation of Apply for Promise. It contains the methods ap and map.

The canonical implementation of Chain for Promise. It contains the methods ap, map, and chain.

The canonical implementation of Functor for Promise. It contains the method map.

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

The canonical implementation of Applicative for ReadonlySet. It contains the methods of, ap, and map.

The canonical implementation of Apply for ReadonlySet. It contains the methods ap and map.

The canonical implementation of Applicative for ReadonlySet. It contains the methods ap, map, and chain

The canonical implementation of Filterable for ReadonlySet. It contains the methods filter, filterMap, partition, and partitionMap.

The canonical implementation of Foldable for ReadonlySet. It contains the method reduce.

The canonical implementation of Functor for ReadonlySet. It contains the method map.

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

The canonical implementation of Traversable for ReadonlySet. It contains the methods map, reduce, and traverse.

The canonical implementation of Eq for string. It contains the method equals.

The canonical implementation of Monoid for string. It contains the method empty and concat.

The canonical implementation of Ord for string. It contains the methods lt, lte, equals, gte, gt, min, max, clamp, between, and compare.

The canonical implementation of Semigroup for string. It contains the method concat.

The canonical implementation of Show for string. It contains the method show.

Functions

UNSAFE This operation mutates a standard Array by pushing onto it. This function is intended for internal use only and thus has no api guaruntees.

UNSAFE This operation creates a new array from ua with the value deleted at the given index. The deletiong index must be tested as in bounds before calling this function. This function is intended for internal use only and thus has no api guaruntees.

UNSAFE This operation creates a new array from ua with the value a inserted at the given index. The insertion index must be tested as in bounds before calling this function. This function is intended for internal use only and thus has no api guaruntees.

UNSAFE This operation mutates a standard Array by adding all elements from a second array to it. This function is intended for internal use only and thus has no api guaruntees.

UNSAFE This operation muuates a standard Array by unshifting onto it. This function is intended for internal use only and thus has no api guaruntees.

UNSAFE This operation mutates a standard Array by pushing onto it. This function is intended for internal use only and thus has no api guaruntees.

UNSAFE This operation creates a new array from ua with the value a changed at the given index. The insertion index must be tested as in bounds before calling this function. This function is intended for internal use only and thus has no api guaruntees.

Given two arrays first and second, if first is empty the return second, otherwise return first.

Given an array of functions ReadonlyArray<A -> I> and a ReadonlyArray apply every function in the function array to every value in the ReadonlyArray. This implementation loops first over the functions, and then over the values, so the order of results will be [fn1(val1), fn2(val1), fn3(val1), ..., fn1(val2), fn2(val2), ... fn1(valN), ... fnN(valN)].

Create a new array by appending an item to the end of an existing array.

Create a NonEmptyArray from a variadic number of arguments.

Given an Ord over A, create a binary search function for a sorted ReadonlyArray that returns the array index that the new value should be inserted at in order to maintain a sorted array.

Given a function A -> ReadonlyArray and a ReadonlyArray apply the function to every value in the array and combine all results, returning a ReadonlyArray.

Given two arrays first and second, join them into a new array effectively doing [...first, ...second].

Delete the value in an array at the given index. If the index is out of bounds then no change is made.

Create an empty array of type A (defaulting to never).

Given a Predicate or Refinement, apply the predicate or refinement to every value in an array, removing (and refining) the elements that the predicate or refinement return false for.

Filter and map over an ReadonlyArray in the same step. This function applies the predicate to each value in an array. If the predicate returns Some, then the inner I is added to the output array.

Given an instance Eq create a Eq<ReadonlyArray>.

Create an instance of Monoid<ReadonlyArray> given a type A. This instance uses array concat and empty as the instance methods for the Monoid.

Given an instance Ord create a Ord<ReadonlyArray>.

Create a Free semigroup over a type A using Array.concat.

Create an instance of Show for ReadonlyArray given an instance of Show for A.

Create a new array by inserting a value into an array at an index. If the index is out of range of the existing array then no change is made.

Create a new array by inserting a value into an array at an index. If the index is out of range of the existing array then no change is made.

This predicate over ReadonlyArray returns true when called with an empty array, otherwise it returns false.

A Refinement<ReadonlyArray, NonEmptyArray>, returning true if called with an array that has at least one item.

Given an index and a ReadonlyArray, return true if the index is valid for the given array. This tests whether index is between 0 and arr.length inclusive.

Given an array of arrays, flatten all inner arrays into a single external array.

Lookup the value in an array at the given index. If the index is out of bounds this function returns none.

Apply the function fai: (A, index) => I to every element in the array ua.

Create a new array by modifying a value of an array at an index. If the index is out of range of the existing array then no change is made.

Create a new array by modifying a value of an array at an index. If the index is out of range of the existing array then no change is made.

Create a NonEmptyArray conuaining the value A.

Given an Ord construct a curried insert function that inserts values into a new array in a sorted fashion. Internally this uses binarySearch to find the insertion index of any inserted items. Since the returned function will always insert this function will always return a new array.

Partition a ReadonlyArray into two ReadonlyArrays using a predicate or refinement to do the sorting. If the predicate or refinement returns true for a value, the value is pushed into the first array in a Pair, otherwise it is pushed into the second array in a pair.

Partition and map over a ReadonlyArray in the same loop. Given a predicate A => Either<J, I>, this function passes each element in an array into the predicate. If the predicate returns Right then the inner I is pushed into the first array in a pair. If the predicate returns Left then the inner J is pushed into the second array in a pair.

Create a new array by prepending an item to the head of an existing array.

Create a range of numbers with count values, starting at start (default 0) and stepping by step (default 1).

Reduce an array from left to right, accumulating into a type O via the function foao: (O, A, index) => O and an initial value O.

Sequence over an array of type V, inverting the relationship between V and ReadonlyArray. This function also keeps the indexed types of in each V at covariant position 0. In other words sequence over [Option, Option] becomes Option<[number, string]>.

Returns a new array conuaining elements of as sorted in ascending order according to the sort order defined by O.

Traverse a ReadonlyArray using an Applicative over V and a mapping function A => V.

Create a new array by replacing a value of an array at an index. If the index is out of range of the existing array then no change is made.

Create a new array by replacing a value of an array at an index. If the index is out of range of the existing array then no change is made.

Provide an alternative for a failed computation. Useful for implementing defaults.

Apply an argument to a function under the Right side.

Sequentially apply arguments

A dual map function that maps over both Left and Right side of a AsyncEither.

Chain AsyncEither based computations together in a pipeline

Chain AsyncEither based failures, Left sides, useful for recovering from error conditions.

Fold away the inner Either from the AsyncEither leaving us with the result of our computation in the form of a Async

Lift an always succeeding async computation (Async) into a AsyncEither

Lifts an Either<B,A> into a AsyncEither<B,A>

Flatten a AsyncEither wrapped in a AsyncEither

Constructs a AsyncEither from a value and wraps it in an inner Left. Traditionally signaling a failure

Map a function over the Right side of a AsyncEither

Map a function over the Left side of a AsyncEither

Pointed constructor of(A) => AsyncEither<B,A>

Constructs a AsyncEither from a value and wraps it in an inner Right. Traditionally signaling a successful computation

Pointed constructor throwError(B) => AsyncEither<B, never>

Wraps a Async of A in a try-catch block which upon failure returns B instead. Upon success returns a Right and Left for a failure.

A curried form of logical And.

Compares two booleans, returning an Ordering. True is greater than False in this ordering, generally, but the specifics are always decided by the runtime.

Test the equality of two booleans.

Create a match function over boolean

Negate a given boolean.

A curried form of logical Or.

Creates a Eq that compares readonly array with items that have the type compared in the supplied eq.

Create a Eq using a Eq and a function that takes a type L and returns a type D.

Create a Eq from an uncurried function that checks equality.

Create a eq from two other eqs. The resultant eq checks that any two values are equal according to both supplied eqs.

Create a eq that tests the output of a thunk (IO). This assumes that the output of the thunk is always the same, which for true IO is not the case. This assumes that the context for the function is undefined, which means that it doesn't rely on "this" to execute.

Create a eq that evaluates lazily. This is useful for equality of recursive types (either mutual or otherwise).

Creates a Eq that compares a union of literals using strict equality.

Create a eq from a method on a class or prototypical object. This exists because many objects in javascript do now allow you to pass an object method around on its own without its parent object. For example, if you pass Date.valueOf (type () => number) into another function and call it, the call will fail because valueOf does not carry the reference of its parent object around.

Creates a derivative Eq that can also compare null values in addition to the source eq.

Create a eq that compares, key for key, structs according to the structure of the eqs passed into struct. It allows the values in the struct to be optional or null.

Create a Eq that casts the inner type of another Eq to Readonly.

Creates a Eq that compares readonly records with items that have the type compared in the supplied eq.

Create a eq that compares, key for key, structs according to the structure of the eqs passed into struct.

Creates a eq that compares, index for index, tuples according to the order and eqs passed into tuple.

Creates a derivative Eq that can also compare undefined values in addition to the source eq.

Create a eq from two other eqs. The resultant eq checks that any two values are equal according to at least one of the supplied eqs.

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.

Given L => A => I and D => A create a new Fn D & L => I. In order to preserve type widening for ap, it only handles unary functions.

Apply functions to an unknown function. Useful for pipeing values into functions.

Create a new Fn by combining A => L => I with D => A to produce D & L => I. This is equivalent to ap with the first two arguments switched. It is also limited to unary functions in order to properly handle type widening on the input type.

Compose two functions by taking the output of one and passing it to another. This is equivalent to the map function.

Create a Fn that always returns a value. This is equivalent to of but without the ability to specify a contravariant argument.

Map over the input of a function, turning D => A and L => D into L => A.

A combination of contramap and map, dimap applies fld to the input of a function and fai to the output.

The flow function is like the pipe function without the initial value. It composes up to 9 functions from left to right (top to bottom). The first function can take multiple arguments but every subsequent function must be unary (only take one argument).

Wrap any function in a try catch block, passing the result and arguments to onResult when the function does not throw as well as passing the error and arguments to the onThrow function when it does. Neither the onResult nor the onThrow functions should ever throw an error themselves. None of the functions in the fun library will throw on their own. If they do it is a bug in fun or the underlying runtime (or you used fun in javascript). This function primarily exists to wrap functions exported from other libraries that may use exceptions as their error mechanism. This makes those functions safe to use with fun.

A thunk over the identity function. It allows one to constrain an identity to a specific type.

The canonical identity function. It returns whatever value was passed to it.

Collapse a curried function D => D => A into D => A.

Map over the output of a Fn. This is equivalent to function composition. ie. a => pipe(f, map(g))(a) === a => g(f(a))

Memoize a unary function using a Map. This means that this algorithm puposefully leaks memory.

Create a Fn that always returns a value. This is equivalent to constant.

The pipe takes a value as the first argument and composes it with subsequent function arguments, returning the result of the last function passed in. It handles and correctly types up to 10 unary functions. Beyond 10 it makes sense to break up pipe into multiple pipes.

A function that can be called to output any type. It's used for type hole based programming. This allows one to define interfaces and types for a function and stub them with todo() until you are ready to implement the actual behavior. The todo function will throw if it is ever actually called.

Wrap a thunk (a Fn that takes no arguments) in a try catch block, using an onThrow function (that should itself never throw) to handle a default case should the original function throw. This is useful for wrapping functions that might throw.

Take a variadic Fn and make it unary, collapsing multiple arguments into a single tuple argument.

Does an unsafe type coercion on any type. This is only safe when the types A and I have referential transparency. This is to say when the type A can be substituted for I and I for A at runtime without there being any change to the operation of the program. The primary use case for unsafeCoerce is in Newtype implementations.

Creates a JsonBuilder of an Array with values that match the supplied JsonBuilder

Creates a JsonBuilder of an boolean type

Creates a JsonBuilder of an date type

Creates a JsonBuilder that intersects two JsonBuilders

Creates a lazy JsonBuilder, which is useful for creating recursive JSON Schemas, mutual or otherwise. A limitation of typescript means that the type must be defined outside of the JsonBuilder and manually annotated

Creates a JsonBuilder over a tuple of literals

Creates a JsonBuilder that makes the given builder nullable

Creates a JsonBuilder of an number type

Creates a JsonBuilder of a partial with values and keys that match the supplied JsonBuilders

Collapse a JsonBuilder into a JsonSchema that can be logged or used as output for a webserver or a file.

Creates a JsonBuilder of a Record with string keys and values that match the supplied JsonBuilder

Creates a JsonBuilder of an string type

Creates a JsonBuilder of a struct with values and keys that match the supplied JsonBuilders

Creates a JsonBuilder of a tuple with values that match the supplied JsonBuilders

Creates a JsonBuilder that makes the given builder undefinable

Creates a JsonBuilder that unions two JsonBuilders

Creates a JsonBuilder of an unknown type

Retype an existing Eq from an inner type to a Newtype.

Retype an existing Monoid from an inner type to a Newtype.

Retype an existing Ord from an inner type to a Newtype.

Retype an existing Semigroup from an inner type to a Newtype.

Construct a Affine from get and modify functions.

Given an optic over a record, focus on an Option(value) at the given key, allowing one to delete the key by modifying with a None value.

Construct an Optic over a ReadonlyMap that can lookup a value by key.

Compose two Optics by:

Collect all values focused on by an Optic into an Array, convert them into a type I, and concat them using the passed Monoid.

Given an Optic focused on A, filter out or refine that A.

Construct a Fold from get and modify functions.

Construct a Getter<S, A> from an Iso<S, A>;

Construct a Affine from a Predicate or a Refinement.

Construct a Getter from get and modify functions.

The starting place for most Optics. Create an Optic over the identity function.

Invariant map over the focus of an existing Optic.

Given an optic over an array, focus on a value at an index in the array.

Given an optic over a record, focus on a value at a key in that record.

Extract the modify function from an Optic

Given an Optic over a structure with a property P, construct a new Optic at that property P.

Given an Optic over a structure with properties P, construct a new optic that only focuses on those properties

Construct a replace function for a given Optic

Traverse a U using a Traversable for U. By construction get for a Fold will return an array of values.

Extract the view function from an Optic

constNone is a thunk that returns the canonical none instance.

fold is the standard catamorphism on an Option. It operates like a switch case operator over the two potential cases for an Option type. One supplies functions for handling the Some case and the None case with matching return types and fold calls the correct function for the given option.

fromNullable takes a potentially null or undefined value and maps null or undefined to None and non-null and non-undefined values to Some<NonNullable>

fromPredicate will test the value a with the predicate. If the predicate evaluates to false then the function will return a None, otherwise the value wrapped in Some

Generates a Eq module for an option with inner type of A.

getOrElse operates like a simplified fold. One supplies a thunk that returns a default inner value of the Option for the cases where the option is None.

Generates a Show module for an option with inner type of A.

Tests wether an Option is None. Can be used as a predicate.

Tests wether an Option is Some. Can be used as a predicate.

mapNullable is useful for piping an option's values through functions that may return null or undefined.

The some constructer takes any value and wraps it in the Some type.

toNullable returns either null or the inner value of an Option. This is useful for interacting with code that handles null but has no concept of the Option type.

toUndefined returns either undefined or the inner value of an Option. This is useful for interacting with code that handles undefined but has no concept of the Option type.

tryCatch takes a thunk that can potentially throw and wraps it in a try/catch statement. If the thunk throws then tryCatch returns None, otherwise it returns the result of the thunk wrapped in a Some.

Creates a new pair by mapping first through the fai function and second through the fbj function.

Creates a pair from a single type

Creates a new pair by constructing a first value from the whole pair and keeping the second value from the original pair. Can be used somewhat like a superpowered Reader.

Just like the first function, extract returns the first value in a pair.

A curried form of the pair constructor, starting with the first value of a pair.

Extracts the first value from a Pair.

Creates a Monad instance for Pair where the second parameter is concatenated according to the Monoid instance passed in.

Extracts the second value from a Pair.

Creates a Show instance for a pair, wrapping the Show instances provided for the first and second values.

Creates a new Pair with the same second value and a new first value determined by the output of the fai function.

Creates a new Pair with the same first value and a new second value determined by the output of the fbj function.

Apply a function in the first position of a pair to a value in the second position of a pair.

Apply a function in the first position of a pair to a value in the second position of a pair.

Creates a Pair from two values first and second with types A and B respectively. Used to quickly construct a Pair.

Reduces a pair with an initial value, also passing the second value into the reducer as well.

A curried form of the pair constructor, starting with the second value of a pair.

Creates a new Pair with the first and second values swapped.

Traverse a pair using another algebraic structure's Applicative.

Creates the intersection of two predicates, returning true if both predicates return true.

Create a Predicate using a Predicate and a function that takes a type L and returns a type D. This maps over the input value of the predicate.

Get a Monoid<Predicate> for any type A that concats using the Predicate and function and defaults to true.

Get a Monoid<Predicate> for any type A that concats using the Predicate or function and defaults to false.

Get a Semigroup<Predicate> for any type A that concats using the Predicate and function.

Get a Semigroup<Predicate> for any type A that concats using the Predicate or function.

Negates the result of an existing Predicate.

Creates the union of two predicates, returning true if either predicate returns true.

Make an existing Promise somewhat abortable. While the returned promise does resolve when the abort signal occurs, the existing promise continues running in the background. For this reason it is important to catch any errors associated with the original promise.

An alias for Promise.all

Create a new Promise from a Promise<(a: A) => I> and a Promise. Although Promises encapsulate asynchrony, there is no way defer a Promise once created, thus this ap function always evaluates both input Promises in parallel.

An alias for Promise.catch

Create a new Promise by chaining over the result of an existing Promise. This is effectively Promise.then.

Create a Deferred from a type.

Delay the resolution of an existing Promise. This does not affect the original promise directly, it only waits for a ms milliseconds before chaining into the original promise.

This function theoretically flattens A Promise<Promise>. However, there is a need for this function in Monad. Since there is no way to actually create the input of this function, there is no example.

Create a new Promise by mapping over the result of an existing Promise. This is effectively Promise.then, but narrowed to non-promise returning functions. If the mapping function returns a Promise then the type for this function will be incorrect, as there is no way to create a Promise<Promise>.

Create a Promise from a value A or another PromiseLike. This is essentially an alias of Promise.resolve.

An alias for Promise.race. Note that Promise.race leaks async operations in most runtimes. This means that the slower promise does not stop when the faster promise resolves/rejects. In effect Promise.race does not handle cancellation.

An alias for Promise.reject.

An alias for Promise.resolve.

An alias for Promise.then

Wrap a function that potentially throws in a try/catch block, handling any thrown errors and returning the result inside of a Promise.

Create a Promise that resolve after ms milliseconds.

A helper function to build a generic Schema that can be used with any Schemable.

Given a semigroup, create a function that will iterate through an array of values and concat them. This is not much more than Array.reduce(concat).

Create a semigroup that always returns the given value, ignoring anything that it is concatenated with.

Get the "Dual" of an existing Semigroup. This effectively reverses the order of the input semigroup's application. For example, the dual of the "first" semigroup is the "last" semigroup. The dual of (boolean, ||) is itself.

Get an Semigroup over A that always returns the first parameter supplied to concat (confusingly this is actually the last parameter since concat is in curried form).

Create a semigroup that works like Array.join, inserting middle between every two values that are concatenated. This can have some interesting results.

Get an Semigroup over A that always returns the last parameter supplied to concat (confusingly this is actually the first parameter since concat is in curried form).

Create a semigroup fron an instance of Ord that returns that maximum for the type being ordered. This Semigroup functions identically to max from Ord.

Create a semigroup fron an instance of Ord that returns that minimum for the type being ordered. This Semigroup functions identically to min from Ord.

Get a Semigroup from a struct of semigroups. The resulting semigroup will operate over similar shaped structs applying the input semigroups applying each based on its position,

Get a Semigroup from a tuple of semigroups. The resulting semigroup will operate over tuples applying the input semigroups applying each based on its position,

Given a ReadonlySet of functions A -> I and a ReadonlySet return a ReadonlySet by applying every function to every value A.

Given a function A -> ReadonlySet and a ReadonlySet return a ReadonlySet created by applying the function to every value A and joining all the resulting ReadonlySets.

Given an instance of Eq create a function that will take a ReadonlySet and return a new ReadonlySet where any members that are equal are deduplicated.

Copies an existing ReadonlySet into a new ReadonlySet, keeping references to the original members.

Given an insuance of Eq create a function that uakes a value A and returns a predicate over ReadonlySet the returns true if there are any members of the set that are equal to the value.

Given an instance of Eq create a function that uakes a ReadonlySet and returns a predicate over a value A the returns true if the value is a member of the set. This is like elem but with the set and value parameters swapped.

Constructs a new ReadonlySet over type A that conuains no values.

Operates like Array.evert, testing values in a ReadonlySet with a Predicate until either the predicate returns false for a value or all of the values have been tested as true. Shortcircuits on the first value that returns false. This is the dual of some.

Given a Refinement or Predicate over A and a ReadonlySet return a new ReadonlySet with only values for which the predicate or refinement return true.

Given a function A -> Option and a ReadonlySet return a ReadonlySet by applying the function to all values A. Any Nones will not enter the resultant set while Some values will. This is effectively filtering and mapping simultaneously.

Given an instance of Eq return Eq<ReadonlySet>.

Given an instance of Show return an instance of Show<ReadonlySet>.

Given an instance of Eq create a Monoid<ReadonlySet> where concat creates a union of two ReadonlySets.

Given an instance of Eq return a function that takes two ReadonlySets and returns a new set with only the elements that exist in both sets.

Given an instance of Eq return a function second => first => boolean that returns true when every member of first is in second.

Given a ReadonlySet of ReadonlySet, flatten all of the inner sets and return a ReadonlySet.

Given a function A -> I and a ReadonlySet return a new ReadonlySet where the values were created by passing each A through the A -> I function.

Given a value A create a new ReadonlySet that contains that value.

Given a Predicate or Refinement over A and a ReadonlySet return a Pair with a first value being a ReadonlySet of values that return true when applied to the refinement or predicate and a second value being a ReadonlySet of values that return false when applied to the predicate.

Given a function A -> Either<J, I> and a ReadonlySet return a Pair(ReadonlySet, ReadonlySet) by applying every value A in the set to the partitioning function.

Reduce a ReadonlySet to a value O by iterating over the values of the set and collecting them with the reducing function.

Constructs a new ReadonlySet from an arbitrary number of values.

Operates like Array.some, testing values in a ReadonlySet with a Predicate until either the predicate returns true for a value or all of the values have been tested. Shortcircuits on the first value that returns true. This is the dual of every.

Traverse a ReadonlySet value by value, applying a function A -> V, then collecting all of the I values into ReadonlySet and returning V<ReadonlySet>. In concrete terms this can take ReadonlySet<Option> and turn it into Option<ReadonlySet> and other ADT inversions.

Given an instance of Eq return a function that takes two ReadonlySets and merges them into a new ReadonlySet that contains all the elements from both sets.

Compare two strings and return an Ordering (-1, 0, or 1);

Combine two strings.

Returns an empty string.

Creates a Predicate over string that returns true when the string ends with the supplied searchString starting at position, if it is supplied.

Compare two strings for equality.

Creates a Predicate over string that returns true when a string includes the searchString at or beyond the optional starting position.

A Predicate for string returning true if the string is empty.

A instance of Refinement<unknown, string>. Used as a type guard to verify any type is actually a string.

Returns the length of an input string.

Create a function that returns an Option. This is a curried form of RegExp.match.

A simple curried pluralizing function. Takes the singular and plural forms of a word and returns a function that takes a count and returns the correct word for the count.

Create a function that replaces all values in a string according to a RegExp and a replacement value.

Create a substring of a string based on 0 based indices.

Split a string into an array of strings at a character or RegExp match.

Creates a Predicate over string that returns true when the string starts with the supplied searchString starting at position, if it is supplied.

Create a Predicate that returns true when it matches the supplied RegExp.

A pipeable form of String.toLowerCase.

A pipeable form of String.toUpperCase.

Trims whitespace from the beginning and end of a string.

Trims whitespace from the end of a string.

Trims whitespace from the beginning of a string.

Interfaces

An instance of Alt extends Functor and provides a new method called alt. This method takes two matching Kinds, left and right, and returns one of them. A good way to think of this is as a boolean or, where if the left kind is "false" then the right kind is returned.

Specifies ReadonlyArray as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions.

A Eq is an algebra with a notion of equality. Specifically, a Eq for a type T has an equal method that determines if the two objects are the same. Eqs can be combined, like many algebraic structures. The combinators for Eq in fun can be found in eq.ts.

Specifies Eq as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any Substitutions.

Specifies Eq as a Higher Kinded Type, with contravariant parameter D corresponding to the 0th index of any Substitutions.

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

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 URI 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 Fn 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. The Fn URI is unique in that it constrains the Fn type to taking a single argument for the purposes of type substitution while the implementations of Fn combinators such as map, chain, etc are mostly variadic (multiple arguments).

The Kind substitution scheme for JsonBuilder.

A Kind implementation used to fix the second parameter in a Pair. Otherwise it operates the same as Pair does.

Specifies Pair as a Higher Kinded Type, with covariant parameters A and B corresponding to the 0th and 1st index of any Substitutions.

Specifies Predicate as a Higher Kinded Type, with contravariant parameter A corresponding to the 0th index of any Substitutions.

Specifies Promise as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any Substitutions.

A Semigroup is an algebra with a notion of concatenation. This means that it's used to merge two Ts into one T. The only rule that this merging must follow is that if you merge A, B, and C, that it doesn't matter if you start by merging A and B or start by merging B and C. There are many ways to merge values that follow these rules. A simple example is addition for numbers. It doesn't matter if you add (A + B) + C or if you add A + (B + C). The resulting sum will be the same. Thus, (number, +) can be used to make a Semigroup (see SemigroupNumberSum for this exact instance).

Specifies ReadonlySet as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions.

Show is the "fun" version of JavaScripts toString method. For algebraic data types that can be stringified it allows a structured way to do so.

Type Aliases

This type alias represents a ReadonlyArray conuaining at least one value at the head.

This type alias extracts the inner type of a ReadonlyArray.

The AsyncEither type can best be thought of as an asynchronous function that returns an Either. ie. async () => Promise<Either<B, A>>. This forms the basis of most Promise based asynchronous communication in TypeScript.

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.

A Fn type over any, useful for constraining generics that take or return Fns.

A Fn, also known as Reader or Environment, is a type over a variadic javascript function. ie. (a: number, b: string) => string can be a Fn. 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) => string, with only one argument. The purposes of a Fn are many and varied, some common purposes are: computation, reading values from a shared environment, and sub-computations in a modified environment. In many ways Fn is a more powerful abstraction than State, and indeed the State monad in fun is exactly State<S, A> = Fn<[S], [A, S]>.

JsonBuilder is a wrapper over State, used to construct a JsonSchema combinator by combinator. In this way it can also be used as a Schemable to create a JsonSchema that matches a Decoder.

Represents a recursive JSON Schema

Represents an intersection of Schemas in JSON Schema

Represents a union of Schemas in JSON Schema

Represents an array or tuple in JSON Schema

Represents boolean in JSON Schema

Represents string Date in JSON Schema

Represents a collection of Schemas that can be referenced by a $ref in JSON Schema

Represents null in JSON Schema

Represents null in JSON Schema

Represents number(float or integer) in JSON Schema

Represents a struct in JSON Schema

Represents an exclusive union of Schemas in JSON Schema

Represents a reference to another Schema in JSON Schema, generally useful for recursion or brevity.

Represents string in JSON Schema

Represents an unknown JSON Schema

Given a JsonBuilder this type will extract T for use in type level programming.

A type alias for Newtype<any, any> that is useful when constructing Newtype related runtime instances.

Extracts the inner type value from a Newtype.

Create a branded type from an existing type. The branded type can be used anywhere the existing type can, but the existing type cannot be used where the branded one can.

We recover the Affine type from the generic Optic

We recover the Fold type from the generic Optic

We recover the Getter type from the generic Optic

Our new Optic definition. Instead of get and set we use get and modify as set can be derived from modify(() => value). This drastically simplifies implementation.

The None type represents the non-existence of a value.

The Option represents a type A that may or may not exist. It's the functional progamming equivalent of A | undefined | null.

The Some type represents the existence of a value.

Pair represents a pair of values. This is equivalent to a Tuple of length two, the Separated type in fp-ts, and any other type that contains exactly two covariant other types.

The Predicate type is a function that takes some value of type A and returns boolean, indicating that a property is true or false for the value A.

A type for Promise over any, useful as an extension target for functions that take any Promise and do not need to extract the type.

A Promise with the inner resolve function hoisted and attached to itself.

A type level extractor, used to pull the inner type from a Promise.

Takes a Schemable and returns a Schemable<ReadonlyArray>

Wraps a boolean type in Schemable.

Takes two schemables, left and right, and returns the intersection of them. This means that any value for must match both schemables.

Takes an id and a thunk returning a schemable and returns a schemable that matches the return value of the thunk. This schemable is necessary for handling recursive or corecursive schemables.

These are the super-types that a Literal schema must extent. They are used to constrain the inputs for LiteralSchemable.

Wraps a union of literals in Schemable.

Takes a Schemable and returns a Schemable<A | null>;

Wraps a number type in Schemable.

Takes a struct of Schemables and returns a Schemable for a struct that matches, key for key, the input schemables but the values can also be partial.

Takes a Schemable and returns a Schemable<ReadonlyRecord>

A Schema is the a function that takes a generic schemable and builds a specific model from it.

A Schemable is the union of all schemable methods. This allows one to build an arbitrary Schema using the Schemable interface, then pass a concrete Schemable implementation to the Schema. Thus, one can build a single model and produce decoders, guards, or jsonschema from that model.

Wraps a string type in Schemable.

Takes a struct of Schemables and returns a Schemable for a struct that matches, key for key, the input schemables.

Takes a tuple of Schemables and returns a Schemable for a tuple that matches, index for index, the input schemables.

Extracts the inner type of a Schema

Takes a Schemable and returns a Schemable<A | undefined>;

Takes two schemables, left and right, and returns the union of them. This means that any value for must match either schemable.

Wraps an unknown type in Schemable. This is the best escape hatch when a Schema isn't known ahead of time.

A type for Semigroup over any, useful as an extension target for functions that take any Semigroup and do not need to extract the type.

A type level extractor, used to pull the inner type from a Semigroup.

Extract the inner type of a ReadonlySet