From d9687b5619bec9940281e70e3ddb5c9a1e1983c1 Mon Sep 17 00:00:00 2001 From: GBT Date: Mon, 10 Aug 2020 16:22:19 +0300 Subject: [PATCH 1/3] Adding arithmetic computation: Power. --- README.md | 1 + src/power/index.d.ts | 41 +++++++++++++++++++++++++++++++++++++++ src/power/index.test-d.ts | 11 +++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/power/index.d.ts create mode 100644 src/power/index.test-d.ts diff --git a/README.md b/README.md index e84ab3d..a57f29e 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ $ yarn test - [Subtract](src/subtract/index.d.ts) - subtracts two numbers. - [Multiply](src/multiply/index.d.ts) - multiplies two numbers. - [Divide](src/divide/index.d.ts) - divides two numbers. + - [Power](src/power/index.d.ts) - computes A power B. - [Greater than or equal](src/gte/index.d.ts) - checks if a value is greater than or equal to another value. - [Less than or equal](src/lte/index.d.ts) - checks if a value is less than or equal to another value. - [Max](src/max/index.d.ts) - computes the maximum value of an array. diff --git a/src/power/index.d.ts b/src/power/index.d.ts new file mode 100644 index 0000000..d93a68f --- /dev/null +++ b/src/power/index.d.ts @@ -0,0 +1,41 @@ +import { Add, Dec, Cast } from '..'; + +// Multiply two numbers: https://lodash.com/docs/4.17.15#multiply. +// +type S = Multiply<2, 3>; // 6 +// +// This type uses recursive (and not officially supported) type alias, see more: +// https://github.com/microsoft/TypeScript/issues/26223#issuecomment-513187373. +export type Multiply< + // The first number in a multiplication. + A extends number, + // The second number in a multiplication. + B extends number + > = { + // If any of the numbers is 0, return 0: + zero: 0; + // Next, check if any of them are equal to 1 and return the other number: + 'one-b': A; + 'one-a': B; + // If they're both not 1s or 0s, we call the recursion again, like this: + // + // Add>> + // + // We add the value of the first number (A) to the result of calling Multiply + // again in which we decrease B's value by 1. This means that we call Add + // B times. + // + // Computation is split into multiple steps with `infer`, see more: + // https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts#L17. + next: Multiply> extends infer G // Assign result to `G` + ? Add> + : never; +}[A extends 0 + ? 'zero' + : B extends 0 + ? 'zero' + : B extends 1 + ? 'one-b' + : A extends 1 + ? 'one-a' + : 'next']; diff --git a/src/power/index.test-d.ts b/src/power/index.test-d.ts new file mode 100644 index 0000000..851703b --- /dev/null +++ b/src/power/index.test-d.ts @@ -0,0 +1,11 @@ +import { expectType } from 'tsd'; +import { Multiply } from '.'; + +expectType>(0); +expectType>(0); +expectType>(1); +expectType>(5); +expectType>(4); +expectType>(9); +expectType>(8); +expectType>(this as never); From 6b759dd666be2c03d85b3523cb763f1bf8698a8b Mon Sep 17 00:00:00 2001 From: GBT Date: Mon, 10 Aug 2020 16:37:17 +0300 Subject: [PATCH 2/3] Fixing, last commit was multiply by mistake. --- src/power/index.d.ts | 35 +++++++++++++---------------------- src/power/index.test-d.ts | 19 ++++++++++--------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/power/index.d.ts b/src/power/index.d.ts index d93a68f..73009fc 100644 --- a/src/power/index.d.ts +++ b/src/power/index.d.ts @@ -1,41 +1,32 @@ -import { Add, Dec, Cast } from '..'; +import { Multiply, Dec, Cast } from '..'; -// Multiply two numbers: https://lodash.com/docs/4.17.15#multiply. +// Power two numbers: https://lodash.com/docs/4.17.15#power. // -type S = Multiply<2, 3>; // 6 +type S = Power<2, 3>; // 8 // // This type uses recursive (and not officially supported) type alias, see more: // https://github.com/microsoft/TypeScript/issues/26223#issuecomment-513187373. -export type Multiply< +export type Power< // The first number in a multiplication. A extends number, // The second number in a multiplication. B extends number > = { - // If any of the numbers is 0, return 0: + // If A is 0: return 0 + // If B is 0: return 1 zero: 0; - // Next, check if any of them are equal to 1 and return the other number: - 'one-b': A; - 'one-a': B; + one: 1; // If they're both not 1s or 0s, we call the recursion again, like this: // - // Add>> + // Multiply>> // - // We add the value of the first number (A) to the result of calling Multiply - // again in which we decrease B's value by 1. This means that we call Add + // We multiply the value of the first number (A) to the result of calling Multiply + // again in which we decrease B's value by 1. This means that we call Multiply // B times. // // Computation is split into multiple steps with `infer`, see more: // https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts#L17. - next: Multiply> extends infer G // Assign result to `G` - ? Add> + next: Power> extends infer G // Assign result to `G` + ? Multiply> : never; -}[A extends 0 - ? 'zero' - : B extends 0 - ? 'zero' - : B extends 1 - ? 'one-b' - : A extends 1 - ? 'one-a' - : 'next']; +}[B extends 0 ? 'one' : A extends 0 ? 'zero' : A extends 1 ? 'one' : 'next']; diff --git a/src/power/index.test-d.ts b/src/power/index.test-d.ts index 851703b..61e3146 100644 --- a/src/power/index.test-d.ts +++ b/src/power/index.test-d.ts @@ -1,11 +1,12 @@ import { expectType } from 'tsd'; -import { Multiply } from '.'; +import { Power } from '.'; -expectType>(0); -expectType>(0); -expectType>(1); -expectType>(5); -expectType>(4); -expectType>(9); -expectType>(8); -expectType>(this as never); +expectType>(8); +expectType>(9); +expectType>(1); +expectType>(0); +expectType>(1); +expectType>(1); +expectType>(1); +expectType>(4); +expectType>(this as never); From ddb9b41e84028d7a98d05430358341b2933ec750 Mon Sep 17 00:00:00 2001 From: GBT Date: Thu, 27 Aug 2020 13:06:20 +0300 Subject: [PATCH 3/3] PR Changes request --- README.md | 2 +- src/power/index.d.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a57f29e..2f9ad4c 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ $ yarn test - [Subtract](src/subtract/index.d.ts) - subtracts two numbers. - [Multiply](src/multiply/index.d.ts) - multiplies two numbers. - [Divide](src/divide/index.d.ts) - divides two numbers. - - [Power](src/power/index.d.ts) - computes A power B. + - [Power](src/power/index.d.ts) - computes the given base taken to the power of the given exponent. - [Greater than or equal](src/gte/index.d.ts) - checks if a value is greater than or equal to another value. - [Less than or equal](src/lte/index.d.ts) - checks if a value is less than or equal to another value. - [Max](src/max/index.d.ts) - computes the maximum value of an array. diff --git a/src/power/index.d.ts b/src/power/index.d.ts index 73009fc..743de32 100644 --- a/src/power/index.d.ts +++ b/src/power/index.d.ts @@ -1,15 +1,15 @@ import { Multiply, Dec, Cast } from '..'; -// Power two numbers: https://lodash.com/docs/4.17.15#power. +// Power two numbers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow // type S = Power<2, 3>; // 8 // // This type uses recursive (and not officially supported) type alias, see more: // https://github.com/microsoft/TypeScript/issues/26223#issuecomment-513187373. export type Power< - // The first number in a multiplication. + // The Base. A extends number, - // The second number in a multiplication. + // The Exponent used to raise the Base. B extends number > = { // If A is 0: return 0