More work on float_list module

This commit is contained in:
NicklasXYZ 2023-01-28 21:45:18 +01:00
parent b0da87f755
commit 7ab5d4bcbe
3 changed files with 70 additions and 12 deletions

View file

@ -507,7 +507,8 @@ pub fn geometric_space(
/// </div>
///
/// The function returns a list with evenly spaced values within a given interval based on a start, stop value and a given increment (step-length) between consecutive values.
///
/// The list returned includes the given start value but excludes the stop value.
///
/// <details>
/// <summary>Example:</summary>
///
@ -515,7 +516,18 @@ pub fn geometric_space(
/// import gleam_community/maths/float_list
///
/// pub fn example () {
///
/// float_list.arrange(1.0, 5.0, 1.0)
/// |> should.equal([1.0, 2.0, 3.0, 4.0])
///
/// // No points returned since
/// // start smaller than stop and positive step
/// float_list.arrange(5.0, 1.0, 1.0)
/// |> should.equal([])
///
/// // Points returned since
/// // start smaller than stop but negative step
/// float_list.arrange(5.0, 1.0, -1.0)
/// |> should.equal([5.0, 4.0, 3.0, 2.0])
/// }
/// </details>
///
@ -525,14 +537,29 @@ pub fn geometric_space(
/// </a>
/// </div>
///
pub fn arrange(
start: Float,
stop: Float,
step: Float,
) -> Result(List(Float), String) {
todo
pub fn arrange(start: Float, stop: Float, step: Float) -> List(Float) {
case start >=. stop && step >. 0.0 || start <=. stop && step <. 0.0 {
True -> []
False -> {
let direction: Float = case start <=. stop {
True -> 1.0
False -> -1.0
}
let step_abs: Float = float.absolute_value(step)
let num: Float = float.absolute_value(start -. stop) /. step_abs
list.range(0, floatx.to_int(num) - 1)
|> list.map(fn(i: Int) -> Float {
start +. intx.to_float(i) *. step_abs *. direction
})
}
}
}
// fn do_arrange(start: Float, step: Float, direction: Float) -> Float {
// case
// }
/// <div style="text-align: right;">
/// <a href="https://github.com/gleam-community/maths/issues">
/// <small>Spot a typo? Open an issue!</small>
@ -557,12 +584,12 @@ pub fn arrange(
/// // An empty list returns an error
/// []
/// |> float_list.sum()
/// |> should.equal(0.)
/// |> should.equal(0.0)
///
/// // Valid input returns a result
/// [1., 2., 3.]
/// [1.0, 2.0, 3.0]
/// |> float_list.sum()
/// |> should.equal(6.)
/// |> should.equal(6.0)
/// }
/// </details>
///

View file

@ -353,6 +353,38 @@ pub fn float_list_geometric_space_test() {
|> should.be_error()
}
pub fn float_list_arrange_test() {
// Positive start, stop, step
float_list.arrange(1.0, 5.0, 1.0)
|> should.equal([1.0, 2.0, 3.0, 4.0])
float_list.arrange(1.0, 5.0, 0.5)
|> should.equal([1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5])
float_list.arrange(1.0, 2.0, 0.25)
|> should.equal([1.0, 1.25, 1.5, 1.75])
// Reverse (switch start/stop largest/smallest value)
float_list.arrange(5.0, 1.0, 1.0)
|> should.equal([])
// Reverse negative step
float_list.arrange(5.0, 1.0, -1.0)
|> should.equal([5.0, 4.0, 3.0, 2.0])
// Positive start, negative stop, step
float_list.arrange(5.0, -1.0, -1.0)
|> should.equal([5.0, 4.0, 3.0, 2.0, 1.0, 0.0])
// Negative start, stop, step
float_list.arrange(-5.0, -1.0, -1.0)
|> should.equal([])
// Negative start, stop, positive step
float_list.arrange(-5.0, -1.0, 1.0)
|> should.equal([-5.0, -4.0, -3.0, -2.0])
}
pub fn float_list_maximum_test() {
// An empty lists returns an error
[]

View file

@ -1044,7 +1044,6 @@ pub fn float_absolute_difference_test() {
pub fn float_constants_test() {
floatx.e()
|> io.debug()
|> floatx.is_close(2.71828, 0.0, 0.00001)
|> should.be_true()