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

x/fun/fn.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.ts";

Variables

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.

Functions

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.

A common pattern in optics is to apply an input value to a function at the beginning and the end of a computation. This can (and has) been achieved by the composition of Pair using flow(P.dup, P.map(fn), P.merge). But for performance reasons it's nice to have a straighforward function that achieves the same result.

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.

Interfaces

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

Type Aliases

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