aboutsummaryrefslogtreecommitdiff
path: root/api/api.md
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-11-09 18:42:16 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-11-09 18:42:16 -0500
commit1d461611bac782c272d0e082d5da74b4fe353ae6 (patch)
tree0b1687a3edb507835f9aa3b7666fd590975b73ff /api/api.md
parent78fd9141bb7dfcf817158a7a4d098e0e4b3d515b (diff)
Rename Num -> Float64, Num32 -> Float32
Diffstat (limited to 'api/api.md')
-rw-r--r--api/api.md1651
1 files changed, 847 insertions, 804 deletions
diff --git a/api/api.md b/api/api.md
index eaf8da17..51c30c2d 100644
--- a/api/api.md
+++ b/api/api.md
@@ -163,14 +163,14 @@ setenv("FOOBAR", "xyz")
## sleep
```tomo
-sleep : func(seconds: Num -> Void)
+sleep : func(seconds: Float64 -> Void)
```
Pause execution for a given number of seconds.
Argument | Type | Description | Default
---------|------|-------------|---------
-seconds | `Num` | How many seconds to sleep for. | -
+seconds | `Float64` | How many seconds to sleep for. | -
**Return:** Nothing.
@@ -342,2120 +342,2163 @@ assert [x for x in Byte(2).to(5, step=2)] == [Byte(2), Byte(4)]
```
-# Int
-## Int.abs
+# CString
+## CString.as_text
```tomo
-Int.abs : func(x: Int -> Int)
+CString.as_text : func(str: CString -> Text)
```
-Calculates the absolute value of an integer.
+Convert a C string to Text.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Int` | The integer whose absolute value is to be calculated. | -
+str | `CString` | The C string. | -
-**Return:** The absolute value of `x`.
+**Return:** The C string as a Text.
**Example:**
```tomo
-assert (-10).abs() == 10
+assert CString("Hello").as_text() == "Hello"
```
-## Int.choose
+## CString.join
```tomo
-Int.choose : func(n: Int, k: Int -> Int)
+CString.join : func(glue: CString, pieces: [CString] -> CString)
```
-Computes the binomial coefficient of the given numbers (the equivalent of `n` choose `k` in combinatorics). This is equal to `n.factorial()/(k.factorial() * (n-k).factorial())`.
+Join a list of C strings together with a separator.
Argument | Type | Description | Default
---------|------|-------------|---------
-n | `Int` | The number of things to choose from. | -
-k | `Int` | The number of things to be chosen. | -
+glue | `CString` | The C joiner used to between elements. | -
+pieces | `[CString]` | A list of C strings to join. | -
-**Return:** The binomial coefficient, equivalent to the number of ways to uniquely choose `k` objects from among `n` objects, ignoring order.
+**Return:** A C string of the joined together bits.
**Example:**
```tomo
-assert (4).choose(2) == 6
+assert CString(",").join([CString("a"), CString("b")]) == CString("a,b")
```
-## Int.clamped
+
+# Float64
+## Float64.1_PI
```tomo
-Int.clamped : func(x: Int, low: Int, high: Int -> Int)
+Float64.1_PI : Float64
```
-Returns the given number clamped between two values so that it is within that range.
+The constant $\frac{1}{\pi}$.
-Argument | Type | Description | Default
----------|------|-------------|---------
-x | `Int` | The integer to clamp. | -
-low | `Int` | The lowest value the result can take. | -
-high | `Int` | The highest value the result can take. | -
+## Float64.2_PI
-**Return:** The first argument clamped between the other two arguments.
+```tomo
+Float64.2_PI : Float64
+```
+The constant $2 \times \pi$.
-**Example:**
-```tomo
-assert (2).clamped(5, 10) == 5
+## Float64.2_SQRTPI
+```tomo
+Float64.2_SQRTPI : Float64
```
-## Int.factorial
+
+The constant $2 \times \sqrt{\pi}$.
+
+## Float64.E
```tomo
-Int.factorial : func(n: Int -> Text)
+Float64.E : Float64
```
-Computes the factorial of an integer.
+The base of the natural logarithm ($e$).
-Argument | Type | Description | Default
----------|------|-------------|---------
-n | `Int` | The integer to compute the factorial of. | -
+## Float64.INF
-**Return:** The factorial of the given integer.
+```tomo
+Float64.INF : Float64
+```
+Positive infinity.
-**Example:**
-```tomo
-assert (10).factorial() == 3628800
+## Float64.LN10
+```tomo
+Float64.LN10 : Float64
```
-## Int.get_bit
+
+The natural logarithm of 10.
+
+## Float64.LN2
```tomo
-Int.get_bit : func(i: Int, bit_index: Int -> Bool)
+Float64.LN2 : Float64
```
-In the binary representation of an integer, check whether a given bit index is set to 1 or not.
+The natural logarithm of 2.
-For fixed-size integers, the bit index must be between 1 and the number of bits in that integer (i.e. 1-64 for `Int64`). For `Int`, the bit index must be between 1 and `Int64.max`. Values outside this range will produce a runtime error.
+## Float64.LOG2E
-Argument | Type | Description | Default
----------|------|-------------|---------
-i | `Int` | The integer whose bits are being inspected. | -
-bit_index | `Int` | The index of the bit to check (1-indexed). | -
+```tomo
+Float64.LOG2E : Float64
+```
-**Return:** Whether or not the given bit index is set to 1 in the binary representation of the integer.
+The base 2 logarithm of $e$
+## Float64.PI
-**Example:**
```tomo
-assert (6).get_bit(1) == no
-assert (6).get_bit(2) == yes
-assert (6).get_bit(3) == yes
-assert (6).get_bit(4) == no
+Float64.PI : Float64
+```
+Pi ($\pi$).
+
+## Float64.PI_2
+
+```tomo
+Float64.PI_2 : Float64
```
-## Int.hex
+
+$\frac{\pi}{2}$
+
+## Float64.PI_4
```tomo
-Int.hex : func(i: Int, digits: Int = 0, uppercase: Bool = yes, prefix: Bool = yes -> Text)
+Float64.PI_4 : Float64
```
-Converts an integer to its hexadecimal representation.
+$\frac{\pi}{4}$
-Argument | Type | Description | Default
----------|------|-------------|---------
-i | `Int` | The integer to be converted. | -
-digits | `Int` | The minimum number of digits in the output string. | `0`
-uppercase | `Bool` | Whether to use uppercase letters for hexadecimal digits. | `yes`
-prefix | `Bool` | Whether to include a "0x" prefix. | `yes`
+## Float64.SQRT1_2
-**Return:** The hexadecimal string representation of the integer.
+```tomo
+Float64.SQRT1_2 : Float64
+```
+$\sqrt{\frac{1}{2}}$
+
+## Float64.SQRT2
-**Example:**
```tomo
-assert (255).hex(digits=4, uppercase=yes, prefix=yes) == "0x00FF"
+Float64.SQRT2 : Float64
+```
+
+$\sqrt{2}$
+## Float64.TAU
+
+```tomo
+Float64.TAU : Float64
```
-## Int.is_between
+
+Tau ($2 \times \pi$)
+
+## Float64.abs
```tomo
-Int.is_between : func(x: Int, low: Int, high: Int -> Bool)
+Float64.abs : func(n: Float64 -> Float64)
```
-Determines if an integer is between two numbers (inclusive).
+Calculates the absolute value of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Int` | The integer to be checked. | -
-low | `Int` | The lower bound to check (inclusive). | -
-high | `Int` | The upper bound to check (inclusive). | -
+n | `Float64` | The number whose absolute value is to be computed. | -
-**Return:** `yes` if `low <= x and x <= high`, otherwise `no`
+**Return:** The absolute value of `n`.
**Example:**
```tomo
-assert (7).is_between(1, 10) == yes
-assert (7).is_between(100, 200) == no
-assert (7).is_between(1, 7) == yes
+assert (-3.5).abs() == 3.5
```
-## Int.is_prime
+## Float64.acos
```tomo
-Int.is_prime : func(x: Int, reps: Int = 50 -> Bool)
+Float64.acos : func(x: Float64 -> Float64)
```
-Determines if an integer is a prime number.
-
-This function is _probabilistic_. With the default arguments, the chances of getting an incorrect answer are astronomically small (on the order of 10^(-30)). See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp) for more details.
+Computes the arc cosine of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Int` | The integer to be checked. | -
-reps | `Int` | The number of repetitions for primality tests. | `50`
+x | `Float64` | The number for which the arc cosine is to be calculated. | -
-**Return:** `yes` if `x` is a prime number, `no` otherwise.
+**Return:** The arc cosine of `x` in radians.
**Example:**
```tomo
-assert (7).is_prime() == yes
-assert (6).is_prime() == no
+assert (0.0).acos() == 1.5708
```
-## Int.next_prime
+## Float64.acosh
```tomo
-Int.next_prime : func(x: Int -> Int)
+Float64.acosh : func(x: Float64 -> Float64)
```
-Finds the next prime number greater than the given integer.
-
-This function is _probabilistic_, but the chances of getting an incorrect answer are astronomically small (on the order of 10^(-30)). See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp) for more details.
+Computes the inverse hyperbolic cosine of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Int` | The integer after which to find the next prime. | -
+x | `Float64` | The number for which the inverse hyperbolic cosine is to be calculated. | -
-**Return:** The next prime number greater than `x`.
+**Return:** The inverse hyperbolic cosine of `x`.
**Example:**
```tomo
-assert (11).next_prime() == 13
+assert (1.0).acosh() == 0
```
-## Int.octal
+## Float64.asin
```tomo
-Int.octal : func(i: Int, digits: Int = 0, prefix: Bool = yes -> Text)
+Float64.asin : func(x: Float64 -> Float64)
```
-Converts an integer to its octal representation.
+Computes the arc sine of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-i | `Int` | The integer to be converted. | -
-digits | `Int` | The minimum number of digits in the output string. | `0`
-prefix | `Bool` | Whether to include a "0o" prefix. | `yes`
+x | `Float64` | The number for which the arc sine is to be calculated. | -
-**Return:** The octal string representation of the integer.
+**Return:** The arc sine of `x` in radians.
**Example:**
```tomo
-assert (64).octal(digits=4, prefix=yes) == "0o0100"
+assert (0.5).asin() == 0.5236
```
-## Int.onward
+## Float64.asinh
```tomo
-Int.onward : func(first: Int, step: Int = 1 -> Text)
+Float64.asinh : func(x: Float64 -> Float64)
```
-Return an iterator that counts infinitely from the starting integer (with an optional step size).
+Computes the inverse hyperbolic sine of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-first | `Int` | The starting integer. | -
-step | `Int` | The increment step size. | `1`
+x | `Float64` | The number for which the inverse hyperbolic sine is to be calculated. | -
-**Return:** An iterator function that counts onward from the starting integer.
+**Return:** The inverse hyperbolic sine of `x`.
**Example:**
```tomo
-nums : &[Int] = &[]
-for i in (5).onward()
-nums.insert(i)
-stop if i == 10
-assert nums[] == [5, 6, 7, 8, 9, 10]
+assert (0.0).asinh() == 0
```
-## Int.parse
+## Float64.atan
```tomo
-Int.parse : func(text: Text, remainder: &Text? = none -> Int?)
+Float64.atan : func(x: Float64 -> Float64)
```
-Converts a text representation of an integer into an integer.
+Computes the arc tangent of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-text | `Text` | The text containing the integer. | -
-remainder | `&Text?` | If non-none, this argument will be set to the remainder of the text after the matching part. If none, parsing will only succeed if the entire text matches. | `none`
+x | `Float64` | The number for which the arc tangent is to be calculated. | -
-**Return:** The integer represented by the text. If the given text contains a value outside of the representable range or if the entire text can't be parsed as an integer, `none` will be returned.
+**Return:** The arc tangent of `x` in radians.
**Example:**
```tomo
-assert Int.parse("123") == 123
-assert Int.parse("0xFF") == 255
-assert Int.parse("123xyz") == none
-remainder : Text
-assert Int.parse("123xyz", &remainder) == 123
-assert remainder == "xyz"
-
-# Can't parse:
-assert Int.parse("asdf") == none
-
-# Outside valid range:
-assert Int8.parse("9999999") == none
+assert (1.0).atan() == 0.7854
```
-## Int.prev_prime
+## Float64.atan2
```tomo
-Int.prev_prime : func(x: Int -> Int?)
+Float64.atan2 : func(x: Float64, y: Float64 -> Float64)
```
-Finds the previous prime number less than the given integer. If there is no previous prime number (i.e. if a number less than `2` is provided), then the function will create a runtime error.
-
-This function is _probabilistic_, but the chances of getting an incorrect answer are astronomically small (on the order of 10^(-30)). See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp) for more details.
+Computes the arc tangent of the quotient of two numbers.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Int` | The integer before which to find the previous prime. | -
+x | `Float64` | The numerator. | -
+y | `Float64` | The denominator. | -
-**Return:** The previous prime number less than `x`, or `none` if `x` is less than 2.
+**Return:** The arc tangent of `x/y` in radians.
**Example:**
```tomo
-assert (11).prev_prime() == 7
+assert Float64.atan2(1, 1) == 0.7854
```
-## Int.sqrt
+## Float64.atanh
```tomo
-Int.sqrt : func(x: Int -> Int)
+Float64.atanh : func(x: Float64 -> Float64)
```
-Calculates the nearest square root of an integer.
+Computes the inverse hyperbolic tangent of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Int` | The integer whose square root is to be calculated. | -
+x | `Float64` | The number for which the inverse hyperbolic tangent is to be calculated. | -
-**Return:** The integer part of the square root of `x`.
+**Return:** The inverse hyperbolic tangent of `x`.
**Example:**
```tomo
-assert (16).sqrt() == 4
-assert (17).sqrt() == 4
+assert (0.5).atanh() == 0.5493
```
-## Int.to
+## Float64.cbrt
```tomo
-Int.to : func(first: Int, last: Int, step: Int? = none -> func(->Int?))
+Float64.cbrt : func(x: Float64 -> Float64)
```
-Returns an iterator function that iterates over the range of numbers specified.
+Computes the cube root of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-first | `Int` | The starting value of the range. | -
-last | `Int` | The ending value of the range. | -
-step | `Int?` | An optional step size to use. If unspecified or `none`, the step will be inferred to be `+1` if `last >= first`, otherwise `-1`. | `none`
+x | `Float64` | The number for which the cube root is to be calculated. | -
-**Return:** An iterator function that returns each integer in the given range (inclusive).
+**Return:** The cube root of `x`.
**Example:**
```tomo
-iter := (2).to(5)
-assert iter() == 2
-assert iter() == 3
-assert iter() == 4
-assert iter() == 5
-assert iter() == none
-
-assert [x for x in (2).to(5)] == [2, 3, 4, 5]
-assert [x for x in (5).to(2)] == [5, 4, 3, 2]
-assert [x for x in (2).to(5, step=2)] == [2, 4]
+assert (27.0).cbrt() == 3
```
-
-# List
-## List.binary_search
+## Float64.ceil
```tomo
-List.binary_search : func(list: [T], by: func(x,y:&T->Int32) = T.compare -> Int)
+Float64.ceil : func(x: Float64 -> Float64)
```
-Performs a binary search on a sorted list.
+Rounds a number up to the nearest integer.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The sorted list to search. | -
-by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
+x | `Float64` | The number to be rounded up. | -
-**Return:** Assuming the input list is sorted according to the given comparison function, return the index where the given item would be inserted to maintain the sorted order. That is, if the item is found, return its index, otherwise return the place where it would be found if it were inserted and the list were sorted.
+**Return:** The smallest integer greater than or equal to `x`.
**Example:**
```tomo
-assert [1, 3, 5, 7, 9].binary_search(5) == 3
-assert [1, 3, 5, 7, 9].binary_search(-999) == 1
-assert [1, 3, 5, 7, 9].binary_search(999) == 6
+assert (3.2).ceil() == 4
```
-## List.by
+## Float64.clamped
```tomo
-List.by : func(list: [T], step: Int -> [T])
+Float64.clamped : func(x: Float64, low: Float64, high: Float64 -> Float64)
```
-Creates a new list with elements spaced by the specified step value.
+Returns the given number clamped between two values so that it is within that range.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The original list. | -
-step | `Int` | The step value for selecting elements. | -
+x | `Float64` | The number to clamp. | -
+low | `Float64` | The lowest value the result can take. | -
+high | `Float64` | The highest value the result can take. | -
-**Return:** A new list with every `step`-th element from the original list.
+**Return:** The first argument clamped between the other two arguments.
**Example:**
```tomo
-assert [1, 2, 3, 4, 5, 6].by(2) == [1, 3, 5]
+assert (2.5).clamped(5.5, 10.5) == 5.5
```
-## List.clear
+## Float64.copysign
```tomo
-List.clear : func(list: @[T] -> Void)
+Float64.copysign : func(x: Float64, y: Float64 -> Float64)
```
-Clears all elements from the list.
+Copies the sign of one number to another.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list to be cleared. | -
+x | `Float64` | The number whose magnitude will be copied. | -
+y | `Float64` | The number whose sign will be copied. | -
-**Return:** Nothing.
+**Return:** A number with the magnitude of `x` and the sign of `y`.
**Example:**
```tomo
-my_list.clear()
+assert (3.0).copysign(-1) == -3
```
-## List.counts
+## Float64.cos
```tomo
-List.counts : func(list: [T] -> {T=Int})
+Float64.cos : func(x: Float64 -> Float64)
```
-Counts the occurrences of each element in the list.
+Computes the cosine of a number (angle in radians).
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to count elements in. | -
+x | `Float64` | The angle in radians. | -
-**Return:** A table mapping each element to its count.
+**Return:** The cosine of `x`.
**Example:**
```tomo
-assert [10, 20, 30, 30, 30].counts() == {10=1, 20=1, 30=3}
+assert (0.0).cos() == 1
```
-## List.find
+## Float64.cosh
```tomo
-List.find : func(list: [T], target: T -> Int?)
+Float64.cosh : func(x: Float64 -> Float64)
```
-Finds the index of the first occurrence of an element (if any).
+Computes the hyperbolic cosine of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to search through. | -
-target | `T` | The item to search for. | -
+x | `Float64` | The number for which the hyperbolic cosine is to be calculated. | -
-**Return:** The index of the first occurrence or `none` if not found.
+**Return:** The hyperbolic cosine of `x`.
**Example:**
```tomo
-assert [10, 20, 30, 40, 50].find(20) == 2
-assert [10, 20, 30, 40, 50].find(9999) == none
+assert (0.0).cosh() == 1
```
-## List.from
+## Float64.erf
```tomo
-List.from : func(list: [T], first: Int -> [T])
+Float64.erf : func(x: Float64 -> Float64)
```
-Returns a slice of the list starting from a specified index.
+Computes the error function of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The original list. | -
-first | `Int` | The index to start from. | -
+x | `Float64` | The number for which the error function is to be calculated. | -
-**Return:** A new list starting from the specified index.
+**Return:** The error function of `x`.
**Example:**
```tomo
-assert [10, 20, 30, 40, 50].from(3) == [30, 40, 50]
+assert (0.0).erf() == 0
```
-## List.has
+## Float64.erfc
```tomo
-List.has : func(list: [T], target: T -> Bool)
+Float64.erfc : func(x: Float64 -> Float64)
```
-Checks if the list has an element.
+Computes the complementary error function of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to check. | -
-target | `T` | The element to check for. | -
+x | `Float64` | The number for which the complementary error function is to be calculated. | -
-**Return:** `yes` if the list has the element, `no` otherwise.
+**Return:** The complementary error function of `x`.
**Example:**
```tomo
-assert [10, 20, 30].has(20) == yes
+assert (0.0).erfc() == 1
```
-## List.heap_pop
+## Float64.exp
```tomo
-List.heap_pop : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> T?)
+Float64.exp : func(x: Float64 -> Float64)
```
-Removes and returns the top element of a heap or `none` if the list is empty. By default, this is the *minimum* value in the heap.
+Computes the exponential function $e^x$ for a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the heap. | -
-by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
+x | `Float64` | The exponent. | -
-**Return:** The removed top element of the heap or `none` if the list is empty.
+**Return:** The value of $e^x$.
**Example:**
```tomo
-my_heap := [30, 10, 20]
-my_heap.heapify()
-assert my_heap.heap_pop() == 10
+assert (1.0).exp() == 2.7183
```
-## List.heap_push
+## Float64.exp2
```tomo
-List.heap_push : func(list: @[T], item: T, by = T.compare -> Void)
+Float64.exp2 : func(x: Float64 -> Float64)
```
-Adds an element to the heap and maintains the heap property. By default, this is a *minimum* heap.
+Computes $2^x$ for a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the heap. | -
-item | `T` | The item to be added. | -
-by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
+x | `Float64` | The exponent. | -
-**Return:** Nothing.
+**Return:** The value of $2^x$.
**Example:**
```tomo
-my_heap.heap_push(10)
+assert (3.0).exp2() == 8
```
-## List.heapify
+## Float64.expm1
```tomo
-List.heapify : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> Void)
+Float64.expm1 : func(x: Float64 -> Float64)
```
-Converts a list into a heap.
+Computes $e^x - 1$ for a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list to be heapified. | -
-by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
+x | `Float64` | The exponent. | -
-**Return:** Nothing.
+**Return:** The value of $e^x - 1$.
**Example:**
```tomo
-my_heap := [30, 10, 20]
-my_heap.heapify()
+assert (1.0).expm1() == 1.7183
```
-## List.insert
+## Float64.fdim
```tomo
-List.insert : func(list: @[T], item: T, at: Int = 0 -> Void)
+Float64.fdim : func(x: Float64, y: Float64 -> Float64)
```
-Inserts an element at a specified position in the list.
-
-Since indices are 1-indexed and negative indices mean "starting from the back", an index of `0` means "after the last item".
+Computes the positive difference between two numbers.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list. | -
-item | `T` | The item to be inserted. | -
-at | `Int` | The index at which to insert the item. | `0`
+x | `Float64` | The first number. | -
+y | `Float64` | The second number. | -
-**Return:** Nothing.
+**Return:** The positive difference $\max(0, x - y)$.
**Example:**
```tomo
-list := [10, 20]
-list.insert(30)
-assert list == [10, 20, 30]
+fd
-list.insert(999, at=2)
-assert list == [10, 999, 20, 30]
+assert (5.0).fdim(3) == 2
```
-## List.insert_all
+## Float64.floor
```tomo
-List.insert_all : func(list: @[T], items: [T], at: Int = 0 -> Void)
+Float64.floor : func(x: Float64 -> Float64)
```
-Inserts a list of items at a specified position in the list.
-
-Since indices are 1-indexed and negative indices mean "starting from the back", an index of `0` means "after the last item".
+Rounds a number down to the nearest integer.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list. | -
-items | `[T]` | The items to be inserted. | -
-at | `Int` | The index at which to insert the item. | `0`
+x | `Float64` | The number to be rounded down. | -
-**Return:** Nothing.
+**Return:** The largest integer less than or equal to `x`.
**Example:**
```tomo
-list := [10, 20]
-list.insert_all([30, 40])
-assert list == [10, 20, 30, 40]
-
-list.insert_all([99, 100], at=2)
-assert list == [10, 99, 100, 20, 30, 40]
+assert (3.7).floor() == 3
```
-## List.pop
+## Float64.hypot
```tomo
-List.pop : func(list: &[T], index: Int = -1 -> T?)
+Float64.hypot : func(x: Float64, y: Float64 -> Float64)
```
-Removes and returns an item from the list. If the given index is present in the list, the item at that index will be removed and the list will become one element shorter.
-
-Since negative indices are counted from the back, the default behavior is to pop the last value.
+Computes the Euclidean norm, $\sqrt{x^2 + y^2}$, of two numbers.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `&[T]` | The list to remove an item from. | -
-index | `Int` | The index from which to remove the item. | `-1`
+x | `Float64` | The first number. | -
+y | `Float64` | The second number. | -
-**Return:** `none` if the list is empty or the given index does not exist in the list, otherwise the item at the given index.
+**Return:** The Euclidean norm of `x` and `y`.
**Example:**
```tomo
-list := &[10, 20, 30, 40]
-
-assert list.pop() == 40
-assert list[] == [10, 20, 30]
-
-assert list.pop(index=2) == 20
-assert list[] == [10, 30]
+assert Float64.hypot(3, 4) == 5
```
-## List.random
+## Float64.is_between
```tomo
-List.random : func(list: [T], random: func(min,max:Int64->Int64)? = none -> T)
+Float64.is_between : func(x: Float64, low: Float64, high: Float64 -> Bool)
```
-Selects a random element from the list.
+Determines if a number is between two numbers (inclusive).
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list from which to select a random element. | -
-random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`
+x | `Float64` | The integer to be checked. | -
+low | `Float64` | The lower bound to check (inclusive). | -
+high | `Float64` | The upper bound to check (inclusive). | -
-**Return:** A random element from the list.
+**Return:** `yes` if `low <= x and x <= high`, otherwise `no`
**Example:**
```tomo
-assert [10, 20, 30].random() == 20
+assert (7.5).is_between(1, 10) == yes
+assert (7.5).is_between(100, 200) == no
+assert (7.5).is_between(1, 7.5) == yes
```
-## List.remove_at
+## Float64.isfinite
```tomo
-List.remove_at : func(list: @[T], at: Int = -1, count: Int = 1 -> Void)
+Float64.isfinite : func(n: Float64 -> Bool)
```
-Removes elements from the list starting at a specified index.
-
-Since negative indices are counted from the back, the default behavior is to remove the last item.
+Checks if a number is finite.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list. | -
-at | `Int` | The index at which to start removing elements. | `-1`
-count | `Int` | The number of elements to remove. | `1`
+n | `Float64` | The number to be checked. | -
-**Return:** Nothing.
+**Return:** `yes` if `n` is finite, `no` otherwise.
**Example:**
```tomo
-list := [10, 20, 30, 40, 50]
-list.remove_at(2)
-assert list == [10, 30, 40, 50]
-
-list.remove_at(2, count=2)
-assert list == [10, 50]
+assert (1.0).isfinite() == yes
+assert Float64.INF.isfinite() == no
```
-## List.remove_item
+## Float64.isinf
```tomo
-List.remove_item : func(list: @[T], item: T, max_count: Int = -1 -> Void)
+Float64.isinf : func(n: Float64 -> Bool)
```
-Removes all occurrences of a specified item from the list.
-
-A negative `max_count` means "remove all occurrences".
+Checks if a number is infinite.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list. | -
-item | `T` | The item to be removed. | -
-max_count | `Int` | The maximum number of occurrences to remove. | `-1`
+n | `Float64` | The number to be checked. | -
-**Return:** Nothing.
+**Return:** `yes` if `n` is infinite, `no` otherwise.
**Example:**
```tomo
-list := [10, 20, 10, 20, 30]
-list.remove_item(10)
-assert list == [20, 20, 30]
-
-list.remove_item(20, max_count=1)
-assert list == [20, 30]
+assert Float64.INF.isinf() == yes
+assert (1.0).isinf() == no
```
-## List.reversed
+## Float64.j0
```tomo
-List.reversed : func(list: [T] -> [T])
+Float64.j0 : func(x: Float64 -> Float64)
```
-Returns a reversed slice of the list.
+Computes the Bessel function of the first kind of order 0.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to be reversed. | -
+x | `Float64` | The number for which the Bessel function is to be calculated. | -
-**Return:** A slice of the list with elements in reverse order.
+**Return:** The Bessel function of the first kind of order 0 of `x`.
**Example:**
```tomo
-assert [10, 20, 30].reversed() == [30, 20, 10]
+assert (0.0).j0() == 1
```
-## List.sample
+## Float64.j1
```tomo
-List.sample : func(list: [T], count: Int, weights: [Num]? = none, random: func(->Num)? = none -> [T])
+Float64.j1 : func(x: Float64 -> Float64)
```
-Selects a sample of elements from the list, optionally with weighted probabilities.
-
-Errors will be raised if any of the following conditions occurs: - The given list has no elements and `count >= 1` - `count < 0` (negative count) - The number of weights provided doesn't match the length of the list. - Any weight in the weights list is negative, infinite, or `NaN` - The sum of the given weights is zero (zero probability for every element).
+Computes the Bessel function of the first kind of order 1.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to sample from. | -
-count | `Int` | The number of elements to sample. | -
-weights | `[Num]?` | The probability weights for each element in the list. These values do not need to add up to any particular number, they are relative weights. If no weights are given, elements will be sampled with uniform probability. | `none`
-random | `func(->Num)?` | If provided, this function will be used to get random values for sampling the list. The provided function should return random numbers between `0.0` (inclusive) and `1.0` (exclusive). (Used for deterministic pseudorandom number generation) | `none`
+x | `Float64` | The number for which the Bessel function is to be calculated. | -
-**Return:** A list of sampled elements from the list.
+**Return:** The Bessel function of the first kind of order 1 of `x`.
**Example:**
```tomo
-assert [10, 20, 30].sample(2, weights=[90%, 5%, 5%]) == [10, 10]
+assert (0.0).j1() == 0
```
-## List.shuffle
+## Float64.log
```tomo
-List.shuffle : func(list: @[T], random: func(min,max:Int64->Int64)? = none -> Void)
+Float64.log : func(x: Float64 -> Float64)
```
-Shuffles the elements of the list in place.
+Computes the natural logarithm (base $e$) of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list to be shuffled. | -
-random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`
+x | `Float64` | The number for which the natural logarithm is to be calculated. | -
-**Return:** Nothing.
+**Return:** The natural logarithm of `x`.
**Example:**
```tomo
-list.shuffle()
+assert Float64.E.log() == 1
```
-## List.shuffled
+## Float64.log10
```tomo
-List.shuffled : func(list: [T], random: func(min,max:Int64->Int64)? = none -> [T])
+Float64.log10 : func(x: Float64 -> Float64)
```
-Creates a new list with elements shuffled.
+Computes the base-10 logarithm of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to be shuffled. | -
-random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`
+x | `Float64` | The number for which the base-10 logarithm is to be calculated. | -
-**Return:** A new list with shuffled elements.
+**Return:** The base-10 logarithm of `x`.
**Example:**
```tomo
-assert [10, 20, 30, 40].shuffled() == [40, 10, 30, 20]
+assert (100.0).log10() == 2
```
-## List.slice
+## Float64.log1p
```tomo
-List.slice : func(list: [T], from: Int, to: Int -> [T])
+Float64.log1p : func(x: Float64 -> Float64)
```
-Returns a slice of the list spanning the given indices (inclusive).
+Computes $\log(1 + x)$ for a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The original list. | -
-from | `Int` | The first index to include. | -
-to | `Int` | The last index to include. | -
+x | `Float64` | The number for which $\log(1 + x)$ is to be calculated. | -
-**Return:** A new list spanning the given indices. Note: negative indices are counted from the back of the list, so `-1` refers to the last element, `-2` the second-to-last, and so on.
+**Return:** The value of $\log(1 + x)$.
**Example:**
```tomo
-assert [10, 20, 30, 40, 50].slice(2, 4) == [20, 30, 40]
-assert [10, 20, 30, 40, 50].slice(-3, -2) == [30, 40]
+assert (1.0).log1p() == 0.6931
```
-## List.sort
+## Float64.log2
```tomo
-List.sort : func(list: @[T], by = T.compare -> Void)
+Float64.log2 : func(x: Float64 -> Float64)
```
-Sorts the elements of the list in place in ascending order (small to large).
+Computes the base-2 logarithm of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `@[T]` | The mutable reference to the list to be sorted. | -
-by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
+x | `Float64` | The number for which the base-2 logarithm is to be calculated. | -
-**Return:** Nothing.
+**Return:** The base-2 logarithm of `x`.
**Example:**
```tomo
-list := [40, 10, -30, 20]
-list.sort()
-assert list == [-30, 10, 20, 40]
-
-list.sort(func(a,b:&Int): a.abs() <> b.abs())
-assert list == [10, 20, -30, 40]
+assert (8.0).log2() == 3
```
-## List.sorted
+## Float64.logb
```tomo
-List.sorted : func(list: [T], by = T.compare -> [T])
+Float64.logb : func(x: Float64 -> Float64)
```
-Creates a new list with elements sorted.
+Computes the binary exponent (base-2 logarithm) of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to be sorted. | -
-by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
+x | `Float64` | The number for which the binary exponent is to be calculated. | -
-**Return:** A new list with sorted elements.
+**Return:** The binary exponent of `x`.
**Example:**
```tomo
-assert [40, 10, -30, 20].sorted() == [-30, 10, 20, 40]
-assert [40, 10, -30, 20].sorted(
- func(a,b:&Int): a.abs() <> b.abs()
-) == [10, 20, -30, 40]
+assert (8.0).logb() == 3
```
-## List.to
+## Float64.mix
```tomo
-List.to : func(list: [T], last: Int -> [T])
+Float64.mix : func(amount: Float64, x: Float64, y: Float64 -> Float64)
```
-Returns a slice of the list from the start of the original list up to a specified index (inclusive).
+Interpolates between two numbers based on a given amount.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The original list. | -
-last | `Int` | The index up to which elements should be included. | -
+amount | `Float64` | The interpolation factor (between `0` and `1`). | -
+x | `Float64` | The starting number. | -
+y | `Float64` | The ending number. | -
-**Return:** A new list containing elements from the start up to the specified index.
+**Return:** The interpolated number between `x` and `y` based on `amount`.
**Example:**
```tomo
-assert [10, 20, 30, 40, 50].to(3) == [10, 20, 30]
-assert [10, 20, 30, 40, 50].to(-2) == [10, 20, 30, 40]
+assert (0.5).mix(10, 20) == 15
+assert (0.25).mix(10, 20) == 12.5
```
-## List.unique
+## Float64.near
```tomo
-List.unique : func(list: [T] -> {T})
+Float64.near : func(x: Float64, y: Float64, ratio: Float64 = 1e-9, min_epsilon: Float64 = 1e-9 -> Bool)
```
-Returns a set of the unique elements of the list.
+Checks if two numbers are approximately equal within specified tolerances. If two numbers are within an absolute difference or the ratio between the two is small enough, they are considered near each other.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to process. | -
+x | `Float64` | The first number. | -
+y | `Float64` | The second number. | -
+ratio | `Float64` | The relative tolerance. Default is `1e-9`. | `1e-9`
+min_epsilon | `Float64` | The absolute tolerance. Default is `1e-9`. | `1e-9`
-**Return:** A set of the unique elements from the list.
+**Return:** `yes` if `x` and `y` are approximately equal within the specified tolerances, `no` otherwise.
**Example:**
```tomo
-assert [10, 20, 10, 10, 30].unique() == {10, 20, 30}
+assert (1.0).near(1.000000001) == yes
+assert (100.0).near(110, ratio=0.1) == yes
+assert (5.0).near(5.1, min_epsilon=0.1) == yes
```
-## List.where
+## Float64.nextafter
```tomo
-List.where : func(list: [T], predicate: func(item:&T -> Bool) -> Int)
+Float64.nextafter : func(x: Float64, y: Float64 -> Float64)
```
-Find the index of the first item that matches a predicate function (if any).
+Computes the next representable value after a given number towards a specified direction.
Argument | Type | Description | Default
---------|------|-------------|---------
-list | `[T]` | The list to search through. | -
-predicate | `func(item:&T -> Bool)` | A function that returns `yes` if the item's index should be returned or `no` if it should not. | -
+x | `Float64` | The starting number. | -
+y | `Float64` | The direction towards which to find the next representable value. | -
-**Return:** Returns the index of the first item where the predicate is true or `none` if no item matches.
+**Return:** The next representable value after `x` in the direction of `y`.
**Example:**
```tomo
-assert [4, 5, 6].where(func(i:&Int): i.is_prime()) == 5
-assert [4, 6, 8].find(func(i:&Int): i.is_prime()) == none
+assert (1.0).nextafter(1.1) == 1.0000000000000002
```
-
-# Num
-## Num.1_PI
+## Float64.parse
```tomo
-Num.1_PI : Num
+Float64.parse : func(text: Text, remainder: &Text? = none -> Float64?)
```
-The constant $\frac{1}{\pi}$.
-
-## Num.2_PI
+Converts a text representation of a number into a floating-point number.
-```tomo
-Num.2_PI : Num
-```
+Argument | Type | Description | Default
+---------|------|-------------|---------
+text | `Text` | The text containing the number. | -
+remainder | `&Text?` | If non-none, this argument will be set to the remainder of the text after the matching part. If none, parsing will only succeed if the entire text matches. | `none`
-The constant $2 \times \pi$.
+**Return:** The number represented by the text or `none` if the entire text can't be parsed as a number.
-## Num.2_SQRTPI
+**Example:**
```tomo
-Num.2_SQRTPI : Num
-```
-
-The constant $2 \times \sqrt{\pi}$.
+assert Float64.parse("3.14") == 3.14
+assert Float64.parse("1e3") == 1000
+assert Float64.parse("1.5junk") == none
+remainder : Text
+assert Float64.parse("1.5junk", &remainder) == 1.5
+assert remainder == "junk"
-## Num.E
+```
+## Float64.percent
```tomo
-Num.E : Num
+Float64.percent : func(n: Float64, precision: Float64 = 0.01 -> Text)
```
-The base of the natural logarithm ($e$).
-
-## Num.INF
+Convert a number into a percentage text with a percent sign.
-```tomo
-Num.INF : Num
-```
+Argument | Type | Description | Default
+---------|------|-------------|---------
+n | `Float64` | The number to be converted to a percent. | -
+precision | `Float64` | Round the percentage to this precision level. | `0.01`
-Positive infinity.
+**Return:** A text representation of the number as a percentage with a percent sign.
-## Num.LN10
+**Example:**
```tomo
-Num.LN10 : Num
-```
-
-The natural logarithm of 10.
+assert (0.5).percent() == "50%"
+assert (1./3.).percent(2) == "33.33%"
+assert (1./3.).percent(2, precision=0.0001) == "33.3333%"
+assert (1./3.).percent(2, precision=10.) == "30%"
-## Num.LN2
+```
+## Float64.rint
```tomo
-Num.LN2 : Num
+Float64.rint : func(x: Float64 -> Float64)
```
-The natural logarithm of 2.
+Rounds a number to the nearest integer, with ties rounded to the nearest even integer.
-## Num.LOG2E
+Argument | Type | Description | Default
+---------|------|-------------|---------
+x | `Float64` | The number to be rounded. | -
-```tomo
-Num.LOG2E : Num
-```
+**Return:** The nearest integer value of `x`.
-The base 2 logarithm of $e$
-## Num.PI
+**Example:**
+```tomo
+assert (3.5).rint() == 4
+assert (2.5).rint() == 2
+
+```
+## Float64.round
```tomo
-Num.PI : Num
+Float64.round : func(x: Float64 -> Float64)
```
-Pi ($\pi$).
+Rounds a number to the nearest whole number integer.
-## Num.PI_2
+Argument | Type | Description | Default
+---------|------|-------------|---------
+x | `Float64` | The number to be rounded. | -
-```tomo
-Num.PI_2 : Num
-```
+**Return:** The nearest integer value of `x`.
-$\frac{\pi}{2}$
-## Num.PI_4
+**Example:**
+```tomo
+assert (2.3).round() == 2
+assert (2.7).round() == 3
+
+```
+## Float64.significand
```tomo
-Num.PI_4 : Num
+Float64.significand : func(x: Float64 -> Float64)
```
-$\frac{\pi}{4}$
+Extracts the significand (or mantissa) of a number.
-## Num.SQRT1_2
+Argument | Type | Description | Default
+---------|------|-------------|---------
+x | `Float64` | The number from which to extract the significand. | -
-```tomo
-Num.SQRT1_2 : Num
-```
+**Return:** The significand of `x`.
-$\sqrt{\frac{1}{2}}$
-## Num.SQRT2
+**Example:**
+```tomo
+assert (1234.567).significand() == 0.1234567
+
+```
+## Float64.sin
```tomo
-Num.SQRT2 : Num
+Float64.sin : func(x: Float64 -> Float64)
```
-$\sqrt{2}$
+Computes the sine of a number (angle in radians).
-## Num.TAU
+Argument | Type | Description | Default
+---------|------|-------------|---------
+x | `Float64` | The angle in radians. | -
-```tomo
-Num.TAU : Num
-```
+**Return:** The sine of `x`.
-Tau ($2 \times \pi$)
-## Num.abs
+**Example:**
+```tomo
+assert (0.0).sin() == 0
+
+```
+## Float64.sinh
```tomo
-Num.abs : func(n: Num -> Num)
+Float64.sinh : func(x: Float64 -> Float64)
```
-Calculates the absolute value of a number.
+Computes the hyperbolic sine of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-n | `Num` | The number whose absolute value is to be computed. | -
+x | `Float64` | The number for which the hyperbolic sine is to be calculated. | -
-**Return:** The absolute value of `n`.
+**Return:** The hyperbolic sine of `x`.
**Example:**
```tomo
-assert (-3.5).abs() == 3.5
+assert (0.0).sinh() == 0
```
-## Num.acos
+## Float64.sqrt
```tomo
-Num.acos : func(x: Num -> Num)
+Float64.sqrt : func(x: Float64 -> Float64)
```
-Computes the arc cosine of a number.
+Computes the square root of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the arc cosine is to be calculated. | -
+x | `Float64` | The number for which the square root is to be calculated. | -
-**Return:** The arc cosine of `x` in radians.
+**Return:** The square root of `x`.
**Example:**
```tomo
-assert (0.0).acos() == 1.5708
+assert (16.0).sqrt() == 4
```
-## Num.acosh
+## Float64.tan
```tomo
-Num.acosh : func(x: Num -> Num)
+Float64.tan : func(x: Float64 -> Float64)
```
-Computes the inverse hyperbolic cosine of a number.
+Computes the tangent of a number (angle in radians).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the inverse hyperbolic cosine is to be calculated. | -
+x | `Float64` | The angle in radians. | -
-**Return:** The inverse hyperbolic cosine of `x`.
+**Return:** The tangent of `x`.
**Example:**
```tomo
-assert (1.0).acosh() == 0
+assert (0.0).tan() == 0
```
-## Num.asin
+## Float64.tanh
```tomo
-Num.asin : func(x: Num -> Num)
+Float64.tanh : func(x: Float64 -> Float64)
```
-Computes the arc sine of a number.
+Computes the hyperbolic tangent of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the arc sine is to be calculated. | -
+x | `Float64` | The number for which the hyperbolic tangent is to be calculated. | -
-**Return:** The arc sine of `x` in radians.
+**Return:** The hyperbolic tangent of `x`.
**Example:**
```tomo
-assert (0.5).asin() == 0.5236
+assert (0.0).tanh() == 0
```
-## Num.asinh
+## Float64.tgamma
```tomo
-Num.asinh : func(x: Num -> Num)
+Float64.tgamma : func(x: Float64 -> Float64)
```
-Computes the inverse hyperbolic sine of a number.
+Computes the gamma function of a number.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the inverse hyperbolic sine is to be calculated. | -
+x | `Float64` | The number for which the gamma function is to be calculated. | -
-**Return:** The inverse hyperbolic sine of `x`.
+**Return:** The gamma function of `x`.
**Example:**
```tomo
-assert (0.0).asinh() == 0
+assert (1.0).tgamma() == 1
```
-## Num.atan
+## Float64.trunc
```tomo
-Num.atan : func(x: Num -> Num)
+Float64.trunc : func(x: Float64 -> Float64)
```
-Computes the arc tangent of a number.
+Truncates a number to the nearest integer towards zero.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the arc tangent is to be calculated. | -
+x | `Float64` | The number to be truncated. | -
-**Return:** The arc tangent of `x` in radians.
+**Return:** The integer part of `x` towards zero.
**Example:**
```tomo
-assert (1.0).atan() == 0.7854
+assert (3.7).trunc() == 3
+assert (-3.7).trunc() == -3
```
-## Num.atan2
+## Float64.with_precision
```tomo
-Num.atan2 : func(x: Num, y: Num -> Num)
+Float64.with_precision : func(n: Float64, precision: Float64 -> Float64)
```
-Computes the arc tangent of the quotient of two numbers.
+Round a number to the given precision level (specified as `10`, `.1`, `.001` etc).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The numerator. | -
-y | `Num` | The denominator. | -
+n | `Float64` | The number to be rounded to a given precision. | -
+precision | `Float64` | The precision to which the number should be rounded. | -
-**Return:** The arc tangent of `x/y` in radians.
+**Return:** The number, rounded to the given precision level.
**Example:**
```tomo
-assert Num.atan2(1, 1) == 0.7854
+assert (0.1234567).with_precision(0.01) == 0.12
+assert (123456.).with_precision(100) == 123500
+assert (1234567.).with_precision(5) == 1234565
```
-## Num.atanh
+## Float64.y0
```tomo
-Num.atanh : func(x: Num -> Num)
+Float64.y0 : func(x: Float64 -> Float64)
```
-Computes the inverse hyperbolic tangent of a number.
+Computes the Bessel function of the second kind of order 0.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the inverse hyperbolic tangent is to be calculated. | -
+x | `Float64` | The number for which the Bessel function is to be calculated. | -
-**Return:** The inverse hyperbolic tangent of `x`.
+**Return:** The Bessel function of the second kind of order 0 of `x`.
**Example:**
```tomo
-assert (0.5).atanh() == 0.5493
+assert (1.0).y0() == -0.7652
```
-## Num.cbrt
+## Float64.y1
```tomo
-Num.cbrt : func(x: Num -> Num)
+Float64.y1 : func(x: Float64 -> Float64)
```
-Computes the cube root of a number.
+Computes the Bessel function of the second kind of order 1.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the cube root is to be calculated. | -
+x | `Float64` | The number for which the Bessel function is to be calculated. | -
-**Return:** The cube root of `x`.
+**Return:** The Bessel function of the second kind of order 1 of `x`.
**Example:**
```tomo
-assert (27.0).cbrt() == 3
+assert (1.0).y1() == 0.4401
```
-## Num.ceil
+
+# Int
+## Int.abs
```tomo
-Num.ceil : func(x: Num -> Num)
+Int.abs : func(x: Int -> Int)
```
-Rounds a number up to the nearest integer.
+Calculates the absolute value of an integer.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number to be rounded up. | -
+x | `Int` | The integer whose absolute value is to be calculated. | -
-**Return:** The smallest integer greater than or equal to `x`.
+**Return:** The absolute value of `x`.
**Example:**
```tomo
-assert (3.2).ceil() == 4
+assert (-10).abs() == 10
```
-## Num.clamped
+## Int.choose
```tomo
-Num.clamped : func(x: Num, low: Num, high: Num -> Num)
+Int.choose : func(n: Int, k: Int -> Int)
```
-Returns the given number clamped between two values so that it is within that range.
+Computes the binomial coefficient of the given numbers (the equivalent of `n` choose `k` in combinatorics). This is equal to `n.factorial()/(k.factorial() * (n-k).factorial())`.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number to clamp. | -
-low | `Num` | The lowest value the result can take. | -
-high | `Num` | The highest value the result can take. | -
+n | `Int` | The number of things to choose from. | -
+k | `Int` | The number of things to be chosen. | -
-**Return:** The first argument clamped between the other two arguments.
+**Return:** The binomial coefficient, equivalent to the number of ways to uniquely choose `k` objects from among `n` objects, ignoring order.
**Example:**
```tomo
-assert (2.5).clamped(5.5, 10.5) == 5.5
+assert (4).choose(2) == 6
```
-## Num.copysign
+## Int.clamped
```tomo
-Num.copysign : func(x: Num, y: Num -> Num)
+Int.clamped : func(x: Int, low: Int, high: Int -> Int)
```
-Copies the sign of one number to another.
+Returns the given number clamped between two values so that it is within that range.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number whose magnitude will be copied. | -
-y | `Num` | The number whose sign will be copied. | -
+x | `Int` | The integer to clamp. | -
+low | `Int` | The lowest value the result can take. | -
+high | `Int` | The highest value the result can take. | -
-**Return:** A number with the magnitude of `x` and the sign of `y`.
+**Return:** The first argument clamped between the other two arguments.
**Example:**
```tomo
-assert (3.0).copysign(-1) == -3
+assert (2).clamped(5, 10) == 5
```
-## Num.cos
+## Int.factorial
```tomo
-Num.cos : func(x: Num -> Num)
+Int.factorial : func(n: Int -> Text)
```
-Computes the cosine of a number (angle in radians).
+Computes the factorial of an integer.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The angle in radians. | -
+n | `Int` | The integer to compute the factorial of. | -
-**Return:** The cosine of `x`.
+**Return:** The factorial of the given integer.
**Example:**
```tomo
-assert (0.0).cos() == 1
+assert (10).factorial() == 3628800
```
-## Num.cosh
+## Int.get_bit
```tomo
-Num.cosh : func(x: Num -> Num)
+Int.get_bit : func(i: Int, bit_index: Int -> Bool)
```
-Computes the hyperbolic cosine of a number.
+In the binary representation of an integer, check whether a given bit index is set to 1 or not.
+
+For fixed-size integers, the bit index must be between 1 and the number of bits in that integer (i.e. 1-64 for `Int64`). For `Int`, the bit index must be between 1 and `Int64.max`. Values outside this range will produce a runtime error.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the hyperbolic cosine is to be calculated. | -
+i | `Int` | The integer whose bits are being inspected. | -
+bit_index | `Int` | The index of the bit to check (1-indexed). | -
-**Return:** The hyperbolic cosine of `x`.
+**Return:** Whether or not the given bit index is set to 1 in the binary representation of the integer.
**Example:**
```tomo
-assert (0.0).cosh() == 1
+assert (6).get_bit(1) == no
+assert (6).get_bit(2) == yes
+assert (6).get_bit(3) == yes
+assert (6).get_bit(4) == no
```
-## Num.erf
+## Int.hex
```tomo
-Num.erf : func(x: Num -> Num)
+Int.hex : func(i: Int, digits: Int = 0, uppercase: Bool = yes, prefix: Bool = yes -> Text)
```
-Computes the error function of a number.
+Converts an integer to its hexadecimal representation.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the error function is to be calculated. | -
+i | `Int` | The integer to be converted. | -
+digits | `Int` | The minimum number of digits in the output string. | `0`
+uppercase | `Bool` | Whether to use uppercase letters for hexadecimal digits. | `yes`
+prefix | `Bool` | Whether to include a "0x" prefix. | `yes`
-**Return:** The error function of `x`.
+**Return:** The hexadecimal string representation of the integer.
**Example:**
```tomo
-assert (0.0).erf() == 0
+assert (255).hex(digits=4, uppercase=yes, prefix=yes) == "0x00FF"
```
-## Num.erfc
+## Int.is_between
```tomo
-Num.erfc : func(x: Num -> Num)
+Int.is_between : func(x: Int, low: Int, high: Int -> Bool)
```
-Computes the complementary error function of a number.
+Determines if an integer is between two numbers (inclusive).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the complementary error function is to be calculated. | -
+x | `Int` | The integer to be checked. | -
+low | `Int` | The lower bound to check (inclusive). | -
+high | `Int` | The upper bound to check (inclusive). | -
-**Return:** The complementary error function of `x`.
+**Return:** `yes` if `low <= x and x <= high`, otherwise `no`
**Example:**
```tomo
-assert (0.0).erfc() == 1
+assert (7).is_between(1, 10) == yes
+assert (7).is_between(100, 200) == no
+assert (7).is_between(1, 7) == yes
```
-## Num.exp
+## Int.is_prime
```tomo
-Num.exp : func(x: Num -> Num)
+Int.is_prime : func(x: Int, reps: Int = 50 -> Bool)
```
-Computes the exponential function $e^x$ for a number.
+Determines if an integer is a prime number.
+
+This function is _probabilistic_. With the default arguments, the chances of getting an incorrect answer are astronomically small (on the order of 10^(-30)). See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp) for more details.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The exponent. | -
+x | `Int` | The integer to be checked. | -
+reps | `Int` | The number of repetitions for primality tests. | `50`
-**Return:** The value of $e^x$.
+**Return:** `yes` if `x` is a prime number, `no` otherwise.
**Example:**
```tomo
-assert (1.0).exp() == 2.7183
+assert (7).is_prime() == yes
+assert (6).is_prime() == no
```
-## Num.exp2
+## Int.next_prime
```tomo
-Num.exp2 : func(x: Num -> Num)
+Int.next_prime : func(x: Int -> Int)
```
-Computes $2^x$ for a number.
+Finds the next prime number greater than the given integer.
+
+This function is _probabilistic_, but the chances of getting an incorrect answer are astronomically small (on the order of 10^(-30)). See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp) for more details.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The exponent. | -
+x | `Int` | The integer after which to find the next prime. | -
-**Return:** The value of $2^x$.
+**Return:** The next prime number greater than `x`.
**Example:**
```tomo
-assert (3.0).exp2() == 8
+assert (11).next_prime() == 13
```
-## Num.expm1
+## Int.octal
```tomo
-Num.expm1 : func(x: Num -> Num)
+Int.octal : func(i: Int, digits: Int = 0, prefix: Bool = yes -> Text)
```
-Computes $e^x - 1$ for a number.
+Converts an integer to its octal representation.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The exponent. | -
+i | `Int` | The integer to be converted. | -
+digits | `Int` | The minimum number of digits in the output string. | `0`
+prefix | `Bool` | Whether to include a "0o" prefix. | `yes`
-**Return:** The value of $e^x - 1$.
+**Return:** The octal string representation of the integer.
**Example:**
```tomo
-assert (1.0).expm1() == 1.7183
+assert (64).octal(digits=4, prefix=yes) == "0o0100"
```
-## Num.fdim
+## Int.onward
```tomo
-Num.fdim : func(x: Num, y: Num -> Num)
+Int.onward : func(first: Int, step: Int = 1 -> Text)
```
-Computes the positive difference between two numbers.
+Return an iterator that counts infinitely from the starting integer (with an optional step size).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The first number. | -
-y | `Num` | The second number. | -
+first | `Int` | The starting integer. | -
+step | `Int` | The increment step size. | `1`
-**Return:** The positive difference $\max(0, x - y)$.
+**Return:** An iterator function that counts onward from the starting integer.
**Example:**
```tomo
-fd
-
-assert (5.0).fdim(3) == 2
+nums : &[Int] = &[]
+for i in (5).onward()
+nums.insert(i)
+stop if i == 10
+assert nums[] == [5, 6, 7, 8, 9, 10]
```
-## Num.floor
+## Int.parse
```tomo
-Num.floor : func(x: Num -> Num)
+Int.parse : func(text: Text, remainder: &Text? = none -> Int?)
```
-Rounds a number down to the nearest integer.
+Converts a text representation of an integer into an integer.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number to be rounded down. | -
+text | `Text` | The text containing the integer. | -
+remainder | `&Text?` | If non-none, this argument will be set to the remainder of the text after the matching part. If none, parsing will only succeed if the entire text matches. | `none`
-**Return:** The largest integer less than or equal to `x`.
+**Return:** The integer represented by the text. If the given text contains a value outside of the representable range or if the entire text can't be parsed as an integer, `none` will be returned.
**Example:**
```tomo
-assert (3.7).floor() == 3
+assert Int.parse("123") == 123
+assert Int.parse("0xFF") == 255
+assert Int.parse("123xyz") == none
+remainder : Text
+assert Int.parse("123xyz", &remainder) == 123
+assert remainder == "xyz"
+
+# Can't parse:
+assert Int.parse("asdf") == none
+
+# Outside valid range:
+assert Int8.parse("9999999") == none
```
-## Num.hypot
+## Int.prev_prime
```tomo
-Num.hypot : func(x: Num, y: Num -> Num)
+Int.prev_prime : func(x: Int -> Int?)
```
-Computes the Euclidean norm, $\sqrt{x^2 + y^2}$, of two numbers.
+Finds the previous prime number less than the given integer. If there is no previous prime number (i.e. if a number less than `2` is provided), then the function will create a runtime error.
+
+This function is _probabilistic_, but the chances of getting an incorrect answer are astronomically small (on the order of 10^(-30)). See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp) for more details.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The first number. | -
-y | `Num` | The second number. | -
+x | `Int` | The integer before which to find the previous prime. | -
-**Return:** The Euclidean norm of `x` and `y`.
+**Return:** The previous prime number less than `x`, or `none` if `x` is less than 2.
**Example:**
```tomo
-assert Num.hypot(3, 4) == 5
+assert (11).prev_prime() == 7
```
-## Num.is_between
+## Int.sqrt
```tomo
-Num.is_between : func(x: Num, low: Num, high: Num -> Bool)
+Int.sqrt : func(x: Int -> Int)
```
-Determines if a number is between two numbers (inclusive).
+Calculates the nearest square root of an integer.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The integer to be checked. | -
-low | `Num` | The lower bound to check (inclusive). | -
-high | `Num` | The upper bound to check (inclusive). | -
+x | `Int` | The integer whose square root is to be calculated. | -
-**Return:** `yes` if `low <= x and x <= high`, otherwise `no`
+**Return:** The integer part of the square root of `x`.
**Example:**
```tomo
-assert (7.5).is_between(1, 10) == yes
-assert (7.5).is_between(100, 200) == no
-assert (7.5).is_between(1, 7.5) == yes
+assert (16).sqrt() == 4
+assert (17).sqrt() == 4
```
-## Num.isfinite
+## Int.to
```tomo
-Num.isfinite : func(n: Num -> Bool)
+Int.to : func(first: Int, last: Int, step: Int? = none -> func(->Int?))
```
-Checks if a number is finite.
+Returns an iterator function that iterates over the range of numbers specified.
Argument | Type | Description | Default
---------|------|-------------|---------
-n | `Num` | The number to be checked. | -
+first | `Int` | The starting value of the range. | -
+last | `Int` | The ending value of the range. | -
+step | `Int?` | An optional step size to use. If unspecified or `none`, the step will be inferred to be `+1` if `last >= first`, otherwise `-1`. | `none`
-**Return:** `yes` if `n` is finite, `no` otherwise.
+**Return:** An iterator function that returns each integer in the given range (inclusive).
**Example:**
```tomo
-assert (1.0).isfinite() == yes
-assert Num.INF.isfinite() == no
+iter := (2).to(5)
+assert iter() == 2
+assert iter() == 3
+assert iter() == 4
+assert iter() == 5
+assert iter() == none
+
+assert [x for x in (2).to(5)] == [2, 3, 4, 5]
+assert [x for x in (5).to(2)] == [5, 4, 3, 2]
+assert [x for x in (2).to(5, step=2)] == [2, 4]
```
-## Num.isinf
+
+# List
+## List.binary_search
```tomo
-Num.isinf : func(n: Num -> Bool)
+List.binary_search : func(list: [T], by: func(x,y:&T->Int32) = T.compare -> Int)
```
-Checks if a number is infinite.
+Performs a binary search on a sorted list.
Argument | Type | Description | Default
---------|------|-------------|---------
-n | `Num` | The number to be checked. | -
+list | `[T]` | The sorted list to search. | -
+by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
-**Return:** `yes` if `n` is infinite, `no` otherwise.
+**Return:** Assuming the input list is sorted according to the given comparison function, return the index where the given item would be inserted to maintain the sorted order. That is, if the item is found, return its index, otherwise return the place where it would be found if it were inserted and the list were sorted.
**Example:**
```tomo
-assert Num.INF.isinf() == yes
-assert (1.0).isinf() == no
+assert [1, 3, 5, 7, 9].binary_search(5) == 3
+assert [1, 3, 5, 7, 9].binary_search(-999) == 1
+assert [1, 3, 5, 7, 9].binary_search(999) == 6
```
-## Num.j0
+## List.by
```tomo
-Num.j0 : func(x: Num -> Num)
+List.by : func(list: [T], step: Int -> [T])
```
-Computes the Bessel function of the first kind of order 0.
+Creates a new list with elements spaced by the specified step value.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the Bessel function is to be calculated. | -
+list | `[T]` | The original list. | -
+step | `Int` | The step value for selecting elements. | -
-**Return:** The Bessel function of the first kind of order 0 of `x`.
+**Return:** A new list with every `step`-th element from the original list.
**Example:**
```tomo
-assert (0.0).j0() == 1
+assert [1, 2, 3, 4, 5, 6].by(2) == [1, 3, 5]
```
-## Num.j1
+## List.clear
```tomo
-Num.j1 : func(x: Num -> Num)
+List.clear : func(list: @[T] -> Void)
```
-Computes the Bessel function of the first kind of order 1.
+Clears all elements from the list.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the Bessel function is to be calculated. | -
+list | `@[T]` | The mutable reference to the list to be cleared. | -
-**Return:** The Bessel function of the first kind of order 1 of `x`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (0.0).j1() == 0
+my_list.clear()
```
-## Num.log
+## List.counts
```tomo
-Num.log : func(x: Num -> Num)
+List.counts : func(list: [T] -> {T=Int})
```
-Computes the natural logarithm (base $e$) of a number.
+Counts the occurrences of each element in the list.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the natural logarithm is to be calculated. | -
+list | `[T]` | The list to count elements in. | -
-**Return:** The natural logarithm of `x`.
+**Return:** A table mapping each element to its count.
**Example:**
```tomo
-assert Num.E.log() == 1
+assert [10, 20, 30, 30, 30].counts() == {10=1, 20=1, 30=3}
```
-## Num.log10
+## List.find
```tomo
-Num.log10 : func(x: Num -> Num)
+List.find : func(list: [T], target: T -> Int?)
```
-Computes the base-10 logarithm of a number.
+Finds the index of the first occurrence of an element (if any).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the base-10 logarithm is to be calculated. | -
+list | `[T]` | The list to search through. | -
+target | `T` | The item to search for. | -
-**Return:** The base-10 logarithm of `x`.
+**Return:** The index of the first occurrence or `none` if not found.
**Example:**
```tomo
-assert (100.0).log10() == 2
+assert [10, 20, 30, 40, 50].find(20) == 2
+assert [10, 20, 30, 40, 50].find(9999) == none
```
-## Num.log1p
+## List.from
```tomo
-Num.log1p : func(x: Num -> Num)
+List.from : func(list: [T], first: Int -> [T])
```
-Computes $\log(1 + x)$ for a number.
+Returns a slice of the list starting from a specified index.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which $\log(1 + x)$ is to be calculated. | -
+list | `[T]` | The original list. | -
+first | `Int` | The index to start from. | -
-**Return:** The value of $\log(1 + x)$.
+**Return:** A new list starting from the specified index.
**Example:**
```tomo
-assert (1.0).log1p() == 0.6931
+assert [10, 20, 30, 40, 50].from(3) == [30, 40, 50]
```
-## Num.log2
+## List.has
```tomo
-Num.log2 : func(x: Num -> Num)
+List.has : func(list: [T], target: T -> Bool)
```
-Computes the base-2 logarithm of a number.
+Checks if the list has an element.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the base-2 logarithm is to be calculated. | -
+list | `[T]` | The list to check. | -
+target | `T` | The element to check for. | -
-**Return:** The base-2 logarithm of `x`.
+**Return:** `yes` if the list has the element, `no` otherwise.
**Example:**
```tomo
-assert (8.0).log2() == 3
+assert [10, 20, 30].has(20) == yes
```
-## Num.logb
+## List.heap_pop
```tomo
-Num.logb : func(x: Num -> Num)
+List.heap_pop : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> T?)
```
-Computes the binary exponent (base-2 logarithm) of a number.
+Removes and returns the top element of a heap or `none` if the list is empty. By default, this is the *minimum* value in the heap.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the binary exponent is to be calculated. | -
+list | `@[T]` | The mutable reference to the heap. | -
+by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
-**Return:** The binary exponent of `x`.
+**Return:** The removed top element of the heap or `none` if the list is empty.
**Example:**
```tomo
-assert (8.0).logb() == 3
+my_heap := [30, 10, 20]
+my_heap.heapify()
+assert my_heap.heap_pop() == 10
```
-## Num.mix
+## List.heap_push
```tomo
-Num.mix : func(amount: Num, x: Num, y: Num -> Num)
+List.heap_push : func(list: @[T], item: T, by = T.compare -> Void)
```
-Interpolates between two numbers based on a given amount.
+Adds an element to the heap and maintains the heap property. By default, this is a *minimum* heap.
Argument | Type | Description | Default
---------|------|-------------|---------
-amount | `Num` | The interpolation factor (between `0` and `1`). | -
-x | `Num` | The starting number. | -
-y | `Num` | The ending number. | -
+list | `@[T]` | The mutable reference to the heap. | -
+item | `T` | The item to be added. | -
+by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
-**Return:** The interpolated number between `x` and `y` based on `amount`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (0.5).mix(10, 20) == 15
-assert (0.25).mix(10, 20) == 12.5
+my_heap.heap_push(10)
```
-## Num.near
+## List.heapify
```tomo
-Num.near : func(x: Num, y: Num, ratio: Num = 1e-9, min_epsilon: Num = 1e-9 -> Bool)
+List.heapify : func(list: @[T], by: func(x,y:&T->Int32) = T.compare -> Void)
```
-Checks if two numbers are approximately equal within specified tolerances. If two numbers are within an absolute difference or the ratio between the two is small enough, they are considered near each other.
+Converts a list into a heap.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The first number. | -
-y | `Num` | The second number. | -
-ratio | `Num` | The relative tolerance. Default is `1e-9`. | `1e-9`
-min_epsilon | `Num` | The absolute tolerance. Default is `1e-9`. | `1e-9`
+list | `@[T]` | The mutable reference to the list to be heapified. | -
+by | `func(x,y:&T->Int32)` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
-**Return:** `yes` if `x` and `y` are approximately equal within the specified tolerances, `no` otherwise.
+**Return:** Nothing.
**Example:**
```tomo
-assert (1.0).near(1.000000001) == yes
-assert (100.0).near(110, ratio=0.1) == yes
-assert (5.0).near(5.1, min_epsilon=0.1) == yes
+my_heap := [30, 10, 20]
+my_heap.heapify()
```
-## Num.nextafter
+## List.insert
```tomo
-Num.nextafter : func(x: Num, y: Num -> Num)
+List.insert : func(list: @[T], item: T, at: Int = 0 -> Void)
```
-Computes the next representable value after a given number towards a specified direction.
+Inserts an element at a specified position in the list.
+
+Since indices are 1-indexed and negative indices mean "starting from the back", an index of `0` means "after the last item".
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The starting number. | -
-y | `Num` | The direction towards which to find the next representable value. | -
+list | `@[T]` | The mutable reference to the list. | -
+item | `T` | The item to be inserted. | -
+at | `Int` | The index at which to insert the item. | `0`
-**Return:** The next representable value after `x` in the direction of `y`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (1.0).nextafter(1.1) == 1.0000000000000002
+list := [10, 20]
+list.insert(30)
+assert list == [10, 20, 30]
+
+list.insert(999, at=2)
+assert list == [10, 999, 20, 30]
```
-## Num.parse
+## List.insert_all
```tomo
-Num.parse : func(text: Text, remainder: &Text? = none -> Num?)
+List.insert_all : func(list: @[T], items: [T], at: Int = 0 -> Void)
```
-Converts a text representation of a number into a floating-point number.
+Inserts a list of items at a specified position in the list.
+
+Since indices are 1-indexed and negative indices mean "starting from the back", an index of `0` means "after the last item".
Argument | Type | Description | Default
---------|------|-------------|---------
-text | `Text` | The text containing the number. | -
-remainder | `&Text?` | If non-none, this argument will be set to the remainder of the text after the matching part. If none, parsing will only succeed if the entire text matches. | `none`
+list | `@[T]` | The mutable reference to the list. | -
+items | `[T]` | The items to be inserted. | -
+at | `Int` | The index at which to insert the item. | `0`
-**Return:** The number represented by the text or `none` if the entire text can't be parsed as a number.
+**Return:** Nothing.
**Example:**
```tomo
-assert Num.parse("3.14") == 3.14
-assert Num.parse("1e3") == 1000
-assert Num.parse("1.5junk") == none
-remainder : Text
-assert Num.parse("1.5junk", &remainder) == 1.5
-assert remainder == "junk"
+list := [10, 20]
+list.insert_all([30, 40])
+assert list == [10, 20, 30, 40]
+
+list.insert_all([99, 100], at=2)
+assert list == [10, 99, 100, 20, 30, 40]
```
-## Num.percent
+## List.pop
```tomo
-Num.percent : func(n: Num, precision: Num = 0.01 -> Text)
+List.pop : func(list: &[T], index: Int = -1 -> T?)
```
-Convert a number into a percentage text with a percent sign.
+Removes and returns an item from the list. If the given index is present in the list, the item at that index will be removed and the list will become one element shorter.
+
+Since negative indices are counted from the back, the default behavior is to pop the last value.
Argument | Type | Description | Default
---------|------|-------------|---------
-n | `Num` | The number to be converted to a percent. | -
-precision | `Num` | Round the percentage to this precision level. | `0.01`
+list | `&[T]` | The list to remove an item from. | -
+index | `Int` | The index from which to remove the item. | `-1`
-**Return:** A text representation of the number as a percentage with a percent sign.
+**Return:** `none` if the list is empty or the given index does not exist in the list, otherwise the item at the given index.
**Example:**
```tomo
-assert (0.5).percent() == "50%"
-assert (1./3.).percent(2) == "33.33%"
-assert (1./3.).percent(2, precision=0.0001) == "33.3333%"
-assert (1./3.).percent(2, precision=10.) == "30%"
+list := &[10, 20, 30, 40]
+
+assert list.pop() == 40
+assert list[] == [10, 20, 30]
+
+assert list.pop(index=2) == 20
+assert list[] == [10, 30]
```
-## Num.rint
+## List.random
```tomo
-Num.rint : func(x: Num -> Num)
+List.random : func(list: [T], random: func(min,max:Int64->Int64)? = none -> T)
```
-Rounds a number to the nearest integer, with ties rounded to the nearest even integer.
+Selects a random element from the list.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number to be rounded. | -
+list | `[T]` | The list from which to select a random element. | -
+random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`
-**Return:** The nearest integer value of `x`.
+**Return:** A random element from the list.
**Example:**
```tomo
-assert (3.5).rint() == 4
-assert (2.5).rint() == 2
+assert [10, 20, 30].random() == 20
```
-## Num.round
+## List.remove_at
```tomo
-Num.round : func(x: Num -> Num)
+List.remove_at : func(list: @[T], at: Int = -1, count: Int = 1 -> Void)
```
-Rounds a number to the nearest whole number integer.
+Removes elements from the list starting at a specified index.
+
+Since negative indices are counted from the back, the default behavior is to remove the last item.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number to be rounded. | -
+list | `@[T]` | The mutable reference to the list. | -
+at | `Int` | The index at which to start removing elements. | `-1`
+count | `Int` | The number of elements to remove. | `1`
-**Return:** The nearest integer value of `x`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (2.3).round() == 2
-assert (2.7).round() == 3
+list := [10, 20, 30, 40, 50]
+list.remove_at(2)
+assert list == [10, 30, 40, 50]
+
+list.remove_at(2, count=2)
+assert list == [10, 50]
```
-## Num.significand
+## List.remove_item
```tomo
-Num.significand : func(x: Num -> Num)
+List.remove_item : func(list: @[T], item: T, max_count: Int = -1 -> Void)
```
-Extracts the significand (or mantissa) of a number.
+Removes all occurrences of a specified item from the list.
+
+A negative `max_count` means "remove all occurrences".
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number from which to extract the significand. | -
+list | `@[T]` | The mutable reference to the list. | -
+item | `T` | The item to be removed. | -
+max_count | `Int` | The maximum number of occurrences to remove. | `-1`
-**Return:** The significand of `x`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (1234.567).significand() == 0.1234567
+list := [10, 20, 10, 20, 30]
+list.remove_item(10)
+assert list == [20, 20, 30]
+
+list.remove_item(20, max_count=1)
+assert list == [20, 30]
```
-## Num.sin
+## List.reversed
```tomo
-Num.sin : func(x: Num -> Num)
+List.reversed : func(list: [T] -> [T])
```
-Computes the sine of a number (angle in radians).
+Returns a reversed slice of the list.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The angle in radians. | -
+list | `[T]` | The list to be reversed. | -
-**Return:** The sine of `x`.
+**Return:** A slice of the list with elements in reverse order.
**Example:**
```tomo
-assert (0.0).sin() == 0
+assert [10, 20, 30].reversed() == [30, 20, 10]
```
-## Num.sinh
+## List.sample
```tomo
-Num.sinh : func(x: Num -> Num)
+List.sample : func(list: [T], count: Int, weights: [Float64]? = none, random: func(->Float64)? = none -> [T])
```
-Computes the hyperbolic sine of a number.
+Selects a sample of elements from the list, optionally with weighted probabilities.
+
+Errors will be raised if any of the following conditions occurs: - The given list has no elements and `count >= 1` - `count < 0` (negative count) - The number of weights provided doesn't match the length of the list. - Any weight in the weights list is negative, infinite, or `NaN` - The sum of the given weights is zero (zero probability for every element).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the hyperbolic sine is to be calculated. | -
+list | `[T]` | The list to sample from. | -
+count | `Int` | The number of elements to sample. | -
+weights | `[Float64]?` | The probability weights for each element in the list. These values do not need to add up to any particular number, they are relative weights. If no weights are given, elements will be sampled with uniform probability. | `none`
+random | `func(->Float64)?` | If provided, this function will be used to get random values for sampling the list. The provided function should return random numbers between `0.0` (inclusive) and `1.0` (exclusive). (Used for deterministic pseudorandom number generation) | `none`
-**Return:** The hyperbolic sine of `x`.
+**Return:** A list of sampled elements from the list.
**Example:**
```tomo
-assert (0.0).sinh() == 0
+assert [10, 20, 30].sample(2, weights=[90%, 5%, 5%]) == [10, 10]
```
-## Num.sqrt
+## List.shuffle
```tomo
-Num.sqrt : func(x: Num -> Num)
+List.shuffle : func(list: @[T], random: func(min,max:Int64->Int64)? = none -> Void)
```
-Computes the square root of a number.
+Shuffles the elements of the list in place.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the square root is to be calculated. | -
+list | `@[T]` | The mutable reference to the list to be shuffled. | -
+random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`
-**Return:** The square root of `x`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (16.0).sqrt() == 4
+list.shuffle()
```
-## Num.tan
+## List.shuffled
```tomo
-Num.tan : func(x: Num -> Num)
+List.shuffled : func(list: [T], random: func(min,max:Int64->Int64)? = none -> [T])
```
-Computes the tangent of a number (angle in radians).
+Creates a new list with elements shuffled.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The angle in radians. | -
+list | `[T]` | The list to be shuffled. | -
+random | `func(min,max:Int64->Int64)?` | If provided, this function will be used to get a random index in the list. Returned values must be between `min` and `max` (inclusive). (Used for deterministic pseudorandom number generation) | `none`
-**Return:** The tangent of `x`.
+**Return:** A new list with shuffled elements.
**Example:**
```tomo
-assert (0.0).tan() == 0
+assert [10, 20, 30, 40].shuffled() == [40, 10, 30, 20]
```
-## Num.tanh
+## List.slice
```tomo
-Num.tanh : func(x: Num -> Num)
+List.slice : func(list: [T], from: Int, to: Int -> [T])
```
-Computes the hyperbolic tangent of a number.
+Returns a slice of the list spanning the given indices (inclusive).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the hyperbolic tangent is to be calculated. | -
+list | `[T]` | The original list. | -
+from | `Int` | The first index to include. | -
+to | `Int` | The last index to include. | -
-**Return:** The hyperbolic tangent of `x`.
+**Return:** A new list spanning the given indices. Note: negative indices are counted from the back of the list, so `-1` refers to the last element, `-2` the second-to-last, and so on.
**Example:**
```tomo
-assert (0.0).tanh() == 0
+assert [10, 20, 30, 40, 50].slice(2, 4) == [20, 30, 40]
+assert [10, 20, 30, 40, 50].slice(-3, -2) == [30, 40]
```
-## Num.tgamma
+## List.sort
```tomo
-Num.tgamma : func(x: Num -> Num)
+List.sort : func(list: @[T], by = T.compare -> Void)
```
-Computes the gamma function of a number.
+Sorts the elements of the list in place in ascending order (small to large).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the gamma function is to be calculated. | -
+list | `@[T]` | The mutable reference to the list to be sorted. | -
+by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
-**Return:** The gamma function of `x`.
+**Return:** Nothing.
**Example:**
```tomo
-assert (1.0).tgamma() == 1
+list := [40, 10, -30, 20]
+list.sort()
+assert list == [-30, 10, 20, 40]
+
+list.sort(func(a,b:&Int): a.abs() <> b.abs())
+assert list == [10, 20, -30, 40]
```
-## Num.trunc
+## List.sorted
```tomo
-Num.trunc : func(x: Num -> Num)
+List.sorted : func(list: [T], by = T.compare -> [T])
```
-Truncates a number to the nearest integer towards zero.
+Creates a new list with elements sorted.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number to be truncated. | -
+list | `[T]` | The list to be sorted. | -
+by | `` | The comparison function used to determine order. If not specified, the default comparison function for the item type will be used. | `T.compare`
-**Return:** The integer part of `x` towards zero.
+**Return:** A new list with sorted elements.
**Example:**
```tomo
-assert (3.7).trunc() == 3
-assert (-3.7).trunc() == -3
+assert [40, 10, -30, 20].sorted() == [-30, 10, 20, 40]
+assert [40, 10, -30, 20].sorted(
+ func(a,b:&Int): a.abs() <> b.abs()
+) == [10, 20, -30, 40]
```
-## Num.with_precision
+## List.to
```tomo
-Num.with_precision : func(n: Num, precision: Num -> Num)
+List.to : func(list: [T], last: Int -> [T])
```
-Round a number to the given precision level (specified as `10`, `.1`, `.001` etc).
+Returns a slice of the list from the start of the original list up to a specified index (inclusive).
Argument | Type | Description | Default
---------|------|-------------|---------
-n | `Num` | The number to be rounded to a given precision. | -
-precision | `Num` | The precision to which the number should be rounded. | -
+list | `[T]` | The original list. | -
+last | `Int` | The index up to which elements should be included. | -
-**Return:** The number, rounded to the given precision level.
+**Return:** A new list containing elements from the start up to the specified index.
**Example:**
```tomo
-assert (0.1234567).with_precision(0.01) == 0.12
-assert (123456.).with_precision(100) == 123500
-assert (1234567.).with_precision(5) == 1234565
+assert [10, 20, 30, 40, 50].to(3) == [10, 20, 30]
+assert [10, 20, 30, 40, 50].to(-2) == [10, 20, 30, 40]
```
-## Num.y0
+## List.unique
```tomo
-Num.y0 : func(x: Num -> Num)
+List.unique : func(list: [T] -> {T})
```
-Computes the Bessel function of the second kind of order 0.
+Returns a set of the unique elements of the list.
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the Bessel function is to be calculated. | -
+list | `[T]` | The list to process. | -
-**Return:** The Bessel function of the second kind of order 0 of `x`.
+**Return:** A set of the unique elements from the list.
**Example:**
```tomo
-assert (1.0).y0() == -0.7652
+assert [10, 20, 10, 10, 30].unique() == {10, 20, 30}
```
-## Num.y1
+## List.where
```tomo
-Num.y1 : func(x: Num -> Num)
+List.where : func(list: [T], predicate: func(item:&T -> Bool) -> Int)
```
-Computes the Bessel function of the second kind of order 1.
+Find the index of the first item that matches a predicate function (if any).
Argument | Type | Description | Default
---------|------|-------------|---------
-x | `Num` | The number for which the Bessel function is to be calculated. | -
+list | `[T]` | The list to search through. | -
+predicate | `func(item:&T -> Bool)` | A function that returns `yes` if the item's index should be returned or `no` if it should not. | -
-**Return:** The Bessel function of the second kind of order 1 of `x`.
+**Return:** Returns the index of the first item where the predicate is true or `none` if no item matches.
**Example:**
```tomo
-assert (1.0).y1() == 0.4401
+assert [4, 5, 6].where(func(i:&Int): i.is_prime()) == 5
+assert [4, 6, 8].find(func(i:&Int): i.is_prime()) == none
```