diff --git a/app/gui/CHANGELOG.md b/app/gui/CHANGELOG.md index a2527db11fce..895f18aecd1c 100644 --- a/app/gui/CHANGELOG.md +++ b/app/gui/CHANGELOG.md @@ -13,12 +13,18 @@ - [Fixed developer console error about failing to decode a notification "executionContext/visualisationEvaluationFailed"][3193] +#### Enso Standard Library + +- [Implemented `Vector.distinct` allowing to remove duplicate elements from a + Vector][3224] + [3153]: https://github.com/enso-org/enso/pull/3153 [3166]: https://github.com/enso-org/enso/pull/3166 [3181]: https://github.com/enso-org/enso/pull/3181 [3186]: https://github.com/enso-org/enso/pull/3186 [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 # 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 d41f29f6f518..f3f95a021199 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 @@ -1,6 +1,8 @@ from Standard.Base import all from Standard.Builtins import Array +polyglot java import java.util.HashSet + ## Creates a new vector of the given length, initializing elements using the provided constructor function. @@ -720,6 +722,42 @@ type Vector Vector new_vec_arr + ## UNSTABLE + Keeps only unique elements within the Vector, removing any duplicates. + + Arguments: + - on: A projection from the element type to the value of that element + which determines the uniqueness criteria. + + The returned unique elements are kept in the same order as they appeared + in the input. + + Out of multiple equivalent (equal up to the `on` projection) elements, + always the one appearing first in the input is kept. + + > Example + Removing repeating entries. + + [1, 3, 1, 2, 2, 1] . distinct == [1, 3, 2] + + > Example + Keeping only pairs whose first elements are unique. + + [Pair 1 "a", Pair 2 "b", Pair 1 "c"] . distinct (on = _.first) == [Pair 1 "a", Pair 2 "b"] + distinct : (Any -> Any) -> Vector Any + distinct (on = x->x) = + builder = here.new_builder + existing_elements = HashSet.new + + this.each elem-> + proj = on elem + if existing_elements.contains proj then Nothing else + existing_elements.add proj + builder.append elem + + builder.to_vector + + ## UNSTABLE Transform the vector into text for displaying as part of its default diff --git a/test/Tests/src/Data/Vector_Spec.enso b/test/Tests/src/Data/Vector_Spec.enso index d1dca94ee0c4..21bf01e9b2cd 100644 --- a/test/Tests/src/Data/Vector_Spec.enso +++ b/test/Tests/src/Data/Vector_Spec.enso @@ -253,3 +253,19 @@ spec = Test.group "Vectors" <| Test.specify "should append elements" <| [1, 2].append 3 . should_equal [1, 2, 3] + Test.specify "should return a vector containing only unique elements" <| + [1, 3, 1, 2, 2, 1].distinct . should_equal [1, 3, 2] + ["a", "a", "a"].distinct . should_equal ["a"] + [].distinct . should_equal [] + + ["a", 2].distinct . should_equal ["a", 2] + [2, "a", Integer, "a", 2].distinct . should_equal [2, "a", Integer] + + structural_eq_reason="Disabled until structural equality of Atoms is properly exposed through Polyglot." + Test.specify "should correctly handle distinct with custom types like Atoms" pending=structural_eq_reason <| + [T 1 2, T 3 3, T 1 2].distinct . should_equal [T 1 2, T 3 3] + + Test.specify "should return a vector containing only unique elements up to some criteria" <| + [Pair 1 "a", Pair 2 "b", Pair 1 "c"] . distinct (on = _.first) . should_equal [Pair 1 "a", Pair 2 "b"] + +main = Test.Suite.run_main here.spec