generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: window function to be a proper window function, stateIter to r…
…eturn always the current state as the first element
- Loading branch information
Showing
16 changed files
with
233 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package channels | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
"testing" | ||
"time" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestIterContext(t *testing.T) { | ||
t.Run("iterates until channel closed", func(t *testing.T) { | ||
ch := make(chan int) | ||
ctx := context.Background() | ||
|
||
// Start goroutine to send values | ||
go func() { | ||
ch <- 1 | ||
ch <- 2 | ||
ch <- 3 | ||
close(ch) | ||
}() | ||
|
||
assert.Equal(t, []int{1, 2, 3}, slices.Collect(IterContext(ctx, ch))) | ||
}) | ||
|
||
t.Run("stops when context cancelled", func(t *testing.T) { | ||
ch := make(chan int) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
// Start goroutine to send values | ||
go func() { | ||
ch <- 1 | ||
ch <- 2 | ||
time.Sleep(10 * time.Millisecond) // Small delay to ensure cancel happens | ||
cancel() // Cancel context before sending 3 | ||
ch <- 3 // This should not be received | ||
close(ch) | ||
}() | ||
|
||
assert.Equal(t, []int{1, 2}, slices.Collect(IterContext(ctx, ch))) | ||
assert.Error(t, ctx.Err()) | ||
}) | ||
} |
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,15 @@ | ||
package iterops | ||
|
||
import "iter" | ||
|
||
func Concat[T any](in ...iter.Seq[T]) iter.Seq[T] { | ||
return func(yield func(T) bool) { | ||
for _, n := range in { | ||
for m := range n { | ||
if !yield(m) { | ||
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,13 @@ | ||
package iterops | ||
|
||
import "iter" | ||
|
||
func Const[T any](in ...T) iter.Seq[T] { | ||
return func(yield func(T) bool) { | ||
for _, n := range in { | ||
if !yield(n) { | ||
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,20 @@ | ||
package iterops | ||
|
||
import ( | ||
"iter" | ||
"reflect" | ||
) | ||
|
||
// Dedup returns an iterator that yields values from the input iterator, removing consecutive duplicates. | ||
func Dedup[T any](seq iter.Seq[T]) iter.Seq[T] { | ||
return func(yield func(T) bool) { | ||
var last T | ||
seq(func(v T) bool { | ||
if reflect.DeepEqual(v, last) { | ||
return true | ||
} | ||
last = v | ||
return yield(v) | ||
}) | ||
} | ||
} |
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,8 @@ | ||
package iterops | ||
|
||
import "iter" | ||
|
||
// Empty returns an empty iterator. | ||
func Empty[T any]() iter.Seq[T] { | ||
return func(yield func(T) bool) {} | ||
} |
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
Oops, something went wrong.