From a2db0b486a95899f4a0c4bbb9c2719c26dc196f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Wed, 16 Oct 2024 12:13:29 +0200 Subject: [PATCH 01/11] Add list utility functions --- numbat/modules/core/lists.nbt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/numbat/modules/core/lists.nbt b/numbat/modules/core/lists.nbt index 8c56dc49..e9a6e4c5 100644 --- a/numbat/modules/core/lists.nbt +++ b/numbat/modules/core/lists.nbt @@ -118,6 +118,29 @@ fn sort_by_key(key: Fn[(A) -> D], xs: List) -> List = @example("sort([3, 2, 7, 8, -4, 0, -5])") fn sort(xs: List) -> List = sort_by_key(id, xs) +@description("looks for an element in a list. Returns true if the element is in the list.") +@example("[3, 2, 7, 8, -4, 0, -5] |> contains(0)") +@example("[3, 2, 7, 8, -4, 0, -5] |> contains(1)") +fn contains(x: A, xs: List) -> Bool = + if is_empty(xs) + then false + else if x == head(xs) + then true + else contains(x, tail(xs)) + +fn _unique(acc: List, xs: List) -> List = + if is_empty(xs) + then acc + else if is_empty(acc) + then _unique([head(xs)], tail(xs)) + else if (acc |> contains(head(xs))) + then _unique(acc, tail(xs)) + else _unique((cons_end(head(xs), acc)), tail(xs)) + +@description("Remove duplicates, ensuring every value is unique") +@example("unique([1, 2, 2, 3, 3, 3])") +fn unique(xs: List) -> List = xs |> _unique([]) + @description("Add an element between each pair of elements in a list") @example("intersperse(0, [1, 1, 1, 1])") fn intersperse(sep: A, xs: List) -> List = From 9df29fd1cb3a22286821c063212a086e7f42ac10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Wed, 16 Oct 2024 12:14:16 +0200 Subject: [PATCH 02/11] add utility functions for numbers --- numbat/modules/core/numbers.nbt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/numbat/modules/core/numbers.nbt b/numbat/modules/core/numbers.nbt index 3265ea38..e8310fae 100644 --- a/numbat/modules/core/numbers.nbt +++ b/numbat/modules/core/numbers.nbt @@ -14,3 +14,13 @@ fn is_infinite(n: T) -> Bool @example("is_finite(37)") @example("is_finite(-inf)") fn is_finite(n: T) -> Bool = !is_nan(n) && !is_infinite(n) + +@description("Returns true if the input is 0 (zero).") +@example("is_zero(37)") +@example("is_zero(0)") +fn is_zero(value: D) -> Bool = value == 0 + +@description("Returns true if the input is anything other than 0 (zero).") +@example("is_not_zero(37)") +@example("is_not_zero(0)") +fn is_not_zero(value: D) -> Bool = !is_zero(value) \ No newline at end of file From 7a8eae9da6fdaac264f876b834a477b6fbc0fa68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Wed, 16 Oct 2024 12:14:39 +0200 Subject: [PATCH 03/11] Add round_mixed_in utility function --- numbat/modules/core/mixed_units.nbt | 13 +++++++++++++ numbat/modules/prelude.nbt | 1 + numbat/modules/units/mixed.nbt | 5 ++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/numbat/modules/core/mixed_units.nbt b/numbat/modules/core/mixed_units.nbt index f87da4ef..a90508d3 100644 --- a/numbat/modules/core/mixed_units.nbt +++ b/numbat/modules/core/mixed_units.nbt @@ -1,5 +1,7 @@ use core::strings use core::lists +use core::numbers +use core::quantities # Helper functions for mixed-unit conversions. See units::mixed for more. @@ -15,3 +17,14 @@ fn _mixed_unit_list(val: D, units: List, acc: List) -> List = if (len(units) > 0) then (val |> trunc_in(head(units))) else error("Units list cannot be empty") + + +fn _clean_units(units: List) -> List = units |> unique() |> sort() |> reverse() + +fn _unit_list(units: List, value: D) -> List = _mixed_unit_list(value, _clean_units(units), []) + +@description("Round a mixed unit number to the nearest multiple of `base`.") +@example("[0 days, 12 hours, 0 minutes, 30 seconds] |> round_mixed_in(minutes)") +@example("[0 days, 12 hours, 29 minutes, 30 seconds] |> round_mixed_in(hours)") +fn round_mixed_in(base: D, value: List) -> List = value |> sum |> round_in(base) |> _unit_list(units) + where units = value |> filter(is_not_zero) |> map(unit_of) |> cons(base) \ No newline at end of file diff --git a/numbat/modules/prelude.nbt b/numbat/modules/prelude.nbt index ab6aec61..3fd2d972 100644 --- a/numbat/modules/prelude.nbt +++ b/numbat/modules/prelude.nbt @@ -7,6 +7,7 @@ use core::strings use core::error use core::random use core::numbers +use core::mixed_units use math::constants use math::transcendental diff --git a/numbat/modules/units/mixed.nbt b/numbat/modules/units/mixed.nbt index d0aa1639..c175eec2 100644 --- a/numbat/modules/units/mixed.nbt +++ b/numbat/modules/units/mixed.nbt @@ -5,7 +5,7 @@ use units::imperial @name("Unit list") @description("Convert a value to a mixed representation using the provided units.") @example("5500 m |> unit_list([miles, yards, feet, inches])") -fn unit_list(units: List, value: D) -> List = _mixed_unit_list(value, units, []) +fn unit_list(units: List, value: D) -> List = _unit_list(units, value) @name("Degrees, minutes, seconds") @description("Convert an angle to a mixed degrees, (arc)minutes, and (arc)seconds representation. Also called sexagesimal degree notation.") @@ -33,5 +33,4 @@ fn feet_and_inches(length: Length) -> List = @url("https://en.wikipedia.org/wiki/Pound_(mass)") @example("1 kg -> pounds_and_ounces") fn pounds_and_ounces(mass: Mass) -> List = - unit_list([pound, ounce], mass) - + unit_list([pound, ounce], mass) \ No newline at end of file From 91bfd2194a317a6ccbb40a59ab2983cc1ec74b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Wed, 16 Oct 2024 12:36:42 +0200 Subject: [PATCH 04/11] Update documentation --- book/src/list-functions-lists.md | 39 ++++++++++++++++++++++++++++ book/src/list-functions-other.md | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/book/src/list-functions-lists.md b/book/src/list-functions-lists.md index c3919ea0..8a556304 100644 --- a/book/src/list-functions-lists.md +++ b/book/src/list-functions-lists.md @@ -300,6 +300,45 @@ fn sort(xs: List) -> List +### `contains` +looks for an element in a list. Returns true if the element is in the list. + +```nbt +fn contains(x: A, xs: List) -> Bool +``` + +
+Examples + +
>>> [3, 2, 7, 8, -4, 0, -5] |> contains(0) + + = true [Bool] +
+ +
>>> [3, 2, 7, 8, -4, 0, -5] |> contains(1) + + = false [Bool] +
+ +
+ +### `unique` +Remove duplicates, ensuring every value is unique. + +```nbt +fn unique
(xs: List) -> List +``` + +
+Examples + +
>>> unique([1, 2, 2, 3, 3, 3]) + + = [1, 2, 3] [List] +
+ +
+ ### `intersperse` Add an element between each pair of elements in a list. diff --git a/book/src/list-functions-other.md b/book/src/list-functions-other.md index 9d528e9b..8e705d57 100644 --- a/book/src/list-functions-other.md +++ b/book/src/list-functions-other.md @@ -85,6 +85,50 @@ fn is_finite(n: T) -> Bool +### `is_zero` +Returns true if the input is 0 (zero). + +```nbt +fn is_zero(value: D) -> Bool +``` + +
+Examples + +
>>> is_zero(37) + + = false [Bool] +
+ +
>>> is_zero(0) + + = true [Bool] +
+ +
+ +### `is_not_zero` +Returns true if the input is anything other than 0 (zero). + +```nbt +fn is_not_zero(value: D) -> Bool +``` + +
+Examples + +
>>> is_not_zero(37) + + = true [Bool] +
+ +
>>> is_not_zero(0) + + = false [Bool] +
+ +
+ ## Quantities Defined in: `core::quantities` From ec726f56f24d77dc68e3584c064a580c24c14af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Sat, 19 Oct 2024 15:44:57 +0200 Subject: [PATCH 05/11] Add sort_descending function --- examples/tests/lists.nbt | 18 ++++++++++++++++++ numbat/modules/core/lists.nbt | 8 +++++++- numbat/modules/core/mixed_units.nbt | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/examples/tests/lists.nbt b/examples/tests/lists.nbt index f4043c02..afcb5ead 100644 --- a/examples/tests/lists.nbt +++ b/examples/tests/lists.nbt @@ -70,6 +70,24 @@ fn negate(x) = -x assert_eq(sort_by_key(negate, [1, 2, 3]), [3, 2, 1]) assert_eq(sort_by_key(str_length, ["aa", "", "aaaa", "aaa"]), ["", "aa", "aaa", "aaaa"]) +# sort_descending: +assert_eq(sort_descending([]), []) + +assert_eq(sort_descending([1]), [1]) + +assert_eq(sort_descending([1, 2]), [2, 1]) +assert_eq(sort_descending([2, 1]), [2, 1]) + +assert_eq(sort_descending([1, 2, 3]), [3, 2, 1]) +assert_eq(sort_descending([1, 3, 2]), [3, 2, 1]) +assert_eq(sort_descending([2, 1, 3]), [3, 2, 1]) +assert_eq(sort_descending([2, 3, 1]), [3, 2, 1]) +assert_eq(sort_descending([3, 1, 2]), [3, 2, 1]) +assert_eq(sort_descending([3, 2, 1]), [3, 2, 1]) + +assert_eq(sort_descending([3, 2, 7, 8, -4, 0, -5]), (reverse(sort([3, 2, 7, 8, -4, 0, -5])))) + + assert_eq(intersperse(0, []), []) assert_eq(intersperse(0, [1]), [1]) assert_eq(intersperse(0, [1, 2, 3]), [1, 0, 2, 0, 3]) diff --git a/numbat/modules/core/lists.nbt b/numbat/modules/core/lists.nbt index e9a6e4c5..247efc5b 100644 --- a/numbat/modules/core/lists.nbt +++ b/numbat/modules/core/lists.nbt @@ -114,10 +114,16 @@ fn sort_by_key(key: Fn[(A) -> D], xs: List
) -> List = sort_by_key(key, drop(floor(len(xs) / 2), xs)), key) -@description("Sort a list of quantities") +@description("Sort a list of quantities in ascending order") @example("sort([3, 2, 7, 8, -4, 0, -5])") fn sort(xs: List) -> List = sort_by_key(id, xs) +fn _negate(x: D) = -x + +@description("Sort a list of quantities in descending order") +@example("sort([3, 2, 7, 8, -4, 0, -5])") +fn sort_descending(xs: List) -> List = sort_by_key(_negate, xs) + @description("looks for an element in a list. Returns true if the element is in the list.") @example("[3, 2, 7, 8, -4, 0, -5] |> contains(0)") @example("[3, 2, 7, 8, -4, 0, -5] |> contains(1)") diff --git a/numbat/modules/core/mixed_units.nbt b/numbat/modules/core/mixed_units.nbt index a90508d3..28996b30 100644 --- a/numbat/modules/core/mixed_units.nbt +++ b/numbat/modules/core/mixed_units.nbt @@ -19,7 +19,7 @@ fn _mixed_unit_list(val: D, units: List, acc: List) -> List = else error("Units list cannot be empty") -fn _clean_units(units: List) -> List = units |> unique() |> sort() |> reverse() +fn _clean_units(units: List) -> List = units |> unique() |> sort_descending() fn _unit_list(units: List, value: D) -> List = _mixed_unit_list(value, _clean_units(units), []) From f004fe736a37f14a0fef306f7034a512bcf5f1b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Sat, 19 Oct 2024 16:12:55 +0200 Subject: [PATCH 06/11] add tests for unique and contains --- examples/tests/lists.nbt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/tests/lists.nbt b/examples/tests/lists.nbt index afcb5ead..76f64846 100644 --- a/examples/tests/lists.nbt +++ b/examples/tests/lists.nbt @@ -87,7 +87,31 @@ assert_eq(sort_descending([3, 2, 1]), [3, 2, 1]) assert_eq(sort_descending([3, 2, 7, 8, -4, 0, -5]), (reverse(sort([3, 2, 7, 8, -4, 0, -5])))) +# contains: +assert_eq(contains(1, []), false) +assert(contains(1, [1])) + +assert(contains(1, [1, 2, 3])) +assert(contains(1, [3, 2, 1])) +assert(contains(1, [3, 1, 2])) + +assert_eq(contains(10, [1, 2, 3]), false) + +assert(contains("1", ["1", "2", "3"])) + +# unique: +assert_eq(unique([]), []) + +assert_eq(unique([1, 2, 3]), [1, 2, 3]) + +assert_eq(unique([1, 2, 2, 3, 3, 3]), [1, 2, 3]) +assert_eq(unique([3, 3, 3, 2, 2, 1]), [3, 2, 1]) +assert_eq(unique([1, 3, 2, 3, 2, 3]), [1, 3, 2]) + +assert_eq(unique([1, 3, 2, 3, 2, 3]), unique(unique([1, 3, 2, 3, 2, 3,]))) + +# intersperse: assert_eq(intersperse(0, []), []) assert_eq(intersperse(0, [1]), [1]) assert_eq(intersperse(0, [1, 2, 3]), [1, 0, 2, 0, 3]) From d266bd4e13bfba09e167832bc3d5aa5faacb21c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Sat, 19 Oct 2024 16:29:53 +0200 Subject: [PATCH 07/11] Add tests for round_mixed_in --- examples/tests/mixed_units.nbt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/tests/mixed_units.nbt b/examples/tests/mixed_units.nbt index 2bb71ddd..67747c95 100644 --- a/examples/tests/mixed_units.nbt +++ b/examples/tests/mixed_units.nbt @@ -49,3 +49,15 @@ assert_eq(test2 |> unit_list([degree, arcmin]) |> sum, test2) assert_eq(test2 |> unit_list([degree, arcmin, arcsec]) |> sum, test2) +# round_mixed_in + +assert_eq(round_mixed_in(seconds, [1 h, 29 min, 30 sec]), [1 h, 29 min, 30 sec]) +assert_eq(round_mixed_in(minutes, [1 h, 29 min, 30 sec]), [1 h, 30 min, 0 sec]) +assert_eq(round_mixed_in(hours, [1 h, 29 min, 30 sec]), [1 h, 0 min, 0 sec]) + +assert_eq(round_mixed_in(mm, [1 m, 29 cm]), [1 m, 29 cm, 0 mm]) + +assert_eq(round_mixed_in(m, round_mixed_in(cm, [1 m, 49 cm, 50 mm])), [2 m, 0 cm]) + +let units = [m, cm, mm] +assert_eq(unit_list(units, 123456 mm) |> round_mixed_in(cm), round_in(cm, 123456 mm) |> unit_list(units)) From 7d83ffd5d3aed20d73348e63ffdc875e40e83230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Sat, 19 Oct 2024 16:45:39 +0200 Subject: [PATCH 08/11] Fix example for sort_descending --- numbat/modules/core/lists.nbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numbat/modules/core/lists.nbt b/numbat/modules/core/lists.nbt index 247efc5b..5dd3e0a1 100644 --- a/numbat/modules/core/lists.nbt +++ b/numbat/modules/core/lists.nbt @@ -121,7 +121,7 @@ fn sort(xs: List) -> List = sort_by_key(id, xs) fn _negate(x: D) = -x @description("Sort a list of quantities in descending order") -@example("sort([3, 2, 7, 8, -4, 0, -5])") +@example("sort_descending([3, 2, 7, 8, -4, 0, -5])") fn sort_descending(xs: List) -> List = sort_by_key(_negate, xs) @description("looks for an element in a list. Returns true if the element is in the list.") From 90761f078d940a529fdd1022e89e81ac7137bd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Sat, 19 Oct 2024 16:45:52 +0200 Subject: [PATCH 09/11] update documentation --- book/src/list-functions-lists.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/book/src/list-functions-lists.md b/book/src/list-functions-lists.md index 8a556304..3edb8b39 100644 --- a/book/src/list-functions-lists.md +++ b/book/src/list-functions-lists.md @@ -284,7 +284,7 @@ sort_by_key(last_digit, [701, 313, 9999, 4]) ### `sort` -Sort a list of quantities. +Sort a list of quantities in ascending order. ```nbt fn sort(xs: List) -> List @@ -300,6 +300,23 @@ fn sort(xs: List) -> List +### `sort_descending` +Sort a list of quantities in descending order. + +```nbt +fn sort_descending(xs: List) -> List +``` + +
+Examples + +
>>> sort_descending([3, 2, 7, 8, -4, 0, -5]) + + = [8, 7, 3, 2, 0, -4, -5] [List] +
+ +
+ ### `contains` looks for an element in a list. Returns true if the element is in the list. From 205e40cde1484de049f9bcd040fcdabc64e25b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Wed, 23 Oct 2024 23:37:26 +0200 Subject: [PATCH 10/11] Make changes suggested in code review --- examples/tests/lists.nbt | 21 +-------------------- examples/tests/mixed_units.nbt | 14 -------------- numbat/modules/core/lists.nbt | 10 ++-------- numbat/modules/core/mixed_units.nbt | 11 ++++------- numbat/modules/core/numbers.nbt | 8 ++++---- 5 files changed, 11 insertions(+), 53 deletions(-) diff --git a/examples/tests/lists.nbt b/examples/tests/lists.nbt index 76f64846..6789de4b 100644 --- a/examples/tests/lists.nbt +++ b/examples/tests/lists.nbt @@ -70,33 +70,14 @@ fn negate(x) = -x assert_eq(sort_by_key(negate, [1, 2, 3]), [3, 2, 1]) assert_eq(sort_by_key(str_length, ["aa", "", "aaaa", "aaa"]), ["", "aa", "aaa", "aaaa"]) -# sort_descending: -assert_eq(sort_descending([]), []) - -assert_eq(sort_descending([1]), [1]) - -assert_eq(sort_descending([1, 2]), [2, 1]) -assert_eq(sort_descending([2, 1]), [2, 1]) - -assert_eq(sort_descending([1, 2, 3]), [3, 2, 1]) -assert_eq(sort_descending([1, 3, 2]), [3, 2, 1]) -assert_eq(sort_descending([2, 1, 3]), [3, 2, 1]) -assert_eq(sort_descending([2, 3, 1]), [3, 2, 1]) -assert_eq(sort_descending([3, 1, 2]), [3, 2, 1]) -assert_eq(sort_descending([3, 2, 1]), [3, 2, 1]) - -assert_eq(sort_descending([3, 2, 7, 8, -4, 0, -5]), (reverse(sort([3, 2, 7, 8, -4, 0, -5])))) - # contains: -assert_eq(contains(1, []), false) - assert(contains(1, [1])) assert(contains(1, [1, 2, 3])) assert(contains(1, [3, 2, 1])) assert(contains(1, [3, 1, 2])) -assert_eq(contains(10, [1, 2, 3]), false) +assert(!contains(10, [1, 2, 3])) assert(contains("1", ["1", "2", "3"])) diff --git a/examples/tests/mixed_units.nbt b/examples/tests/mixed_units.nbt index 67747c95..29311f64 100644 --- a/examples/tests/mixed_units.nbt +++ b/examples/tests/mixed_units.nbt @@ -47,17 +47,3 @@ let test2 = 12 degree + 34 arcminute + 5 arcsec assert_eq(test2 |> unit_list([degree]) |> head, test2) assert_eq(test2 |> unit_list([degree, arcmin]) |> sum, test2) assert_eq(test2 |> unit_list([degree, arcmin, arcsec]) |> sum, test2) - - -# round_mixed_in - -assert_eq(round_mixed_in(seconds, [1 h, 29 min, 30 sec]), [1 h, 29 min, 30 sec]) -assert_eq(round_mixed_in(minutes, [1 h, 29 min, 30 sec]), [1 h, 30 min, 0 sec]) -assert_eq(round_mixed_in(hours, [1 h, 29 min, 30 sec]), [1 h, 0 min, 0 sec]) - -assert_eq(round_mixed_in(mm, [1 m, 29 cm]), [1 m, 29 cm, 0 mm]) - -assert_eq(round_mixed_in(m, round_mixed_in(cm, [1 m, 49 cm, 50 mm])), [2 m, 0 cm]) - -let units = [m, cm, mm] -assert_eq(unit_list(units, 123456 mm) |> round_mixed_in(cm), round_in(cm, 123456 mm) |> unit_list(units)) diff --git a/numbat/modules/core/lists.nbt b/numbat/modules/core/lists.nbt index 5dd3e0a1..8a326684 100644 --- a/numbat/modules/core/lists.nbt +++ b/numbat/modules/core/lists.nbt @@ -118,13 +118,7 @@ fn sort_by_key(key: Fn[(A) -> D], xs: List
) -> List = @example("sort([3, 2, 7, 8, -4, 0, -5])") fn sort(xs: List) -> List = sort_by_key(id, xs) -fn _negate(x: D) = -x - -@description("Sort a list of quantities in descending order") -@example("sort_descending([3, 2, 7, 8, -4, 0, -5])") -fn sort_descending(xs: List) -> List = sort_by_key(_negate, xs) - -@description("looks for an element in a list. Returns true if the element is in the list.") +@description("Returns true if the element `x` is in the list `xs`.") @example("[3, 2, 7, 8, -4, 0, -5] |> contains(0)") @example("[3, 2, 7, 8, -4, 0, -5] |> contains(1)") fn contains(x: A, xs: List) -> Bool = @@ -143,7 +137,7 @@ fn _unique(acc: List, xs: List) -> List = then _unique(acc, tail(xs)) else _unique((cons_end(head(xs), acc)), tail(xs)) -@description("Remove duplicates, ensuring every value is unique") +@description("Remove duplicates from a given list.") @example("unique([1, 2, 2, 3, 3, 3])") fn unique(xs: List) -> List = xs |> _unique([]) diff --git a/numbat/modules/core/mixed_units.nbt b/numbat/modules/core/mixed_units.nbt index 28996b30..1f4a44a2 100644 --- a/numbat/modules/core/mixed_units.nbt +++ b/numbat/modules/core/mixed_units.nbt @@ -18,13 +18,10 @@ fn _mixed_unit_list(val: D, units: List, acc: List) -> List = then (val |> trunc_in(head(units))) else error("Units list cannot be empty") +fn _negate(x: D) = -x -fn _clean_units(units: List) -> List = units |> unique() |> sort_descending() +fn _sort_descending(xs: List) -> List = sort_by_key(_negate, xs) -fn _unit_list(units: List, value: D) -> List = _mixed_unit_list(value, _clean_units(units), []) +fn _clean_units(units: List) -> List = units |> unique() |> _sort_descending() -@description("Round a mixed unit number to the nearest multiple of `base`.") -@example("[0 days, 12 hours, 0 minutes, 30 seconds] |> round_mixed_in(minutes)") -@example("[0 days, 12 hours, 29 minutes, 30 seconds] |> round_mixed_in(hours)") -fn round_mixed_in(base: D, value: List) -> List = value |> sum |> round_in(base) |> _unit_list(units) - where units = value |> filter(is_not_zero) |> map(unit_of) |> cons(base) \ No newline at end of file +fn _unit_list(units: List, value: D) -> List = _mixed_unit_list(value, _clean_units(units), []) diff --git a/numbat/modules/core/numbers.nbt b/numbat/modules/core/numbers.nbt index e8310fae..38fa9ccc 100644 --- a/numbat/modules/core/numbers.nbt +++ b/numbat/modules/core/numbers.nbt @@ -20,7 +20,7 @@ fn is_finite(n: T) -> Bool = !is_nan(n) && !is_infinite(n) @example("is_zero(0)") fn is_zero(value: D) -> Bool = value == 0 -@description("Returns true if the input is anything other than 0 (zero).") -@example("is_not_zero(37)") -@example("is_not_zero(0)") -fn is_not_zero(value: D) -> Bool = !is_zero(value) \ No newline at end of file +@description("Returns true unless the input is 0 (zero).") +@example("is_nonzero(37)") +@example("is_nonzero(0)") +fn is_nonzero(value: D) -> Bool = !is_zero(value) \ No newline at end of file From a5950ffb1a9071223f7f6aedb1cd5570e9e8ff0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=B8ller=20Jensen?= Date: Thu, 24 Oct 2024 00:03:29 +0200 Subject: [PATCH 11/11] Update documentations --- book/src/list-functions-lists.md | 21 ++------------------- book/src/list-functions-other.md | 10 +++++----- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/book/src/list-functions-lists.md b/book/src/list-functions-lists.md index 3edb8b39..6527de8b 100644 --- a/book/src/list-functions-lists.md +++ b/book/src/list-functions-lists.md @@ -300,25 +300,8 @@ fn sort(xs: List) -> List -### `sort_descending` -Sort a list of quantities in descending order. - -```nbt -fn sort_descending(xs: List) -> List -``` - -
-Examples - -
>>> sort_descending([3, 2, 7, 8, -4, 0, -5]) - - = [8, 7, 3, 2, 0, -4, -5] [List] -
- -
- ### `contains` -looks for an element in a list. Returns true if the element is in the list. +Returns true if the element `x` is in the list `xs`. ```nbt fn contains
(x: A, xs: List) -> Bool @@ -340,7 +323,7 @@ fn contains(x: A, xs: List) -> Bool ### `unique` -Remove duplicates, ensuring every value is unique. +Remove duplicates from a given list. ```nbt fn unique(xs: List) -> List diff --git a/book/src/list-functions-other.md b/book/src/list-functions-other.md index 8e705d57..48a15eb5 100644 --- a/book/src/list-functions-other.md +++ b/book/src/list-functions-other.md @@ -107,22 +107,22 @@ fn is_zero(value: D) -> Bool -### `is_not_zero` -Returns true if the input is anything other than 0 (zero). +### `is_nonzero` +Returns true unless the input is 0 (zero). ```nbt -fn is_not_zero(value: D) -> Bool +fn is_nonzero(value: D) -> Bool ```
Examples -
>>> is_not_zero(37) +
>>> is_nonzero(37) = true [Bool]
-
>>> is_not_zero(0) +
>>> is_nonzero(0) = false [Bool]