Skip to content

Commit

Permalink
Moving distinct to Map
Browse files Browse the repository at this point in the history
  • Loading branch information
jdunkerley committed Jan 19, 2022
1 parent ed0e918 commit aef32f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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 @@ -747,13 +745,12 @@ type Vector
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
this.fold Map.empty state->
item->
key = on item
if (state.get_or_else key False) then state else
builder.append item
state.insert key True

builder.to_vector

Expand Down
3 changes: 3 additions & 0 deletions test/Tests/src/Data/Vector_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ spec = Test.group "Vectors" <|
[1, 3, 1, 2, 2, 1].distinct . should_equal [1, 3, 2]
["a", "a", "a"].distinct . should_equal ["a"]
[].distinct . should_equal []
[1, 1.0, 2, 2.0].distinct . should_equal [1, 2]

structural_eq_reason="Disabled until map can support mixed types."
Test.specify "should return a vector containing only unique mixed type elements" <|
["a", 2].distinct . should_equal ["a", 2]
[2, "a", Integer, "a", 2].distinct . should_equal [2, "a", Integer]

Expand Down

0 comments on commit aef32f3

Please sign in to comment.