-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update for Go 1.22, and add types and functions in anticipation of Go 1.23 iterators. * Update CI to Go 1.22. * Tweak docstrings.
- Loading branch information
Showing
9 changed files
with
275 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package iter | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestAll(t *testing.T) { | ||
var ( | ||
ints = Ints(1, 1) // All integers starting at 1 | ||
first5 = FirstN(ints, 5) // First 5 integers | ||
) | ||
|
||
testSeq(t, All(first5), []int{1, 2, 3, 4, 5}) | ||
} | ||
|
||
func TestAllCount(t *testing.T) { | ||
names := FromSlice([]string{"Alice", "Bob", "Carol"}) | ||
|
||
testSeq2(t, AllCount(names), []int{0, 1, 2}, []string{"Alice", "Bob", "Carol"}) | ||
} | ||
|
||
func TestAllPairs(t *testing.T) { | ||
var ( | ||
letters = FromSlice([]string{"a", "b", "c", "d"}) | ||
nums = FromSlice([]int{1, 2, 3}) | ||
pairs = Zip(letters, nums) | ||
) | ||
|
||
testSeq2(t, AllPairs(pairs), []string{"a", "b", "c", "d"}, []int{1, 2, 3, 0}) | ||
} | ||
|
||
func testSeq[T any](t *testing.T, seq Seq[T], want []T) { | ||
var got []T | ||
|
||
seq(func(val T) bool { | ||
got = append(got, val) | ||
return true | ||
}) | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func testSeq2[T, U any](t *testing.T, seq Seq2[T, U], wantT []T, wantU []U) { | ||
var ( | ||
gotT []T | ||
gotU []U | ||
) | ||
|
||
seq(func(valT T, valU U) bool { | ||
gotT = append(gotT, valT) | ||
gotU = append(gotU, valU) | ||
return true | ||
}) | ||
|
||
if !reflect.DeepEqual(gotT, wantT) { | ||
t.Errorf("got %v, want %v", gotT, wantT) | ||
} | ||
|
||
if !reflect.DeepEqual(gotU, wantU) { | ||
t.Errorf("got %v, want %v", gotU, wantU) | ||
} | ||
} |
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,51 @@ | ||
//go:build go1.23 || goexperiment.rangefunc | ||
|
||
package iter | ||
|
||
import "iter" | ||
|
||
// Pull converts the “push-style” iterator sequence seq | ||
// into a “pull-style” iterator accessed by the two functions | ||
// next and stop. | ||
// | ||
// Next returns the next value in the sequence | ||
// and a boolean indicating whether the value is valid. | ||
// When the sequence is over, next returns the zero V and false. | ||
// It is valid to call next after reaching the end of the sequence | ||
// or after calling stop. These calls will continue | ||
// to return the zero V and false. | ||
// | ||
// Stop ends the iteration. It must be called when the caller is | ||
// no longer interested in next values and next has not yet | ||
// signaled that the sequence is over (with a false boolean return). | ||
// It is valid to call stop multiple times and when next has | ||
// already returned false. | ||
// | ||
// It is an error to call next or stop from multiple goroutines | ||
// simultaneously. | ||
func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) { | ||
return iter.Pull(iter.Seq[V](seq)) | ||
} | ||
|
||
// Pull2 converts the “push-style” iterator sequence seq | ||
// into a “pull-style” iterator accessed by the two functions | ||
// next and stop. | ||
// | ||
// Next returns the next pair in the sequence | ||
// and a boolean indicating whether the pair is valid. | ||
// When the sequence is over, next returns a pair of zero values and false. | ||
// It is valid to call next after reaching the end of the sequence | ||
// or after calling stop. These calls will continue | ||
// to return a pair of zero values and false. | ||
// | ||
// Stop ends the iteration. It must be called when the caller is | ||
// no longer interested in next values and next has not yet | ||
// signaled that the sequence is over (with a false boolean return). | ||
// It is valid to call stop multiple times and when next has | ||
// already returned false. | ||
// | ||
// It is an error to call next or stop from multiple goroutines | ||
// simultaneously. | ||
func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) { | ||
return iter.Pull2(iter.Seq2[K, V](seq)) | ||
} |
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,65 @@ | ||
//go:build go1.23 || goexperiment.rangefunc | ||
|
||
package iter | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPull(t *testing.T) { | ||
var ( | ||
ints = Ints(1, 1) // All integers starting at 1 | ||
next, stop = Pull(All(ints)) | ||
want = []int{1, 2, 3, 4, 5} | ||
got []int | ||
) | ||
|
||
for range 5 { | ||
v, ok := next() | ||
if !ok { | ||
break | ||
} | ||
got = append(got, v) | ||
} | ||
stop() | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("have %v, want %v", got, want) | ||
} | ||
|
||
v, ok := next() | ||
if v != 0 || ok != false { | ||
t.Errorf("next() after stop() gives %d, %v, want 0, false", v, ok) | ||
} | ||
} | ||
|
||
func TestPull2(t *testing.T) { | ||
var ( | ||
names = FromSlice([]string{"Alice", "Bob", "Carol", "Dave"}) | ||
namelens = FromSlice([]int{5, 3, 5, 4}) | ||
pairs = Zip(names, namelens) | ||
next, stop = Pull2(AllPairs(pairs)) | ||
want = []any{ | ||
"Alice", 5, | ||
"Bob", 3, | ||
"Carol", 5, | ||
"Dave", 4, | ||
"", 0, | ||
} | ||
got []any | ||
) | ||
|
||
for { | ||
name, namelen, ok := next() | ||
got = append(got, name, namelen) | ||
if !ok { | ||
break | ||
} | ||
} | ||
stop() | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v, want %v", 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