Functional
Type factory
The Type factory can be used to build complex data structure.
import { factorizeType } from "https://deno.land/x/functional/SumType.js"
const Coordinates = factorizeType("Coordinates", [ "x", "y" ]);
const vector = Coordinates(150, 200);
// vector.x === 150
// vector.y === 200
Type.from
from :: Type ~> Object -> t
Create an instance of Type using an object representation.
const vector = Coordinates.from({ x: 150, y: 200 });
// vector.x === 150
// vector.y === 200
Type.is
is :: Type ~> Type t -> Boolean
Assert that an instance is of the same Type.
Coordinates.is(vector);
// true
Type.toString
toString :: Type ~> () -> String
Serialize the Type Representation into a string.
Coordinates.toString();
// "Coordinates"
Type(a).toString
toString :: Type t => t ~> () -> String
Serialize the instance into a string.
vector.toString();
// "Coordinates(150, 200)"
Type Sum factory
import { factorizeSumType } from "https://deno.land/x/functional/SumType.js"
const Shape = factorizeSumType(
"Shape",
{
// Square :: (Coord, Coord) -> Shape
Square: [ "topLeft", "bottomRight" ],
// Circle :: (Coord, Number) -> Shape
Circle: [ "center", "radius" ]
}
);
SumType.from
from :: SumType ~> Object -> t
Create an instance of Type using an object representation.
const oval = Shape.Circle.from(
{
center: Coordinates.from({ x: 150, y: 200 }),
radius: 200
}
);
// oval.center === Coordinates(150, 200)
// oval.radius === 200
SumType.is
is :: SumType ~> SumType t -> Boolean
Assert that an instance is of the same Sum Type.
Shape.Circle.is(oval);
// true
SumType.fold
Shape.prototype.translate =
function (x, y, z) {
return this.fold({
Square: (topleft, bottomright) =>
Shape.Square(
topLeft.translate(x, y, z),
bottomRight.translate(x, y, z)
),
Circle: (centre, radius) =>
Shape.Circle(
centre.translate(x, y, z),
radius
)
})
};
SumType(a).toString
toString :: SumType t => t ~> () -> String
Serialize the instance into a string.
oval.toString();
// "Shape.Circle(Coordinates(150, 200), 200)"
Example of writing a binary tree with Sum Types
import { factorizeSumType } from "https://deno.land/x/functional/SumType.js"
const BinaryTree = factorizeSumType('BinaryTree', {
Node: ['left', 'x', 'right'],
Leaf: []
});
BinaryTree.prototype.reduce = function (f, accumulator) {
return this.fold(
{
Node: (l, x, r) => {
const left = l.reduce(f, accumulator);
const leftAndMiddle = f(left, x);
return r.reduce(f, leftAndMiddle);
},
Leaf: () => accumulator
}
);
};
const tree =
BinaryTree.Node(
BinaryTree.Node(
BinaryTree.Leaf,
1,
BinaryTree.Node(
BinaryTree.Leaf,
2,
BinaryTree.Leaf
)
),
3,
BinaryTree.Node(
BinaryTree.Node(
BinaryTree.Leaf,
4,
BinaryTree.Leaf
),
5,
BinaryTree.Leaf
)
);
// tree.reduce((x, y) => x + y, 0) === 15
Maybe
type
The Maybe
type represents potentially Just
a value or Nothing
.
import Maybe from "https://deno.land/x/functional/Maybe.js"
const container = Maybe.Just(42);
const serialize = (container) =>
container.fold({
Nothing: () => "There is no value.",
Just: value => `The value is ${value}.`
});
// serialize(container) === "The value is 42."
This implementation of Maybe is a valid Filterable
,
Functor
,
Applicative
,
Alternative
,
Traversable
and
Monad
.
Either
type
The Either
type represents the possibility of two values; either an a
or a b
.
import Either from "https://deno.land/x/functional/Either.js"
const container = Either.Right(42);
const serialize = (container) =>
container.fold({
Left: value => `An error occured: ${value}.`,
Right: value => `The value is ${value}.`
});
// serialize(container) === "The value is 42."
This implementation of Maybe is a valid Functor
,
Applicative
,
Alternative
and
Monad
.
TypeScript
I will try to publish TypeScript type hint files for those who needs it.
So far, I’ve only implemented the Type factory functions.
// @deno-types="https://deno.land/x/functional/SumType.d.ts"
import { factorizeType, factorizeSumType } from "https://deno.land/x/functional/SumType.js";
Deno
This codebase uses the assertion library from Deno.
MIT License
Copyright © 2020 - Sebastien Filion
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.