Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
baetheus committed Sep 10, 2020
0 parents commit 684e3e5
Show file tree
Hide file tree
Showing 17 changed files with 958 additions and 0 deletions.
Empty file added .gitignore
Empty file.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"editor.formatOnSave": true
}
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License (MIT)

Copyright (c) 2018 Tom Crockett
Copyright (c) 2019 Brandon Blaylock

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.

Empty file added README.md
Empty file.
12 changes: 12 additions & 0 deletions either.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import * as E from "./either.ts";

// left
// right
// fold
// isLeft
// isRight
// Monad
// Foldable
// Traversable
62 changes: 62 additions & 0 deletions either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { _0, _1 } from "./hkts.ts";
import * as SL from "./type-classes.ts";

/***************************************************************************************************
* @section Types
**************************************************************************************************/

export type Left<L> = { tag: "Left"; left: L };
export type Right<R> = { tag: "Right"; right: R };
export type Either<L, R> = Left<L> | Right<R>;

/***************************************************************************************************
* @section Constructors
**************************************************************************************************/

export const left = <L>(left: L): Left<L> => ({ tag: "Left", left });
export const right = <R>(right: R): Right<R> => ({ tag: "Right", right });

/***************************************************************************************************
* @section Destructors
**************************************************************************************************/

export const fold = <L, R, B>(
onLeft: (left: L) => B,
onRight: (right: R) => B
) => (ma: Either<L, R>): B => {
switch (ma.tag) {
case "Left":
return onLeft(ma.left);
case "Right":
return onRight(ma.right);
}
};

/***************************************************************************************************
* @section Guards
**************************************************************************************************/

export const isLeft = <L, R>(m: Either<L, R>): m is Left<L> => m.tag === "Left";
export const isRight = <L, R>(m: Either<L, R>): m is Right<R> =>
m.tag === "Right";

/***************************************************************************************************
* @section Instances
**************************************************************************************************/

export const Monad = SL.createMonad2<Either<_0, _1>>({
of: (a) => right(a),
map: (fab, ta) => (isRight(ta) ? right(fab(ta.right)) : ta),
join: (tta) => (isRight(tta) ? tta.right : tta),
});

export const Foldable: SL.Foldable2<Either<_0, _1>> = {
reduce: (faba, a, tb) => (isRight(tb) ? faba(a, tb.right) : a),
};

export const Traversable: SL.Traversable2<Either<_0, _1>> = {
map: Monad.map,
reduce: Foldable.reduce,
traverse: (F, faub, ta) =>
isLeft(ta) ? F.of(left(ta.left)) : F.map(right, faub(ta.right)),
};
24 changes: 24 additions & 0 deletions fns.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import { compose, constant, flip, identity } from "./fns.ts";

const a = {};
const add = (a: number) => (b: number) => a + b;
const addOne = add(1);
const addTwo = add(2);

Deno.test("identity", () => {
assertEquals(identity(a), a);
});

Deno.test("flip", () => {
assertEquals(flip(add)(1)(2), flip(add)(2)(1));
});

Deno.test("compose", () => {
assertEquals(compose(addOne)(addTwo)(0), 3);
});

Deno.test("constant", () => {
assertEquals(constant(a)(), a);
});
27 changes: 27 additions & 0 deletions fns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/***************************************************************************************************
* @section Types
**************************************************************************************************/

export type Fn<AS extends unknown[], B> = (...as: AS) => B;

/***************************************************************************************************
* @section Functions
**************************************************************************************************/

export const curry2 = <A, B, C>(fn: (a: A, b: B) => C) => (a: A) => (b: B): C =>
fn(a, b);
export const curry3 = <A, B, C, D>(fn: (a: A, b: B, c: C) => D) => (a: A) => (
b: B
) => (c: C): D => fn(a, b, c);

export const identity = <A>(a: A): A => a;

export const flip = <A, B, C>(f: (a: A) => (b: B) => C) => (b: B) => (
a: A
): C => f(a)(b);

export const compose = <A, B>(fab: (a: A) => B) => <C>(fbc: (b: B) => C) => (
a: A
): C => fbc(fab(a));

export const constant = <A>(a: A) => () => a;
53 changes: 53 additions & 0 deletions hkts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/***************************************************************************************************
* @section Hole Types
* @description Marks a type hole to be filled by the substitution ($) type
**************************************************************************************************/

declare const index: unique symbol;

export interface _<N extends number = 0> {
[index]: N;
}
export type _0 = _<0>;
export type _1 = _<1>;
export type _2 = _<2>;
export type _3 = _<3>;
export type _4 = _<4>;
export type _5 = _<5>;
export type _6 = _<6>;
export type _7 = _<7>;
export type _8 = _<8>;
export type _9 = _<9>;

/***************************************************************************************************
* @section Fixed Type
* @description Fixes a type so it is not replaced by the substitution ($) type
**************************************************************************************************/

