diff --git a/src/gleam_community/maths/float_list.gleam b/src/gleam_community/maths/float_list.gleam index 4f49abf..860774b 100644 --- a/src/gleam_community/maths/float_list.gleam +++ b/src/gleam_community/maths/float_list.gleam @@ -507,7 +507,8 @@ pub fn geometric_space( /// /// /// 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. +/// ///
/// Example: /// @@ -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]) /// } ///
/// @@ -525,14 +537,29 @@ pub fn geometric_space( /// /// /// -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 +// } + ///
/// /// Spot a typo? Open an issue! @@ -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) /// } /// /// diff --git a/test/gleam/gleam_community_maths_float_list_test.gleam b/test/gleam/gleam_community_maths_float_list_test.gleam index 599578d..60c0401 100644 --- a/test/gleam/gleam_community_maths_float_list_test.gleam +++ b/test/gleam/gleam_community_maths_float_list_test.gleam @@ -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 [] diff --git a/test/gleam/gleam_community_maths_float_test.gleam b/test/gleam/gleam_community_maths_float_test.gleam index d8569bc..9f5684b 100644 --- a/test/gleam/gleam_community_maths_float_test.gleam +++ b/test/gleam/gleam_community_maths_float_test.gleam @@ -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()