Browse Source

Make changes suggested in code review

Mads Møller Jensen 1 year ago
parent
commit
205e40cde1

+ 1 - 20
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"]))
 

+ 0 - 14
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))

+ 2 - 8
numbat/modules/core/lists.nbt

@@ -118,13 +118,7 @@ fn sort_by_key<A, D: Dim>(key: Fn[(A) -> D], xs: List<A>) -> List<A> =
 @example("sort([3, 2, 7, 8, -4, 0, -5])")
 fn sort<D: Dim>(xs: List<D>) -> List<D> = sort_by_key(id, xs)
 
-fn _negate<D: Dim>(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<D: Dim>(xs: List<D>) -> List<D> = 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<A>(x: A, xs: List<A>) -> Bool = 
@@ -143,7 +137,7 @@ fn _unique<A>(acc: List<A>, xs: List<A>) -> List<A> =
         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<A>(xs: List<A>) -> List<A> = xs |> _unique([])
 

+ 4 - 7
numbat/modules/core/mixed_units.nbt

@@ -18,13 +18,10 @@ fn _mixed_unit_list<D: Dim>(val: D, units: List<D>, acc: List<D>) -> List<D> =
       then (val |> trunc_in(head(units)))
       else error("Units list cannot be empty")
   
+fn _negate<D: Dim>(x: D) = -x
 
-fn _clean_units<D: Dim>(units: List<D>) -> List<D> = units |> unique() |> sort_descending()
+fn _sort_descending<D: Dim>(xs: List<D>) -> List<D> = sort_by_key(_negate, xs)
 
-fn _unit_list<D: Dim>(units: List<D>, value: D) -> List<D> = _mixed_unit_list(value, _clean_units(units), [])
+fn _clean_units<D: Dim>(units: List<D>) -> List<D> = 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<D: Dim>(base: D, value: List<D>) -> List<D> = value |> sum |> round_in(base) |> _unit_list(units)
-  where units = value |> filter(is_not_zero) |> map(unit_of) |> cons(base)
+fn _unit_list<D: Dim>(units: List<D>, value: D) -> List<D> = _mixed_unit_list(value, _clean_units(units), [])

+ 4 - 4
numbat/modules/core/numbers.nbt

@@ -20,7 +20,7 @@ fn is_finite<T: Dim>(n: T) -> Bool = !is_nan(n) && !is_infinite(n)
 @example("is_zero(0)")
 fn is_zero<D: Dim>(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<D: Dim>(value: D) -> Bool = !is_zero(value)
+@description("Returns true unless the input is 0 (zero).")
+@example("is_nonzero(37)")
+@example("is_nonzero(0)")
+fn is_nonzero<D: Dim>(value: D) -> Bool = !is_zero(value)