Skip to content

Commit

Permalink
frontend: Implement FromIterator for HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Feb 15, 2025
1 parent e4ea185 commit add4dfd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkgs/std/collections.dora
Original file line number Diff line number Diff line change
Expand Up @@ -1033,17 +1033,17 @@ impl[K: Hash + Equals] std::traits::IntoIterator for HashSet[K] {
}
}

// impl[X] FromIterator[X] for HashSet[X] {
// static fn fromIter[T](iter: T): HashSet[X] where T: Iterator[Item=X] {
// let result = HashSet[X]::new();
impl[X: Hash + Equals] FromIterator[X] for HashSet[X] {
static fn fromIter[T](iter: T): HashSet[X] where T: Iterator[Item=X] {
let result = HashSet[X]::new();

// while iter.next() is Some(value) {
// result.insert(value);
// }
while iter.next() is Some(value) {
result.insert(value);
}

// result
// }
// }
result
}
}

pub class HashSetIter[K: Hash + Equals] {
map: HashMap[K, ()],
Expand Down
7 changes: 7 additions & 0 deletions tests/stdlib/iterator-collect.dora
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

fn main() {
let initial = Vec[Int]::new(1, 2, 3, 4, 5);
let x = initial.iter().collect[Vec[Int]]();
Expand All @@ -8,4 +10,9 @@ fn main() {
let x = initial.iter().collect[Array[Int]]();
assert(initial !== x);
assert(initial == x);

let initial = Vec[Int]::new(1, 5, 3, 1, 5);
let x = initial.iter().collect[HashSet[Int]]().iter().collect[Vec[Int]]();
x.sort();
assert(x == Vec[Int]::new(1, 3, 5));
}

0 comments on commit add4dfd

Please sign in to comment.