-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package it | ||
|
||
import "iter" | ||
|
||
// FilterUnique yields all the unique values from an iterator. | ||
// | ||
// Note: All unique values seen from an iterator are stored in memory. | ||
func FilterUnique[V comparable](iterator func(func(V) bool)) iter.Seq[V] { | ||
return func(yield func(V) bool) { | ||
seen := make(map[V]struct{}) | ||
|
||
for value := range iterator { | ||
if _, ok := seen[value]; ok { | ||
continue | ||
} | ||
|
||
seen[value] = struct{}{} | ||
if !yield(value) { | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package it_test | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"testing" | ||
|
||
"github.com/BooleanCat/go-functional/v2/internal/assert" | ||
"github.com/BooleanCat/go-functional/v2/it" | ||
) | ||
|
||
func ExampleFilterUnique() { | ||
for number := range it.FilterUnique(slices.Values([]int{1, 2, 2, 3, 3, 3, 4})) { | ||
fmt.Println(number) | ||
} | ||
|
||
// Output: | ||
// 1 | ||
// 2 | ||
// 3 | ||
// 4 | ||
} | ||
|
||
func TestFilterUniqueEmpty(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Empty[int](t, slices.Collect(it.Exhausted[int]())) | ||
} | ||
|
||
func TestFilterUniqueYieldFalse(t *testing.T) { | ||
t.Parallel() | ||
|
||
iterator := it.FilterUnique(slices.Values([]int{100, 200, 300})) | ||
|
||
iterator(func(v int) bool { | ||
return false | ||
}) | ||
} | ||
|
||
func TestFilterUniqueWithNoDuplicates(t *testing.T) { | ||
t.Parallel() | ||
|
||
numbers := slices.Collect(it.FilterUnique(slices.Values([]int{1, 2, 3}))) | ||
assert.SliceEqual(t, []int{1, 2, 3}, numbers) | ||
} | ||
|
||
func TestFilterUniqueWithDuplicates(t *testing.T) { | ||
t.Parallel() | ||
|
||
strings := slices.Collect(it.FilterUnique(slices.Values([]string{"hello", "world", "hello", "world", "hello"}))) | ||
assert.SliceEqual(t, []string{"hello", "world"}, strings) | ||
} |