Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding arithmetic computation: Power. #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 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.
Expand Down
32 changes: 32 additions & 0 deletions src/power/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Multiply, Dec, Cast } from '..';

// Power two numbers: https://lodash.com/docs/4.17.15#power.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be the same description as in the README.md. Also, this URL doesn't lead anywhere, how about we use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow instead?

//
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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think multiplication is a mistake, how about The base number?

A extends number,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be B (short for base).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it breaks the consistency of other arithmetic computations in the project where A is always the first number of the evaluation (like in normal calculators)

// The second number in a multiplication.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a mistake too, how about The exponent used to raise the base?

B extends number
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be E (short for exponent).

> = {
// If A is 0: return 0
// If B is 0: return 1
zero: 0;
one: 1;
// If they're both not 1s or 0s, we call the recursion again, like this:
//
// Multiply<A, Power<A, Dec<B>>>
//
// 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<A, A>
// 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: Power<A, Dec<B>> extends infer G // Assign result to `G`
? Multiply<A, Cast<G, number>>
: never;
}[B extends 0 ? 'one' : A extends 0 ? 'zero' : A extends 1 ? 'one' : 'next'];
12 changes: 12 additions & 0 deletions src/power/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expectType } from 'tsd';
import { Power } from '.';

expectType<Power<2, 3>>(8);
expectType<Power<3, 2>>(9);
expectType<Power<0, 0>>(1);
expectType<Power<0, 3>>(0);
expectType<Power<4, 0>>(1);
expectType<Power<1, 1>>(1);
expectType<Power<1, 5>>(1);
expectType<Power<4, 1>>(4);
expectType<Power<3, 5>>(this as never);