adt

Allows to create tagged ADT constructors of normal types and union types. Have a look at the Quickstart-Final tutorial

Source:

Methods

(inner) Type(type, vals) → {Type}

Allows to create tagged type constructors which can be used without "new"

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
type String

Name of the type

vals Array

Names of the values

Returns:
Type:
Type

Constructor function of the type

Example
const {Type} = require('futils').adt;

const Point = Type('Point', ['x', 'y']);

Point.prototype.move = function (x, y) {
    return Point(this.x + x, this.y + y);
}


const p = Point.of(100, 200);
p.move(50, 100).x; // -> 150
Point.is(p); // -> true

(inner) UnionType(type, defs) → {UnionType}

Allows to create tagged sum type constructors which can be used without "new"

Version:
  • 3.0.0
Source:
Parameters:
Name Type Description
type String

Name of the type

defs Object

Names and values of the sum types

Returns:
Type:
UnionType

Constructor functions of the sum type

Example
const {UnionType} = require('futils').adt;

const Shape = UnionType('Shape', {
    Rect: ['topLeft', 'bottomRight'],
    Circle: ['radius', 'center']
});

const {Rect, Circle} = Shape;

Shape.prototype.move = function (x, y) {
    return this.caseOf({
        Rect: (tl, br) => Rect(tl.move(x, y), br.move(x, y)),
        Circle: (r, c) => Circle(r, c.move(x, y))
    });
}


const rect = Rect.of(Point.of(100, 200), Point.of(200, 300));
rect.move(-50, -50);

const line = Circle.of(50, Point.of(200, 200));
line.move(-50, -50);