Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The user should be able to remove duplicate elements from a Vector #3224

Merged
merged 6 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/gui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/Tests/src/Data/Vector_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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