Skip to content

Commit

Permalink
chore(stdlib): Add Examples to rational docs
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Jan 2, 2024
1 parent 3c0ea18 commit a76601b
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
55 changes: 55 additions & 0 deletions stdlib/rational.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Utilities for working with the Rational type.
* @example include "rational"
*
* @example 1/2r
* @example 3/4r
*
* @since v0.6.0
*/

Expand Down Expand Up @@ -30,6 +33,9 @@ provide { fromNumber, toNumber, numerator, denominator }
* @param x: The rational to split
* @returns The numerator and denominator of the rational
*
* @example Rational.toIntegerRatio(1/2r) == (1, 2)
* @example Rational.toIntegerRation(2/8r) == (1, 4)
*
* @since v0.6.0
*/
provide let toIntegerRatio = x => {
Expand All @@ -46,6 +52,9 @@ provide let toIntegerRatio = x => {
* @throws InvalidArgument(String): If the numerator is not an integer
* @throws InvalidArgument(String): If the denominator is not an integer
*
* @example Rational.fromIntegerRatio(1, 2) == 1/2r
* @example Rational.fromIntegerRatio(2, 8) == 1/4r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -68,6 +77,10 @@ provide let fromIntegerRatio = (numerator: Number, denominator: Number) => {
* @param y: The second operand
* @returns The sum of the two operands
*
* @example
* from Rational use { (+) }
* assert 1/2r + 1/4r == 3/4r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -85,6 +98,10 @@ provide let (+) = (x: Rational, y: Rational) => {
* @param y: The second operand
* @returns The difference of the two operands
*
* @example
* from Rational use { (-) }
* assert 1/2r - 1/4r == 1/4r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -102,6 +119,10 @@ provide let (-) = (x: Rational, y: Rational) => {
* @param y: The second operand
* @returns The product of the two operands
*
* @example
* from Rational use { (*) }
* assert 1/2r * 1/4r == 1/8r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -119,6 +140,10 @@ provide let (*) = (x: Rational, y: Rational) => {
* @param y: The second operand
* @returns The quotient of the two operands
*
* @example
* from Rational use { (/) }
* assert 1/2r / 1/4r == 2/1r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -136,6 +161,10 @@ provide let (/) = (x: Rational, y: Rational) => {
* @param y: The second value
* @returns `true` if the first value is equal to the second value or `false` otherwise
*
* @example
* from Rational use { (==) }
* assert 1/2r == 1/2r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -152,6 +181,10 @@ provide let (==) = (x: Rational, y: Rational) => {
* @param y: The second value
* @returns `true` if the first value is not equal to the second value or `false` otherwise
*
* @example
* from Rational use { (!=) }
* assert 1/2r != 1/4r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -168,6 +201,10 @@ provide let (!=) = (x: Rational, y: Rational) => {
* @param y: The second value
* @returns `true` if the first value is less than the second value or `false` otherwise
*
* @example
* from Rational use { (<) }
* assert 1/4r < 1/2r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -185,6 +222,10 @@ provide let (<) = (x: Rational, y: Rational) => {
* @param y: The second value
* @returns `true` if the first value is greater than the second value or `false` otherwise
*
* @example
* from Rational use { (>) }
* assert 1/2r > 1/4r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -202,6 +243,13 @@ provide let (>) = (x: Rational, y: Rational) => {
* @param y: The second value
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
*
* @example
* from Rational use { (<=) }
* assert 1/4r <= 1/2r
* @example
* from Rational use { (<=) }
* assert 1/2r <= 1/2r
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -219,6 +267,13 @@ provide let (<=) = (x: Rational, y: Rational) => {
* @param y: The second value
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
*
* @example
* from Rational use { (>=) }
* assert 1/2r >= 1/4r
* @example
* from Rational use { (>=) }
* assert 1/2r >= 1/2r
*
* @since v0.6.0
*/
@unsafe
Expand Down
108 changes: 108 additions & 0 deletions stdlib/rational.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ No other changes yet.
include "rational"
```

```grain
1/2r
```

```grain
3/4r
```

## Values

Functions and constants included in the Rational module.
Expand Down Expand Up @@ -142,6 +150,16 @@ Returns:
|----|-----------|
|`(Number, Number)`|The numerator and denominator of the rational|

Examples:

```grain
Rational.toIntegerRatio(1/2r) == (1, 2)
```

```grain
Rational.toIntegerRation(2/8r) == (1, 4)
```

### Rational.**fromIntegerRatio**

<details disabled>
Expand Down Expand Up @@ -175,6 +193,16 @@ Throws:
* If the numerator is not an integer
* If the denominator is not an integer

Examples:

```grain
Rational.fromIntegerRatio(1, 2) == 1/2r
```

```grain
Rational.fromIntegerRatio(2, 8) == 1/4r
```

### Rational.**(+)**

<details disabled>
Expand All @@ -201,6 +229,13 @@ Returns:
|----|-----------|
|`Rational`|The sum of the two operands|

Examples:

```grain
from Rational use { (+) }
assert 1/2r + 1/4r == 3/4r
```

### Rational.**(-)**

<details disabled>
Expand All @@ -227,6 +262,13 @@ Returns:
|----|-----------|
|`Rational`|The difference of the two operands|

Examples:

```grain
from Rational use { (-) }
assert 1/2r - 1/4r == 1/4r
```

### Rational.**(*)**

<details disabled>
Expand All @@ -253,6 +295,13 @@ Returns:
|----|-----------|
|`Rational`|The product of the two operands|

Examples:

```grain
from Rational use { (*) }
assert 1/2r * 1/4r == 1/8r
```

### Rational.**(/)**

<details disabled>
Expand All @@ -279,6 +328,13 @@ Returns:
|----|-----------|
|`Rational`|The quotient of the two operands|

Examples:

```grain
from Rational use { (/) }
assert 1/2r / 1/4r == 2/1r
```

### Rational.**(==)**

<details disabled>
Expand All @@ -305,6 +361,13 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is equal to the second value or `false` otherwise|

Examples:

```grain
from Rational use { (==) }
assert 1/2r == 1/2r
```

### Rational.**(!=)**

<details disabled>
Expand All @@ -331,6 +394,13 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is not equal to the second value or `false` otherwise|

Examples:

```grain
from Rational use { (!=) }
assert 1/2r != 1/4r
```

### Rational.**(<)**

<details disabled>
Expand All @@ -357,6 +427,13 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is less than the second value or `false` otherwise|

Examples:

```grain
from Rational use { (<) }
assert 1/4r < 1/2r
```

### Rational.**(>)**

<details disabled>
Expand All @@ -383,6 +460,13 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is greater than the second value or `false` otherwise|

Examples:

```grain
from Rational use { (>) }
assert 1/2r > 1/4r
```

### Rational.**(<=)**

<details disabled>
Expand All @@ -409,6 +493,18 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|

Examples:

```grain
from Rational use { (<=) }
assert 1/4r <= 1/2r
```

```grain
from Rational use { (<=) }
assert 1/2r <= 1/2r
```

### Rational.**(>=)**

<details disabled>
Expand All @@ -435,3 +531,15 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|

Examples:

```grain
from Rational use { (>=) }
assert 1/2r >= 1/4r
```

```grain
from Rational use { (>=) }
assert 1/2r >= 1/2r
```

0 comments on commit a76601b

Please sign in to comment.