Skip to content

Commit

Permalink
docs: add math fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 26, 2024
1 parent 84dfec9 commit fb5638a
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,20 @@ await sleep(1000); // It suspends the exection for 1 second.

### Maths

- [clamp](https://js-std.pages.dev/Maths/clamp)
- [divMod](https://js-std.pages.dev/Maths/divMod)
- [isEven](https://js-std.pages.dev/Maths/isEven)
- [isOdd](https://js-std.pages.dev/Maths/isOdd)
- [mean](https://js-std.pages.dev/Maths/mean)
- [median](https://js-std.pages.dev/Maths/median)
- [mode](https://js-std.pages.dev/Maths/mode)
- [percentage](https://js-std.pages.dev/Maths/percentage)
- [percentageOf](https://js-std.pages.dev/Maths/percentageOf)
- [prod](https://js-std.pages.dev/Maths/prod)
- [sum](https://js-std.pages.dev/Maths/sum)

### Number

- [clamp](https://js-std.pages.dev/Number/clamp)
- [isNegZero](https://js-std.pages.dev/Number/isNegZero)
- [isZero](https://js-std.pages.dev/Number/isZero)
- [toNum](https://js-std.pages.dev/Number/toNum)
Expand Down
10 changes: 9 additions & 1 deletion apps/docs/pages/Maths/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"clamp": "clamp",
"divMod": "divMod",
"isEven": "isEven",
"isOdd": "isOdd",
"mean": "mean",
"median": "median",
"mode": "mode",
"percentage": "percentage",
"percentageOf": "percentageOf"
"percentageOf": "percentageOf",
"prod": "prod",
"sum": "sum"
}
32 changes: 32 additions & 0 deletions apps/docs/pages/Maths/divMod.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import REPL from "../../components/REPL";

> Returns a tuple with `Quotient` and `Remainder`.
## Syntax

```ts
import { divMod } from '@opentf/std';

divMod(a: number, b: number): [number, number]
```

## Examples

```ts
divMod(4, 2) //=> [2, 0]);

divMod(11, 4) //=> [2.75, 3]);

divMod(1, 0) //=> [Infinity, NaN]);

divMod(0, 1) //=> [0, 0]);

divMod(-9, -2) //=> [4.5, -1]);
```

## Try

<REPL code={`const { divMod } = require('@opentf/std');
divMod(4, 2);
`} />
32 changes: 32 additions & 0 deletions apps/docs/pages/Maths/isEven.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import REPL from "../../components/REPL";

> Checks if the given number is an `Even` number.
## Syntax

```ts
import { isEven } from '@opentf/std';

isEven(n: number): boolean
```

## Examples

```ts
isEven(0) //=> true

isEven(1) //=> false

isEven(2) //=> true

isEven(-1) //=> false

isEven(-2) //=> true
```

## Try

<REPL code={`const { isEven } = require('@opentf/std');
isEven(42);
`} />
32 changes: 32 additions & 0 deletions apps/docs/pages/Maths/isOdd.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import REPL from "../../components/REPL";

> Checks if the given number is a `Odd` number.
## Syntax

```ts
import { isOdd } from '@opentf/std';

isOdd(n: number): boolean
```

## Examples

```ts
isOdd(0) //=> false

isOdd(1) //=> true

isOdd(2) //=> false

isOdd(-1) //=> true

isOdd(-2) //=> false
```

## Try

<REPL code={`const { isOdd } = require('@opentf/std');
isOdd(9);
`} />
41 changes: 41 additions & 0 deletions apps/docs/pages/Maths/mean.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Calculates the `mean` value of the given array.
<Callout type="info">
Mean: The "average" number; found by adding all data points and dividing by the number of data points.
<div style={{textAlign: 'right'}}>
\- [Khan Academy](https://www.khanacademy.org/math/statistics-probability/summarizing-quantitative-data/mean-median-basics/a/mean-median-and-mode-review)
</div>
</Callout>

## Syntax

```ts
import { mean } from '@opentf/std';

mean(
arr: number[] = [],
cb?: (v: number, index: number) => number
): number
```

## Examples

```ts
mean([]) //=> NaN

mean([1]) //=> 1

mean([4, 1, 7]) //=> 4

mean([4, 2, 8]) //=> 4.67
```

## Try

<REPL code={`const { mean } = require('@opentf/std');
mean([4, 2, 8]);
`} />
45 changes: 45 additions & 0 deletions apps/docs/pages/Maths/median.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Calculates the `median` value of the given array.
<Callout type="info">
Median: The middle number; found by ordering all data points and picking out the one in the middle (or if there are two middle numbers, taking the mean of those two numbers).
<div style={{textAlign: 'right'}}>
\- [Khan Academy](https://www.khanacademy.org/math/statistics-probability/summarizing-quantitative-data/mean-median-basics/a/mean-median-and-mode-review)
</div>
</Callout>

## Syntax

```ts
import { median } from '@opentf/std';

median(
arr: number[] = [],
cb?: (val: number, index: number) => number
): number
```

## Examples

```ts
median([]) //=> NaN

median([1]) //=> 1

median([4, 1, 7]) //=> 1

median([4, 2, 8]) //=> 2

median([1, 4, 2, 5, 0]) //=> 2

median([10, 20, 40, 50]) //=> 30
```

## Try

<REPL code={`const { median } = require('@opentf/std');
median([10, 20, 40, 50]);
`} />
46 changes: 46 additions & 0 deletions apps/docs/pages/Maths/mode.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Callout } from "nextra/components";
import REPL from "../../components/REPL";

> Calculates the `mode` value of the given array.
<Callout type="info">
Mode: The most frequent number—that is, the number that occurs the highest number of times.
<div style={{textAlign: 'right'}}>
\- [Khan Academy](https://www.khanacademy.org/math/statistics-probability/summarizing-quantitative-data/mean-median-basics/a/mean-median-and-mode-review)
</div>
</Callout>

## Syntax

```ts
import { mode } from '@opentf/std';

mode<T>(
arr: T[] = [],
cb?: (val: T, index: number) => number
): number[]
```

## Examples

```ts
mode([1]) //=> []

mode([1, 2, 3, 4, 5]) //=> []

mode([4, 2]) //=> []

mode([4, 2, 3, 2, 2]) //=> [2]

mode([0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 4]) //=> [1, 2]

mode([{ n: 1 }, { n: 2 }, { n: 3 }, { n: 2 }], ({ n }) => n) //=> [2]

```

## Try

<REPL code={`const { mode } = require('@opentf/std');
mode([0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 4]);
`} />
40 changes: 40 additions & 0 deletions apps/docs/pages/Maths/prod.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import REPL from "../../components/REPL";

> Calculates the `product` of values in the given array.
## Syntax

```ts
import { prod } from '@opentf/std';

prod(
arr: number[] = [],
cb?: (val: number, index: number) => number
): number
```

## Examples

```ts
prod() //=> 1

prod([]) //=> 1

prod([1]) //=> 1

prod([1, -1]) //=> -1

prod([-1, -2]) //=> 2

prod([1, 2, 3, 4, 5]) //=> 120

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];
prod(objects, (v) => v.n) //=> 384
```

## Try

<REPL code={`const { prod } = require('@opentf/std');
prod([1, 2, 3, 4, 5]);
`} />
49 changes: 49 additions & 0 deletions apps/docs/pages/Maths/sum.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import REPL from "../../components/REPL";

> Calculates the `sum` of values in the given array.
## Syntax

```ts
import { sum } from '@opentf/std';

sum(
arr: number[] = [],
cb?: (val: number, index: number) => number
): number
```

## Examples

```ts
sum() //=> 0

sum([]) //=> 0

sum([1]) //=> 1

sum([1, -1]) //=> 0

sum([-1, -2]) //=> -3

sum([1, 2, 3, 4, 5]) //=> 15

sum([1.2, Math.PI]) //=> 4.34

sum([1, 2, 3, 4, 5.4], Math.round) //=> 15

const objects = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];
sum(objects, (v) => v.n) //=> 20

sum(objects, (v, i) => v.n * i) //=> 36

sum([Infinity, Infinity]) //=> Infinity

```

## Try

<REPL code={`const { sum } = require('@opentf/std');
sum([1, 2, 3, 4, 5]);
`} />
5 changes: 3 additions & 2 deletions apps/docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ You can try out these examples on the [Playground](/playground).

Some benchmark outputs are shown here for reference.

> [!IMPORTANT]
> Our priorities are reliability and accuracy rather than performance.
<Callout type="info">
Our priorities are reliability and accuracy rather than performance.
</Callout>

```sh
clone:
Expand Down

0 comments on commit fb5638a

Please sign in to comment.