-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: redo event filtering as a sequence of iterators
This should be both easier to understand and debug. Replace this stuff with `range` loops once Go 1.23 lands. Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
- Loading branch information
Showing
10 changed files
with
600 additions
and
125 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
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
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
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,57 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// Package iter provides utilities for working with iterators. | ||
package iter | ||
|
||
// Seq is a sequence of elements. | ||
type Seq[T any] func(yield func(T) bool) | ||
|
||
// Deduplicate yields elements from elems, skipping duplicates. It always yields the last equal element. | ||
// The equal function is used to compare elements. | ||
// The yield function is used to yield elements. If it returns false, the iteration stops. | ||
// Slice should be sorted before calling this function. | ||
func Deduplicate[T any](elems []T, equal func(a, b T) bool) Seq[T] { | ||
return func(yield func(T) bool) { | ||
switch len(elems) { | ||
case 1: | ||
yield(elems[0]) | ||
|
||
fallthrough | ||
case 0: | ||
return | ||
} | ||
|
||
last := elems[0] | ||
for _, elem := range elems[1:] { | ||
if equal(last, elem) { | ||
last = elem | ||
|
||
continue | ||
} | ||
|
||
if !yield(last) { | ||
return | ||
} | ||
|
||
last = elem | ||
} | ||
|
||
yield(last) | ||
} | ||
} | ||
|
||
// Filter iterates over elements in seq, calling the given function for each element. | ||
// If the function returns true, the element is yielded. | ||
func Filter[T any](seq Seq[T], fn func(T) bool) Seq[T] { | ||
return func(yield func(T) bool) { | ||
seq(func(elem T) bool { | ||
if fn(elem) { | ||
return yield(elem) | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
} |
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,78 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package iter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/siderolink/pkg/iter" | ||
) | ||
|
||
func TestDeduplicate(t *testing.T) { | ||
type elem struct { //nolint:govet | ||
value int | ||
name string | ||
} | ||
|
||
tests := map[string]struct { | ||
elems []elem | ||
expected []string | ||
}{ | ||
"empty": {}, | ||
"single": { | ||
elems: []elem{ | ||
{1, "a"}, | ||
}, | ||
expected: []string{"a"}, | ||
}, | ||
"multiple equal": { | ||
elems: []elem{ | ||
{1, "a"}, | ||
{1, "b"}, | ||
{1, "c"}, | ||
}, | ||
expected: []string{"c"}, | ||
}, | ||
"two different": { | ||
elems: []elem{ | ||
{1, "a"}, | ||
{1, "b"}, | ||
{1, "c"}, | ||
{2, "d"}, | ||
{2, "e"}, | ||
{2, "f"}, | ||
}, | ||
expected: []string{"c", "f"}, | ||
}, | ||
"three different": { | ||
elems: []elem{ | ||
{1, "a"}, | ||
{2, "d"}, | ||
{2, "e"}, | ||
{2, "f"}, | ||
{3, "g"}, | ||
}, | ||
expected: []string{"a", "f", "g"}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
it := iter.Deduplicate(test.elems, func(a, b elem) bool { return a.value == b.value }) | ||
|
||
var result []string | ||
|
||
it(func(elem elem) bool { | ||
result = append(result, elem.name) | ||
|
||
return true | ||
}) | ||
|
||
require.Equal(t, test.expected, result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.