Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Sep 26, 2024
1 parent 2ce19fb commit 8dc79f3
Show file tree
Hide file tree
Showing 82 changed files with 1,350 additions and 612 deletions.
6 changes: 3 additions & 3 deletions arrays/aggregation.js → array/aggregation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { equals } from "../booleans/equality.js";
import { identity } from "../higher_order.js";
import { defaultValue } from "../type.js";
import { equals } from "../boolean/equality.js";
import { identity } from "../higher_order/identity.js";
import { defaultValue } from "../validation/type.js";

/**
* Takes a callback of three arguments (`acc`, `el`, `i`), an initial value and
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions arrays/filtering.js → array/filtering.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isSingle } from "../collections/length.js";
import { isArray, isNull } from "../type.js";
import { not } from "../booleans/negation.js";
import { propertyEquals } from "../objects/membership.js";
import { isSingle } from "../collection/length.js";
import { isArray, isNull } from "../validation/type.js";
import { not } from "../boolean/negation.js";
import { propertyEquals } from "../object/membership.js";

function filter(predicate) {
return (arr) => arr.filter(predicate);
Expand Down Expand Up @@ -56,7 +56,7 @@ function removeDuplicatedProperties(key) {
};
}

export function searchForPropertyEquals(property) {
export function filterForPropertyEquals(property) {
return (array) => (value) =>
array.filter((obj) => propertyEquals(property)(value)(obj));
}
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions arrays/grouping.js → array/grouping.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { identity } from "../higher_order.js";
import { identity } from "../higher_order/identity.js";
import { map } from "./mapping.js";
import { zip } from "./join.js";
import { keys } from "../objects/access.js";
import { keys } from "../object/access.js";

function partition(predicate) {
return (arr) => {
Expand Down
2 changes: 1 addition & 1 deletion arrays/join.js → array/join.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { map } from "./mapping.js";
import { isSingle, length } from "../collections/length.js";
import { isSingle, length } from "../collection/length.js";

function flatten(arr) {
return arr.flat();
Expand Down
2 changes: 1 addition & 1 deletion arrays/mapping.js → array/mapping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSingleOrEmpty } from "../collections/length.js";
import { isSingleOrEmpty } from "../collection/length.js";

function map(f) {
return (arr) => arr.map(f);
Expand Down
12 changes: 6 additions & 6 deletions arrays/membership.js → array/membership.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isSingle } from "../collections/length.js";
import { first } from "../collections/single_access.js";
import { isArray } from "../type.js";
import { some } from "../booleans/some.js";
import { none } from "../booleans/none.js";
import { equals } from "../booleans/equality.js";
import { isSingle } from "../collection/length.js";
import { first } from "../collection/single_access.js";
import { isArray } from "../validation/type.js";
import { some } from "../boolean/some.js";
import { none } from "../boolean/none.js";
import { equals } from "../boolean/equality.js";

function contains(item) {
return (arr) => some(equals(item))(arr);
Expand Down
31 changes: 31 additions & 0 deletions array/pair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function pair(a) {
return (b) => [a, b];
}

export function pairWith(b) {
return (a) => [a, b];
}

export function pairBy(f) {
return (x) => [x, f(x)];
}

export function duplicate(a) {
return [a, a];
}

export function mapFirst(f) {
return ([a, b]) => [f(a), b];
}

export function mapSecond(g) {
return ([a, b]) => [a, g(b)];
}

export function mapPair(g) {
return ([a, b]) => [a, g(a)(b)];
}

export function foldPair(f) {
return ([a, b]) => f(a)(b);
}
42 changes: 42 additions & 0 deletions array/pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Define generic types
type Pair<A, B> = [A, B];

// Function to create a pair with the first element fixed
export function pair<A>(a: A) {
return <B>(b: B): Pair<A, B> => [a, b];
}

// Function to create a pair with the second element fixed
export function pairWith<B>(b: B) {
return <A>(a: A): Pair<A, B> => [a, b];
}

// Function to create a pair with the second element as a result of a function applied to the first element
export function pairBy<F extends (x: any) => any>(f: F) {
return <X>(x: X): [X, ReturnType<F>] => [x, f(x)];
}

// Function to duplicate a value in a pair
export function duplicate<A>(a: A): Pair<A, A> {
return [a, a];
}

// Function to map the first element of a pair using a function
export function mapFirst<A, B, C>(f: (a: A) => C) {
return ([a, b]: Pair<A, B>): Pair<C, B> => [f(a), b];
}

// Function to map the second element of a pair using a function
export function mapSecond<A, B, C>(g: (b: B) => C) {
return ([a, b]: Pair<A, B>): Pair<A, C> => [a, g(b)];
}

// Function to map the second element of a pair using a function that takes the first element
export function mapPair<A, B, C>(g: (a: A) => (b: B) => C) {
return ([a, b]: Pair<A, B>): Pair<A, C> => [a, g(a)(b)];
}

// Function to fold a pair using a function that takes two arguments
export function foldPair<A, B, C>(f: (a: A) => (b: B) => C) {
return ([a, b]: Pair<A, B>): C => f(a)(b);
}
64 changes: 64 additions & 0 deletions array/pair_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/assert/mod.ts";
import {
duplicate,
foldPair,
mapFirst,
mapPair,
mapSecond,
pair,
pairBy,
pairWith,
} from "./pair.ts";

// Test `pair` function
Deno.test("pair should create a pair with the first element fixed", () => {
const p = pair(1)("test");
assertEquals(p, [1, "test"]);
});

Deno.test("pairWith should create a pair with the second element fixed", () => {
const p = pairWith("fixed")(2);
assertEquals(p, [2, "fixed"]);
});

// Test `pairBy` function
Deno.test("pairBy should create a pair with the second element as result of function applied to first", () => {
const p = pairBy((x: number) => x * 2)(5);
assertEquals(p, [5, 10]);
});

// Test `duplicate` function
Deno.test("duplicate should create a pair with both elements the same", () => {
const p = duplicate("same");
assertEquals(p, ["same", "same"]);
});

// Test `mapFirst` function
Deno.test("mapFirst should map the first element of the pair", () => {
const p = mapFirst((a: number) => a + 1)([1, "value"]);
assertEquals(p, [2, "value"]);
});

// Test `mapSecond` function
Deno.test("mapSecond should map the second element of the pair", () => {
const p = mapSecond((b: string) => b.toUpperCase())([1, "value"]);
assertEquals(p, [1, "VALUE"]);
});

// Test `mapPair` function
Deno.test("mapPair should apply function to the second element based on the first element", () => {
const p = mapPair((a: number) => (b: string) => `${a}-${b}`)([2, "value"]);
assertEquals(p, [2, "2-value"]);
});

// Test `foldPair` function
Deno.test("foldPair should fold a pair into a single value", () => {
const result = foldPair((a: number) => (b: string) => `${a}:${b}`)([
3,
"test",
]);
assertEquals(result, "3:test");
});
4 changes: 2 additions & 2 deletions arrays/search.js → array/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { filterIndices } from "./filtering.js";
import { equals } from "../booleans/equality.js";
import { isNull } from "../type.js";
import { equals } from "../boolean/equality.js";
import { isNull } from "../validation/type.js";

function findSingleOrNull(predicate) {
return (input) => {
Expand Down
4 changes: 2 additions & 2 deletions arrays/sorting.js → array/sorting.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEmpty } from "../collections/length.js";
import { isNumber } from "../type.js";
import { isEmpty } from "../collection/length.js";
import { isNumber } from "../validation/type.js";

function sort(arr) {
const copy = arr.slice();
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions promise.js → async/promise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isArray, isFunction } from "./type.js";
import { first } from "./collections/single_access.js";
import { isSingle } from "./collections/length.js";
import { map } from "./arrays/mapping.js";
import { isArray, isFunction } from "../validation/type.js";
import { first } from "../collection/single_access.js";
import { isSingle } from "../collection/length.js";
import { map } from "../array/mapping.js";

export function resolve(value) {
return Promise.resolve(value);
Expand Down
23 changes: 23 additions & 0 deletions boolean/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isArray } from "../validation/type.js";
import { isFalse, isTrue } from "./equality.js";

function all(...predicates) {
return (...itemsOrArray) => {
const items = itemsOrArray.length === 1 && Array.isArray(itemsOrArray[0])
? itemsOrArray[0]
: itemsOrArray;

for (const item of items) {
for (const predicate of predicates) {
if (!predicate(item)) {
return false;
}
}
}
return true;
};
}

const allTrue = all(isTrue);

export { all, allTrue };
2 changes: 1 addition & 1 deletion booleans/conditionality.js → boolean/conditionality.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { is2DArray, isFunction } from "../type.js";
import { is2DArray, isFunction } from "../validation/type.js";
import { equals } from "./equality.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion booleans/constant.js → boolean/constant.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { constant } from "../higher_order.js";
import { constant } from "../higher_order/constance.js";

const alwaysTrue = constant(true);

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions booleans/none.js → boolean/none.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArray } from "../type.js";
import { all, allPass } from "./all.js";
import { isArray } from "../validation/type.js";
import { all } from "./all.js";
import { isFalse } from "./equality.js";

function none(predicate) {
Expand All @@ -22,7 +22,7 @@ const allFalse = all(isFalse);
function allFail(...predicates) {
const firstItem = predicates[0];
if (isArray(firstItem)) {
return allPass(...firstItem);
return all(...firstItem);
}

return (items) => {
Expand Down
2 changes: 1 addition & 1 deletion booleans/some.js → boolean/some.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from "../type.js";
import { isArray } from "../validation/type.js";
import { isTrue } from "./equality.js";

function some(predicate) {
Expand Down
38 changes: 0 additions & 38 deletions booleans/all.js

This file was deleted.

2 changes: 1 addition & 1 deletion collections/join.js → collection/join.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isString } from "../type.js";
import { isArray, isString } from "../validation/type.js";

export function concat(...items) {
if (items.length === 1) {
Expand Down
2 changes: 1 addition & 1 deletion collections/length.js → collection/length.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString } from "../type.js";
import { isString } from "../validation/type.js";

/**
* length.
Expand Down
6 changes: 3 additions & 3 deletions collections/multi_access.js → collection/multi_access.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { head, last } from "./single_access.js";
import { lastIndex, length } from "./length.js";
import { findIndex } from "../arrays/search.js";
import { isNull } from "../type.js";
import { inclusiveRange, range } from "../arrays/creation.js";
import { findIndex } from "../array/search.js";
import { isNull } from "../validation/type.js";
import { inclusiveRange, range } from "../array/creation.js";

function slice(indices) {
return (input) => {
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions collections/update.js → collection/update.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isString } from "../type.js";
import { equals } from "../booleans/equality.js";
import { exclude } from "../arrays/filtering.js";
import { indexOf } from "../arrays/search.js";
import { isString } from "../validation/type.js";
import { equals } from "../boolean/equality.js";
import { exclude } from "../array/filtering.js";
import { indexOf } from "../array/search.js";
import { throwError } from "../util/error.js";

/**
Expand Down
Loading

0 comments on commit 8dc79f3

Please sign in to comment.