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.ts";

This file contains the Fn algebraic data type. Fn is short for a unary function, which is to say a function that only takes one input variable. Most computation can be encoded in Fn, but the standard ADT in most functional languages is called Reader.

§Variables

ApplicableFn

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

bind
bindTo
ComposableFn
FlatmappableFn

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

MappableFn

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

PremappableFn

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

tap
WrappableFn

§Functions

apply

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.

compose

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

constant

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

curry2
dimap

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

flatmap

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.

flow

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

handleThrow

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.

id

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

identity

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

map

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

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

over

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.

pipe

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.

premap

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

todo

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.

tryCatch
tryThunk

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.

unary

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

uncurry2
unsafeCoerce

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.

wrap

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

§Interfaces

KindFn

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, flatmap, etc are mostly variadic (multiple arguments).

§Type Aliases

AnyFn

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

Fn

A Fn, also known as Reader or Environment, is a type over a unary javascript function. ie. (a: number) => 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]>.