-
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 #77 from MajorLift/230518-number-arithmetic
Add `Integer` type and arithmetic methods
- Loading branch information
Showing
92 changed files
with
4,153 additions
and
30 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
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,33 @@ | ||
import { $, Test, DigitList } from '..' | ||
|
||
type TrimRight_Spec = [ | ||
/** | ||
* Trimming zero should result in zero. | ||
*/ | ||
Test.Expect<$<DigitList.TrimRight, ['0']>, ['0']>, | ||
|
||
/** | ||
* Trimming an empty zero should result in zero. | ||
*/ | ||
Test.Expect<$<DigitList.TrimRight, []>, ['0']>, | ||
|
||
/** | ||
* Trimming a single digit should result in the same digit. | ||
*/ | ||
Test.Expect<$<DigitList.TrimRight, ['1']>, ['1']>, | ||
|
||
/** | ||
* Trimming a digit with trailing zeros should result in the same digit. | ||
*/ | ||
Test.Expect<$<DigitList.TrimRight, ['1', '0']>, ['1']>, | ||
|
||
/** | ||
* Should be able to handle multiple trailing zeros. | ||
*/ | ||
Test.Expect<$<DigitList.TrimRight, ['1', '0', '0']>, ['1']>, | ||
|
||
/** | ||
* Should be able to handle all zeroes. | ||
*/ | ||
Test.Expect<$<DigitList.TrimRight, ['0', '0', '0']>, ['0']> | ||
] |
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,53 @@ | ||
import { Type, Kind, DigitList } from '..' | ||
|
||
type _$trimRight2<A extends DigitList.DigitList> = A extends [ | ||
...infer Rest extends DigitList.DigitList, | ||
'0' | ||
] | ||
? _$trimRight2<Rest> | ||
: A | ||
|
||
/** | ||
* `_$trimRight` is a type-level function that trims trailing zeros from a digit list. | ||
* It returns the trimmed digit list. | ||
* | ||
* @template A - The digit list to trim. | ||
* @template TRIM - The digit list after trimming trailing zeros. | ||
* @template OUTPUT - The final output after trimming. If the trimmed list is empty, it returns ["0"]. | ||
* | ||
* @example | ||
* For example, we can use `_$trimRight` to trim trailing zeros from a digit list: | ||
* | ||
* ```ts | ||
* import { DigitList } from "hkt-toolbelt"; | ||
* | ||
* type Result = DigitList._$trimRight<["1", "2", "3", "0"]>; // ["1", "2", "3"] | ||
* ``` | ||
* | ||
* In this example, `Result` is a type that represents the digit list ["1", "2", "3"], which is the result of trimming the trailing zeros from the digit list ["1", "2", "3", "0"]. | ||
* | ||
*/ | ||
export type _$trimRight< | ||
A extends DigitList.DigitList, | ||
TRIM extends DigitList.DigitList = _$trimRight2<A>, | ||
OUTPUT extends DigitList.DigitList = TRIM extends [] ? ['0'] : TRIM | ||
> = OUTPUT | ||
|
||
/** | ||
* `TrimRight` is a type-level function that trims trailing zeros from a digit list. | ||
* It returns the trimmed digit list. | ||
* | ||
* @template x - A digit list to trim trailing zeros from. | ||
* | ||
* @example | ||
* For example, we can use `TrimRight` to trim trailing zeros from a digit list: | ||
* | ||
* ```ts | ||
* import { $, DigitList } from "hkt-toolbelt"; | ||
* | ||
* type Result = $<DigitList.TrimRight, ["3", "0", "0"]>; // ["3"] | ||
* ``` | ||
*/ | ||
export interface TrimRight extends Kind.Kind { | ||
f(x: Type._$cast<this[Kind._], DigitList.DigitList>): _$trimRight<typeof x> | ||
} |
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 @@ | ||
export * from './integer/' |
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,39 @@ | ||
import { $, Test, Integer } from '..' | ||
|
||
export type Add_Spec = [ | ||
/** | ||
* Can add two zeros. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, 0>, 0>, 0>, | ||
Test.Expect<$<$<Integer.Add, 0>, -0>, 0>, | ||
|
||
/** | ||
* Can add two positive numbers. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, 5>, 5>, 10>, | ||
|
||
/** | ||
* Can add two negative numbers. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, -1>, -1>, -2>, | ||
Test.Expect<$<$<Integer.Add, -7>, -4>, -11>, | ||
|
||
/** | ||
* Can add a positive number to a negative number. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, -1>, 0>, -1>, | ||
Test.Expect<$<$<Integer.Add, -1>, 1>, 0>, | ||
Test.Expect<$<$<Integer.Add, -100>, 1>, -99>, | ||
|
||
/** | ||
* Can add large numbers. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, 123456789>, 123456789>, 246913578>, | ||
Test.Expect<$<$<Integer.Add, -123456789>, -123456789>, -246913578>, | ||
|
||
/** | ||
* Can add numbers as strings. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, '-123456789'>, '-123456789'>, -246913578>, | ||
Test.Expect<$<$<Integer.Add, '-123456789'>, '123456789'>, 0> | ||
] |
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,113 @@ | ||
import { $, Test, Integer, List } from '..' | ||
|
||
export type Add_Spec = [ | ||
/** | ||
* Can add large numbers. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, -123456789>, -123456789>, -246913578>, | ||
|
||
/** | ||
* Can add numbers as strings. | ||
*/ | ||
Test.Expect<$<$<Integer.Add, '-123456789'>, '-123456789'>, -246913578>, | ||
|
||
/** | ||
* Can add very large numbers. | ||
*/ | ||
Test.Expect< | ||
$<$<Integer.Add, -9007199254740991>, -9007199254740991>, | ||
-18014398509481982 | ||
>, | ||
|
||
/** | ||
* Can add bigint numbers. | ||
*/ | ||
Test.Expect< | ||
$<$<Integer.Add, 9007199254740991n>, 9007199254740991n>, | ||
18014398509481982 | ||
>, | ||
|
||
/** | ||
* Can map and add over lists. | ||
*/ | ||
Test.Expect< | ||
$<$<List.Map, $<Integer.Add, -10>>, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>, | ||
[-9, -8, -7, -6, -5, -4, -3, -2, -1, 0] | ||
>, | ||
|
||
/** | ||
* Can add a lot of negative numbers in a nested way. | ||
*/ | ||
Test.Expect< | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$< | ||
$<Integer.Add, -10>, | ||
$<$<Integer.Add, -10>, -10> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
>, | ||
-200 | ||
>, | ||
|
||
/** | ||
* Can add numbers with hundreds of digits. | ||
*/ | ||
Test.Expect< | ||
$< | ||
$< | ||
Integer.Add, | ||
'123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789' | ||
>, | ||
'123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789' | ||
>, | ||
246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578246913578n | ||
> | ||
] |
Oops, something went wrong.