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

Commit

Permalink
feat: fill out static-land modules for array
Browse files Browse the repository at this point in the history
Added more static land modules and module getters for array.ts.
Implemmented most of the module and module getter tests for
array.ts as well. Still need to tests for pipeables and
sequenceTuple/sequenceArray.
  • Loading branch information
baetheus committed Oct 28, 2020
1 parent 67fd567 commit ea5f3f4
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 19 deletions.
75 changes: 58 additions & 17 deletions array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const _reduce = <A, B>(
return out;
};

const _concat = <A>(
export const _concat = <A>(
a: readonly A[],
b: readonly A[],
): readonly A[] => {
Expand Down Expand Up @@ -101,21 +101,13 @@ export const zero: ReadonlyArray<never> = [];
export const empty = <A = never>(): readonly A[] => zero;

/***************************************************************************************************
* @section Module Getters
* @section Modules
**************************************************************************************************/

export const getShow = <A>({ show }: TC.Show<A>): TC.Show<readonly A[]> => ({
show: (ta) => `ReadonlyArray[${ta.map(show).join(", ")}]`,
});

export const getMonoid = <A = never>(): TC.Monoid<readonly A[]> => ({
export const Monoid: TC.Monoid<ReadonlyArray<_>> = {
empty,
concat: _concat,
});

/***************************************************************************************************
* @section Modules
**************************************************************************************************/
};

export const Functor: TC.Functor<ReadonlyArray<_>> = {
map: (fab, ta) => _map(ta, (a) => fab(a)),
Expand All @@ -131,19 +123,24 @@ export const Monad: TC.Monad<ReadonlyArray<_>> = {
_reduce(ta, (bs, a) => _concat(bs, fatb(a)), [] as readonly any[]),
};

export const Apply: TC.Apply<ReadonlyArray<_>> = {
ap: Monad.ap,
map: Functor.map,
};

export const Applicative: TC.Applicative<ReadonlyArray<_>> = {
of: Monad.of,
ap: Monad.ap,
map: Functor.map,
};

export const Filterable: TC.Filterable<ReadonlyArray<_>> = {
filter: (predicate, ta) => ta.filter(predicate),
export const Alt: TC.Alt<ReadonlyArray<_>> = {
alt: (ta, tb) => ta.length === 0 ? tb : ta,
map: Functor.map,
};

export const Apply: TC.Apply<ReadonlyArray<_>> = {
ap: Monad.ap,
map: Functor.map,
export const Filterable: TC.Filterable<ReadonlyArray<_>> = {
filter: (predicate, ta) => ta.filter(predicate),
};

export const IndexedFoldable: TC.IndexedFoldable<ReadonlyArray<_>> = {
Expand Down Expand Up @@ -177,6 +174,50 @@ export const Foldable: TC.Foldable<ReadonlyArray<_>> = IndexedFoldable;

export const Traversable: TC.Traversable<ReadonlyArray<_>> = IndexedTraversable;

/***************************************************************************************************
* @section Module Getters
**************************************************************************************************/

export const getSetoid = <A>(S: TC.Setoid<A>): TC.Setoid<readonly A[]> => ({
equals: (a, b) =>
a === b || (a.length === b.length && a.every((v, i) => S.equals(v, b[i]))),
});

export const getOrd = <A>(O: TC.Ord<A>): TC.Ord<readonly A[]> => {
const { equals } = getSetoid(O);
return ({
equals,
lte: (a, b) => {
const length = Math.min(a.length, b.length);
for (let i = 0; i < length; i++) {
if (!O.equals(a[i], b[i])) {
return O.lte(a[i], b[i]);
}
}
return a.length <= b.length;
},
});
};

export const getSemigroup = <A>(
S: TC.Semigroup<A>,
): TC.Semigroup<readonly A[]> => ({
concat: (a, b) => [a.reduce(S.concat, b.reduce(S.concat))],
});

export const getFreeSemigroup = <A>(): TC.Semigroup<readonly A[]> => ({
concat: _concat,
});

export const getShow = <A>({ show }: TC.Show<A>): TC.Show<readonly A[]> => ({
show: (ta) => `ReadonlyArray[${ta.map(show).join(", ")}]`,
});

export const getMonoid = <A = never>(): TC.Monoid<readonly A[]> => ({
empty,
concat: _concat,
});

/***************************************************************************************************
* @section Pipeables
**************************************************************************************************/
Expand Down
128 changes: 126 additions & 2 deletions testing/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { assertMonad } from "./assert.ts";
import { assertEquals } from "https://deno.land/std@0.70.0/testing/asserts.ts";

import * as Test from "./assert.ts";

import * as A from "../array.ts";

Deno.test({
name: "Array Constructors",
async fn() {
assertEquals(A.zero, []);
assertEquals(A.empty(), []);
},
});

const toString = (n: number): string => n.toString();
const toLength = (s: string): number => s.length;
const fromNumber = (n: number) => A.of(n.toString());
Expand All @@ -10,7 +20,13 @@ const fromString = (s: string) => A.of(s.length);
Deno.test({
name: "Array Modules",
async fn() {
await assertMonad(
await Test.assertFunctor(A.Functor, "Array", {
ta: A.of(1),
fab: toString,
fbc: toLength,
});

await Test.assertMonad(
A.Monad,
"Array",
{
Expand All @@ -24,5 +40,113 @@ Deno.test({
fbtc: fromString,
},
);

await Test.assertApply(
A.Apply,
"Array",
{
ta: A.of(1),
fab: toString,
fbc: toLength,
tfab: A.of(toString),
tfbc: A.of(toLength),
},
);

await Test.assertApplicative(
A.Applicative,
"Array",
{
a: 1,
ta: A.of(1),
fab: toString,
fbc: toLength,
tfab: A.of(toString),
tfbc: A.of(toLength),
},
);

await Test.assertAlt(
A.Alt,
"Array",
{
ta: [1, 2, 3],
tb: [4, 5, 6],
tc: [7, 8, 9],
fab: toString,
fbc: toLength,
},
);

await Test.assertFilterable(
A.Filterable,
"Array",
{
a: [1, 2, 3, 4],
b: [-2, 0, 2, 4],
f: (n: number) => n % 2 === 0,
g: (n: number) => n < 3,
},
);

await Test.assertFoldable(
A.Foldable,
"Array",
{
a: 0,
tb: [1, 2, 3, 4],
faba: (a: number, b: number) => a + b,
},
);
},
});

Deno.test({
name: "Array Module Getters",
async fn() {
const equals = (a: number, b: number) => a === b;
const lte = (a: number, b: number) => a < b;
const concat = (a: number, b: number) => a + b;

await Test.assertSetoid(
A.getSetoid({ equals }),
"Array",
{
a: [1, 2, 3],
b: [1, 2, 3],
c: [1, 2, 3],
z: [1, 2, 3, 4],
},
);

await Test.assertOrd(
A.getOrd({ equals, lte }),
"Array",
{
a: [1, 2, 3],
b: [3, 2, 1],
},
);

await Test.assertSemigroup(A.getSemigroup({ concat }), "Array", {
a: [1, 2, 3],
b: [2, 4, 8],
c: [10, 12, 14],
});

await Test.assertSemigroup(A.getFreeSemigroup<number>(), "Array", {
a: [1],
b: [2],
c: [3],
});

const { show } = A.getShow({ show: (n: number) => n.toString() });
assertEquals(show([1, 2, 3]), "ReadonlyArray[1, 2, 3]");

await Test.assertMonoid(A.getMonoid<number>(), "Array", {
a: [1, 2, 3],
b: [2, 4, 8],
c: [-2, 0, -100],
});
},
});

0 comments on commit ea5f3f4

Please sign in to comment.