From 387a227b9a87a4f859272385c5d150f1f27b63dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wa=C5=9Bko?= Date: Sat, 22 Jan 2022 16:15:04 +0100 Subject: [PATCH] Filter_with_index, changelog --- app/gui/CHANGELOG.md | 5 ++++ .../Base/0.2.32-SNAPSHOT/src/Data/Vector.enso | 24 +++++++++++++++++++ test/Tests/src/Data/Vector_Spec.enso | 13 ++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/gui/CHANGELOG.md b/app/gui/CHANGELOG.md index 895f18aecd1ce..e638a12d7aa9d 100644 --- a/app/gui/CHANGELOG.md +++ b/app/gui/CHANGELOG.md @@ -17,6 +17,10 @@ - [Implemented `Vector.distinct` allowing to remove duplicate elements from a Vector][3224] +- [Improved performance of `Vector.filter` and `Vector.each`; implemented + `Vector.filter_with_index`. Made `Vector.at` accept negative indices and + ensured it fails with a dataflow error on out of bounds access instead of an + internal Java exception.][3232] [3153]: https://github.com/enso-org/enso/pull/3153 [3166]: https://github.com/enso-org/enso/pull/3166 @@ -25,6 +29,7 @@ [3193]: https://github.com/enso-org/enso/pull/3193 [3208]: https://github.com/enso-org/enso/pull/3208 [3224]: https://github.com/enso-org/enso/pull/3224 +[3232]: https://github.com/enso-org/enso/pull/3232 # Enso 2.0.0-alpha.18 (2021-10-12) diff --git a/distribution/lib/Standard/Base/0.2.32-SNAPSHOT/src/Data/Vector.enso b/distribution/lib/Standard/Base/0.2.32-SNAPSHOT/src/Data/Vector.enso index 3fdbce9a5e265..4254da9c48f6e 100644 --- a/distribution/lib/Standard/Base/0.2.32-SNAPSHOT/src/Data/Vector.enso +++ b/distribution/lib/Standard/Base/0.2.32-SNAPSHOT/src/Data/Vector.enso @@ -334,6 +334,30 @@ type Vector case acc of _ -> builder.to_vector + ## Selects all elements of this vector which satisfy a predicate. + + Arguments: + - predicate: A function that takes an index and a list element and + returns a boolean value that says whether that value should be included + in the result. + + > Example + Selecting all elements which are equal to their position in the vector. + + [0, 10, 2, 2].filter (==) == [0, 2] + filter_with_index : (Integer -> Any -> Boolean) -> Vector Any + filter_with_index predicate = + builder = here.new_builder + acc = this.fold 0 ix-> elem-> + ## The accumulator is threaded through to preserve dataflow + dependencies which are cut by using the stateful builder. This to + ensure correct dataflow error propagation. + acc = if predicate ix elem then builder.append elem + case acc of + _ -> ix+1 + case acc of + _ -> builder.to_vector + ## Applies a function to each element of the vector, returning the vector of results. diff --git a/test/Tests/src/Data/Vector_Spec.enso b/test/Tests/src/Data/Vector_Spec.enso index 416b7f75c0600..418c1049c6c5d 100644 --- a/test/Tests/src/Data/Vector_Spec.enso +++ b/test/Tests/src/Data/Vector_Spec.enso @@ -87,10 +87,15 @@ spec = Test.group "Vectors" <| Test.specify "should filter elements" <| vec = [1, 2, 3, 4, 5] - vec.filter (ix -> ix > 3) . should_equal [4, 5] - vec.filter (ix -> ix == 1) . should_equal [1] - vec.filter (ix -> ix < 0) . should_equal [] - vec.filter (ix -> if ix == 2 then Error.throw <| My_Error "foo" else True) . should_fail_with My_Error + vec.filter (x -> x > 3) . should_equal [4, 5] + vec.filter (x -> x == 1) . should_equal [1] + vec.filter (x -> x < 0) . should_equal [] + vec.filter (x -> if x == 2 then Error.throw <| My_Error "foo" else True) . should_fail_with My_Error + + Test.specify "should filter elements with indices" <| + [0, 10, 2, 2].filter_with_index (==) . should_equal [0, 2] + ([1, 2, 3, 4].filter_with_index ix-> _-> ix < 2) . should_equal [1, 2] + ([1, 2, 3, 4].filter_with_index ix-> _-> if ix == 1 then Error.throw <| My_Error "foo" else True) . should_fail_with My_Error Test.specify "should allow mapping an operation, returning a new vector" <| vec = [1, 2, 3, 4]