-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
82 changed files
with
1,350 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.