-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate a custom iterator from go1.23
- Loading branch information
Showing
7 changed files
with
233 additions
and
3 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,55 @@ | ||
package iterators | ||
|
||
import ( | ||
"iter" | ||
"slices" | ||
"testing" | ||
) | ||
|
||
func Concatenate(seq iter.Seq[string]) string { | ||
var result string | ||
for s := range seq { | ||
result += s | ||
} | ||
return result | ||
} | ||
|
||
// annoyingly, there is no builtin way to go from seq2, to seq (e.g just get the values) | ||
func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V] { | ||
return func(yield func(V) bool) { | ||
for _, v := range seq { | ||
if !yield(v) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
// WIP! | ||
func TestConcatenate(t *testing.T) { | ||
t.Run("values of a slice", func(t *testing.T) { | ||
got := Concatenate(slices.Values([]string{"a", "b", "c"})) | ||
want := "abc" | ||
if got != want { | ||
t.Errorf("got %q want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("values of a slice backwards", func(t *testing.T) { | ||
backward := slices.Backward([]string{"a", "b", "c"}) | ||
|
||
got := Concatenate(Values(backward)) | ||
want := "cba" | ||
if got != want { | ||
t.Errorf("got %q want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("values of a slice sorted", func(t *testing.T) { | ||
got := Concatenate(slices.Values(slices.Sorted(slices.Values([]string{"c", "a", "b"})))) | ||
want := "abc" | ||
if got != want { | ||
t.Errorf("got %q want %q", got, want) | ||
} | ||
}) | ||
} |
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,81 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCountdown(t *testing.T) { | ||
|
||
t.Run("prints 3 to Go!", func(t *testing.T) { | ||
buffer := &bytes.Buffer{} | ||
Countdown(buffer, &SpyCountdownOperations{}) | ||
|
||
got := buffer.String() | ||
want := `3 | ||
2 | ||
1 | ||
Go!` | ||
|
||
if got != want { | ||
t.Errorf("got %q want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("sleep before every print", func(t *testing.T) { | ||
spySleepPrinter := &SpyCountdownOperations{} | ||
Countdown(spySleepPrinter, spySleepPrinter) | ||
|
||
want := []string{ | ||
write, | ||
sleep, | ||
write, | ||
sleep, | ||
write, | ||
sleep, | ||
write, | ||
} | ||
|
||
if !reflect.DeepEqual(want, spySleepPrinter.Calls) { | ||
t.Errorf("wanted calls %v got %v", want, spySleepPrinter.Calls) | ||
} | ||
}) | ||
} | ||
|
||
func TestConfigurableSleeper(t *testing.T) { | ||
sleepTime := 5 * time.Second | ||
|
||
spyTime := &SpyTime{} | ||
sleeper := ConfigurableSleeper{sleepTime, spyTime.Sleep} | ||
sleeper.Sleep() | ||
|
||
if spyTime.durationSlept != sleepTime { | ||
t.Errorf("should have slept for %v but slept for %v", sleepTime, spyTime.durationSlept) | ||
} | ||
} | ||
|
||
type SpyCountdownOperations struct { | ||
Calls []string | ||
} | ||
|
||
func (s *SpyCountdownOperations) Sleep() { | ||
s.Calls = append(s.Calls, sleep) | ||
} | ||
|
||
func (s *SpyCountdownOperations) Write(p []byte) (n int, err error) { | ||
s.Calls = append(s.Calls, write) | ||
return | ||
} | ||
|
||
const write = "write" | ||
const sleep = "sleep" | ||
|
||
type SpyTime struct { | ||
durationSlept time.Duration | ||
} | ||
|
||
func (s *SpyTime) Sleep(duration time.Duration) { | ||
s.durationSlept = duration | ||
} |
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 main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"iter" | ||
"os" | ||
"time" | ||
) | ||
|
||
// Sleeper allows you to put delays. | ||
type Sleeper interface { | ||
Sleep() | ||
} | ||
|
||
// ConfigurableSleeper is an implementation of Sleeper with a defined delay. | ||
type ConfigurableSleeper struct { | ||
duration time.Duration | ||
sleep func(time.Duration) | ||
} | ||
|
||
// Sleep will pause execution for the defined Duration. | ||
func (c *ConfigurableSleeper) Sleep() { | ||
c.sleep(c.duration) | ||
} | ||
|
||
const finalWord = "Go!" | ||
|
||
// Countdown prints a countdown from 3 to out with a delay between count provided by Sleeper. | ||
func Countdown(out io.Writer, sleeper Sleeper) { | ||
for i := range countDownFrom(3) { | ||
fmt.Fprintln(out, i) | ||
sleeper.Sleep() | ||
} | ||
|
||
fmt.Fprint(out, finalWord) | ||
} | ||
|
||
func countDownFrom(from int) iter.Seq[int] { | ||
return func(yield func(int) bool) { | ||
for i := from; i > 0; i-- { | ||
if !yield(i) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
sleeper := &ConfigurableSleeper{1 * time.Second, time.Sleep} | ||
Countdown(os.Stdout, sleeper) | ||
} |