# Funland

Funland is a specification of common algebraic structures for JavaScript, TypeScript and Flow, based on Fantasy Land and compatible with static-land.

## Usage

For the TypeScript / Flow types, which are very light (no accompanying JavaScript code, just types):

    npm install --save funland

For the laws:

    npm install --save funland-laws

Usage of laws mostly makes sense in tests, being built for property-based testing, so usage of jsverify is recommended, but not required.

### Documentation

API docs are available:

- funland (core)
- funland-laws (laws)

### Modules: UMD and ES 2015

The library has been compiled using UMD (Universal Module Definition), so it should work with CommonJS and AMD, for standalone usage in browsers or Node.js.

But it also provides a `module` definition in `package.json`, thus providing compatibility with ECMAScript 2015 modules, for usage when used with a modern JS engine, or when bundling with a tool chain that understands ES2015 modules, like Rollup or Webpack.

## TypeScript or Flow?

Funland exposes both TypeScript and Flow type annotations out of the box.

## License

All code in this repository is licensed under the MIT license.

## Index

### Interfaces

- Applicative
- Apply
- Chain
- ChainRec
- Functor
- HK
- HK2
- HK3
- Monad
- Setoid

### Type aliases

- Constructor

## Type aliases

### Constructor

Constructor: object | object

- Defined in kinds.ts:55

Given a type `T` representing instances of a class `C`, the type `Constructor<T>` is the type of the class `C`.

This type emulates Class<T> from Flow.

Note that in TypeScript constructors can also be `protected` or `private` and unfortunately specifying `{ new(): T }` is thus insufficient. Which is why, for classes without a public constructor, we have to specify a `_funErasure` (static) member as a property, to help the compiler infer type `T`.

Example:

    class NumBox { constructor(public num: number) {} }
    class GenBox<A> { constructor(public a: A) {} }

    function getDefault<F>(ref: Constructor<F>): Option<F> {
      if ((ref as any)._default) return Some(ref._default)
      return None
    }

    (NumBox as any)._default = new NumBox(10)
    (GenBox as any)._default = new GenBox("value")

    const r1: Option<NumBox> = getDefault(NumBox)
    const r2: Option<GenBox<any>> = getDefault(GenBox)

And for classes with a private constructor:

    class PrivateBox<A> {
      private constructor(public a: A) {}

      static _funErasure: PrivateBox<any> // leaving undefined
    }

    const F = PrivateBox as any
    F._default = new F("hello")

    const r: Option<PrivateBox<any>> = getDefault(NumBox)

Generated using TypeDoc