declare const fixed: unique symbol;

export interface Fixed<T> {
[fixed]: T;
}

/***************************************************************************************************
* @section Substitution Type
* @description Replaces any type holes in a type with the supplied parameters
* @example
* type FunctorFn<T> = <A, B>(fab: (a: A) => B, ta: $<T, [A]>) => $<T, [B]>;
* type ArrayInstance = FunctorFn<Array<_>>;
* // ArrayInstance = <A, B>(fab: (a: A) => B, ta: A[]): B[]
* type RecordInstance = FunctorFn<{ value: _ }>;
* // PromiseInstance = <A, B>(fab: (a: A) => B, ta: { value: A }): { value: B }
**************************************************************************************************/

// prettier-ignore
export type $<T, S extends any[]> = (
T extends Fixed<infer U> ? U :
T extends _<infer N> ? S[N] :
T extends any[] ? { [K in keyof T]: $<T[K], S> } :
T extends (...x: infer I) => infer O ? (...x: $<I, S>) => $<O, S> :
T extends object ? { [K in keyof T]: $<T[K], S> } :
T extends undefined | null | boolean | string | number ? T :
T
);
10 changes: 10 additions & 0 deletions option.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import * as O from "./option.ts";

const addOne = (n: number) => n + 1;
const someAddOne = O.some(addOne);
const someNumber = O.some(2);
const someOtherNumber = O.some(3);
const onSome = addOne;
const onNone = () => 100;
121 changes: 121 additions & 0 deletions option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { compose, identity } from "./fns.ts";
import { $, _ } from "./hkts.ts";
import * as SL from "./type-classes.ts";
import { pipe } from "./pipe.ts";

/***************************************************************************************************
* @section Types
**************************************************************************************************/

export type None = { tag: "None" };
export type Some<V> = { tag: "Some"; value: V };
export type Option<A> = None | Some<A>;

/***************************************************************************************************
* @section Constructors
**************************************************************************************************/

export const none: None = { tag: "None" };
export const some = <A>(value: A): Option<A> => ({ tag: "Some", value });
export const constNone = () => none;

/***************************************************************************************************
* @section Destructors
**************************************************************************************************/

export const getOrElse = <B>(onNone: () => B, ta: Option<B>): B =>
pipe(ta, fold(identity, onNone));

/***************************************************************************************************
* @section Guards
**************************************************************************************************/

export const isNone = <A>(m: Option<A>): m is None => m.tag === "None";
export const isSome = <A>(m: Option<A>): m is Some<A> => m.tag === "Some";

/***************************************************************************************************
* @section Instances
**************************************************************************************************/

/**
* Show
*/
export const getShow = <A>({ show }: SL.Show<A>): SL.Show<Option<A>> => ({
show: (ma) => (isNone(ma) ? "None" : `${"Some"}(${show(ma.value)})`),
});

/**
* Monad
*/
export const Monad = SL.createMonad<Option<_>>({
of: some,
map: (fab, ta) => pipe(ta, fold(compose(fab)(some), constNone)),
join: (tta) => (isNone(tta) ? tta : tta.value),
});

/**
* Apply
*/
export const Apply: SL.Apply<Option<_>> = {
ap: Monad.ap,
map: Monad.map,
};

/**
* Alternative
*/
export const Alternative: SL.Alternative<Option<_>> = {
of: Monad.of,
ap: Monad.ap,
map: Monad.map,
zero: constNone,
alt: (a, b) => (isSome(a) ? a : b),
};

/**
* Foldable
*/
export const Foldable: SL.Foldable<Option<_>> = {
reduce: <A, B>(faba: (a: A, b: B) => A, a: A, tb: Option<B>): A =>
isSome(tb) ? faba(a, tb.value) : a,
};

/**
* Traversable
*/
export const Traversable: SL.Traversable<Option<_>> = {
map: Monad.map,
reduce: Foldable.reduce,
traverse: <U, A, B>(
F: SL.Applicative<U>,
faub: (x: A) => $<U, [B]>,
ta: Option<A>
): $<U, [Option<B>]> =>
isNone(ta) ? F.of(none) : F.map(some, faub(ta.value)),
};

/***************************************************************************************************
* @section Pipeables
**************************************************************************************************/

export const fold = <A, B>(onSome: (a: A) => B, onNone: () => B) => (
ta: Option<A>
): B => {
switch (ta.tag) {
case "None":
return onNone();
case "Some":
return onSome(ta.value);
}
};

export const of = some;

export const map = <A, B>(fab: (a: A) => B) => (ta: Option<A>): Option<B> =>
Monad.map(fab, ta);

export const join = <A>(tta: Option<Option<A>>): Option<A> => Monad.join(tta);

export const chain = <A, B>(fatb: (a: A) => Option<B>) => (
ta: Option<A>
): Option<B> => Monad.chain(fatb, ta);
Loading

0 comments on commit 684e3e5

Please sign in to comment.