-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from poteat/poteat/object-utils
Add additional object utilities + natural number reifications + `Iso` module.
- Loading branch information
Showing
62 changed files
with
1,351 additions
and
54 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
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,24 @@ | ||
export * from './iso/' | ||
|
||
/** | ||
* The `Iso` module contains various isomorphisms. These isomorphisms are used | ||
* to transform between different representations of the same type. | ||
* | ||
* Generally, isomorphisms consist of a mapping and an inverse mapping. | ||
* The isomorphism takes in a kind, and returns a new kind where the input is | ||
* transformed via the primary mapping, and the output is transformed via the | ||
* inverse mapping. | ||
* | ||
* A very common operation is to perform a .split(""), do some operations, and | ||
* then .join("") the result. This can be combined into a single step using | ||
* the `Iso` module. | ||
* | ||
* @example | ||
* ```ts | ||
* import { String, Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = $<Iso.String.Words, $<List.Map, String.Capitalize>>; | ||
* type T1 = $<T0, 'foo bar baz'>; // 'Foo Bar Baz' | ||
* ``` | ||
*/ | ||
declare module './iso' {} |
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,14 @@ | ||
export * as NaturalNumber from './natural-number' | ||
export * as String from './string' | ||
|
||
import * as NaturalNumber from './natural-number' | ||
import * as String from './string' | ||
|
||
const _ = { | ||
NaturalNumber: NaturalNumber, | ||
String: String | ||
} | ||
|
||
type _ = typeof _ | ||
|
||
export default _ |
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,16 @@ | ||
export * from './natural-number/' | ||
|
||
/** | ||
* The `Iso.NaturalNumber` module contains various number-related isomorphisms. | ||
* These first compute some transformation on the input, and then apply the | ||
* inverse transformation to the output. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = $<Iso.NaturalNumber.Increment, $<NaturalNumber.Multiply, 2>>; | ||
* type T1 = $<T0, 10>; // (N + 1) * 2 - 1 = 21 | ||
* ``` | ||
*/ | ||
declare module './natural-number' {} |
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,15 @@ | ||
import { $, Iso, NaturalNumber, Test } from '../..' | ||
|
||
type Decrement_Spec = [ | ||
/** | ||
* Can wrap a kind i.e. f(n - 1) + 1, for f(x) = x * 2 | ||
*/ | ||
Test.Expect< | ||
$<$<Iso.NaturalNumber.Decrement, $<NaturalNumber.Multiply, 2>>, 10>, | ||
19 | ||
> | ||
] | ||
|
||
it('should wrap a kind i.e. f(n - 1) + 1, for f(x) = x * 2', () => { | ||
expect(Iso.NaturalNumber.decrement(NaturalNumber.multiply(2))(10)).toBe(19) | ||
}) |
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,58 @@ | ||
import { $, Type, Kind, NaturalNumber } from '../..' | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is decremented by | ||
* one, and the output is incremented by one. | ||
* | ||
* @param {Kind.Kind} K - The kind to wrap with an increment/decrement. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = Iso.NaturalNumber._$decrement<$<NaturalNumber.Multiply, 2>>; | ||
* type T1 = $<T0, 10>; // (N - 1) * 2 + 1 = 19 | ||
* ``` | ||
*/ | ||
export type _$decrement<K extends Kind.Kind> = $< | ||
Kind.Pipe, | ||
[NaturalNumber.Decrement, K, NaturalNumber.Increment] | ||
> | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is decremented by | ||
* one, and the output is incremented by one. | ||
* | ||
* @param {Kind.Kind} K - The kind to wrap with an increment/decrement. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = $<Iso.NaturalNumber.Decrement, $<NaturalNumber.Multiply, 2>>; | ||
* type T1 = $<T0, 10>; // (N - 1) * 2 + 1 = 19 | ||
* ``` | ||
*/ | ||
export interface Decrement extends Kind.Kind { | ||
f(x: Type._$cast<this[Kind._], Kind.Kind>): _$decrement<typeof x> | ||
} | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is decremented by | ||
* one, and the output is incremented by one. | ||
* | ||
* @param {Kind.Kind} K - The kind to wrap with an increment/decrement. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* const T0 = Iso.NaturalNumber.decrement(NaturalNumber.multiply(2)); | ||
* const T1 = T0(10); // (N - 1) * 2 + 1 = 19 | ||
* ``` | ||
*/ | ||
export const decrement = ((f: Kind._$reify<Kind.Kind<(x: number) => number>>) => | ||
(x: number) => | ||
NaturalNumber.increment( | ||
f(NaturalNumber.decrement(x)) | ||
)) as Kind._$reify<Decrement> |
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,22 @@ | ||
import { $, Iso, NaturalNumber, Test, List } from '../..' | ||
|
||
type Digits_Spec = [ | ||
/** | ||
* Can convert a natural number to a list of digits. | ||
*/ | ||
Test.Expect< | ||
$< | ||
$<Iso.NaturalNumber.Digits, $<List.Map, $<NaturalNumber.Multiply, 2>>>, | ||
99 | ||
>, | ||
1818 | ||
> | ||
] | ||
|
||
it('should convert a natural number to a list of digits', () => { | ||
const result = Iso.NaturalNumber.digits(List.map(NaturalNumber.multiply(2)))( | ||
99 | ||
) | ||
|
||
expect(result).toBe(1818) | ||
}) |
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,65 @@ | ||
import { $, Type, Kind, NaturalNumber, Number } from '../..' | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is converted to a | ||
* list of digits, and the output is converted from a list of digits back to a | ||
* natural number. | ||
* | ||
* @param {Kind.Kind} K - The kind to convert to a natural number. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = Iso.NaturalNumber._$digits<$<List.Map<$<NaturalNumber.Multiply, 2>>>; | ||
* type T1 = $<T0, 99>; // 1818 | ||
* ``` | ||
*/ | ||
export type _$digits< | ||
K extends Kind.Kind<(x: Number.Number[]) => Number.Number[]> | ||
> = $<Kind.Pipe, [NaturalNumber.Digits, K, NaturalNumber.Undigits]> | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is converted to a | ||
* list of digits, and the output is converted from a list of digits back to a | ||
* natural number. | ||
* | ||
* @param {Kind.Kind} K - The kind to convert to a natural number. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = $<Iso.NaturalNumber.Digits, $<List.Map, $<NaturalNumber.Multiply, 2>>> | ||
* type T1 = $<T0, 99>; // 1818 | ||
* ``` | ||
*/ | ||
export interface Digits extends Kind.Kind { | ||
f( | ||
x: Type._$cast< | ||
this[Kind._], | ||
Kind.Kind<(x: Number.Number[]) => Number.Number[]> | ||
> | ||
): _$digits<typeof x> | ||
} | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is converted to a | ||
* list of digits, and the output is converted from a list of digits back to a | ||
* natural number. | ||
* | ||
* @param {Kind.Kind} K - The kind to convert to a natural number. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* const T0 = Iso.NaturalNumber.digits(List.map(NaturalNumber.multiply(2))); | ||
* const T1 = T0(99); // 1818 | ||
* ``` | ||
*/ | ||
export const digits = (( | ||
f: Kind._$reify<Kind.Kind<(x: Number.Number[]) => number[]>> | ||
) => | ||
(x: number) => | ||
NaturalNumber.undigits(f(NaturalNumber.digits(x)))) as Kind._$reify<Digits> |
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,15 @@ | ||
import { $, Iso, NaturalNumber, Test } from '../..' | ||
|
||
type Increment_Spec = [ | ||
/** | ||
* Can wrap a kind i.e. f(n + 1) - 1, for f(x) = x * 2 | ||
*/ | ||
Test.Expect< | ||
$<$<Iso.NaturalNumber.Increment, $<NaturalNumber.Multiply, 2>>, 10>, | ||
21 | ||
> | ||
] | ||
|
||
it('should wrap a kind i.e. f(n + 1) - 1, for f(x) = x * 2', () => { | ||
expect(Iso.NaturalNumber.increment(NaturalNumber.multiply(2))(10)).toBe(21) | ||
}) |
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,58 @@ | ||
import { $, Type, Kind, NaturalNumber } from '../..' | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is incremented by | ||
* one, and the output is decremented by one. | ||
* | ||
* @param {Kind.Kind} K - The kind to wrap with an increment/decrement. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = Iso.NaturalNumber._$increment<$<NaturalNumber.Multiply, 2>>; | ||
* type T1 = $<T0, 10>; // (N + 1) * 2 - 1 = 21 | ||
* ``` | ||
*/ | ||
export type _$increment<K extends Kind.Kind> = $< | ||
Kind.Pipe, | ||
[NaturalNumber.Increment, K, NaturalNumber.Decrement] | ||
> | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is incremented by | ||
* one, and the output is decremented by one. | ||
* | ||
* @param {Kind.Kind} K - The kind to wrap with an increment/decrement. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = $<Iso.NaturalNumber.Increment, $<NaturalNumber.Multiply, 2>>; | ||
* type T1 = $<T0, 10>; // (N + 1) * 2 - 1 = 21 | ||
* ``` | ||
*/ | ||
export interface Increment extends Kind.Kind { | ||
f(x: Type._$cast<this[Kind._], Kind.Kind>): _$increment<typeof x> | ||
} | ||
|
||
/** | ||
* Given a kind, return an isomorphism such that the input is incremented by | ||
* one, and the output is decremented by one. | ||
* | ||
* @param {Kind.Kind} K - The kind to wrap with an increment/decrement. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Iso } from "hkt-toolbelt"; | ||
* | ||
* const T0 = Iso.NaturalNumber.increment(NaturalNumber.multiply(2)); | ||
* const T1 = T0(10); // (N + 1) * 2 - 1 = 21 | ||
* ``` | ||
*/ | ||
export const increment = ((f: Kind._$reify<Kind.Kind<(x: number) => number>>) => | ||
(x: number) => | ||
NaturalNumber.decrement( | ||
f(NaturalNumber.increment(x)) | ||
)) as Kind._$reify<Increment> |
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,3 @@ | ||
export * from './decrement' | ||
export * from './digits' | ||
export * from './increment' |
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,16 @@ | ||
export * from './string/' | ||
|
||
/** | ||
* The `Iso.String` module contains various string-related isomorphisms. These | ||
* isomorphisms are used to transform between different representations of | ||
* strings. | ||
* | ||
* @example | ||
* ```ts | ||
* import { String, Iso } from "hkt-toolbelt"; | ||
* | ||
* type T0 = $<Iso.String.Words, $<List.Map, String.Capitalize>>; | ||
* type T1 = $<T0, 'foo bar baz'>; // 'Foo Bar Baz' | ||
* ``` | ||
*/ | ||
declare module './string' {} |
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,15 @@ | ||
import { $, Iso, String, Test, List } from '../..' | ||
|
||
type Chars_Spec = [ | ||
/** | ||
* Can repeat each character of a string by two. | ||
*/ | ||
Test.Expect< | ||
$<$<Iso.String.Chars, $<List.Map, $<String.RepeatBy, 2>>>, 'bar'>, | ||
'bbaarr' | ||
> | ||
] | ||
|
||
it('should repeat each character of a string by two', () => { | ||
expect(Iso.String.chars(List.map(String.repeatBy(2)))('bar')).toBe('bbaarr') | ||
}) |
Oops, something went wrong.