Skip to content

Commit cd3e4f7

Browse files
samberCopilot
andauthored
refactor(it): move SeqToSeq2 from map.go to seq.go (#898)
* refactor(it): move SeqToSeq2 from map.go to seq.go Adds doc page for SeqToSeq2. * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 79e0b08 commit cd3e4f7

7 files changed

Lines changed: 61 additions & 32 deletions

File tree

docs/data/it-seqtoseq2.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
name: SeqToSeq2
3+
slug: seqtoseq2
4+
sourceRef: it/seq.go#L1167
5+
category: iter
6+
subCategory: sequence
7+
signatures:
8+
- "func SeqToSeq2[T any](in iter.Seq[T]) iter.Seq2[int, T]"
9+
playUrl: https://go.dev/play/p/V5wL9xY8nQr
10+
variantHelpers:
11+
- iter#sequence#seqtoseq2
12+
similarHelpers:
13+
- iter#map#seq2keytoseq
14+
- iter#map#seq2valuetoseq
15+
position: 185
16+
---
17+
18+
Converts a sequence into an indexed sequence of key-value pairs. The first element of `iter.Seq2` is the index (starting from 0, incrementing by 1 for each item).
19+
20+
```go
21+
seq := slices.Values([]string{"foo", "bar", "baz"})
22+
for i, v := range it.SeqToSeq2(seq) {
23+
fmt.Printf("%d:%s ", i, v)
24+
}
25+
// 0:foo 1:bar 2:baz
26+
```

it/map.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,6 @@ func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V
232232
}
233233
}
234234

235-
// SeqToSeq2 converts a sequence into a sequence of key-value pairs keyed by index.
236-
// Play: https://go.dev/play/p/V5wL9xY8nQr
237-
func SeqToSeq2[T any](in iter.Seq[T]) iter.Seq2[int, T] {
238-
return func(yield func(int, T) bool) {
239-
var i int
240-
for item := range in {
241-
if !yield(i, item) {
242-
return
243-
}
244-
i++
245-
}
246-
}
247-
}
248-
249235
// Seq2KeyToSeq converts a sequence of key-value pairs into a sequence of keys.
250236
// Play: https://go.dev/play/p/W6xM7zZ9oSt
251237
func Seq2KeyToSeq[K, V any](in iter.Seq2[K, V]) iter.Seq[K] {

it/map_example_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,6 @@ func ExampleFilterValues() {
156156
// Output: [foo]
157157
}
158158

159-
func ExampleSeqToSeq2() {
160-
result := maps.Collect(SeqToSeq2(slices.Values([]string{"foo", "bar", "baz"})))
161-
162-
fmt.Printf("%v %v %v %v", len(result), result[0], result[1], result[2])
163-
// Output: 3 foo bar baz
164-
}
165-
166159
func ExampleSeq2KeyToSeq() {
167160
result := slices.Collect(Seq2KeyToSeq(maps.All(map[string]int{
168161
"foo": 1,

it/map_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,6 @@ func TestFilterValues(t *testing.T) {
265265
is.Empty(slices.Collect(result2))
266266
}
267267

268-
func TestSeqToSeq2(t *testing.T) {
269-
t.Parallel()
270-
is := assert.New(t)
271-
272-
r1 := maps.Collect(SeqToSeq2(values("foo", "bar")))
273-
is.Equal(map[int]string{0: "foo", 1: "bar"}, r1)
274-
275-
r2 := maps.Collect(SeqToSeq2(values[string]()))
276-
is.Empty(r2)
277-
}
278-
279268
func TestSeq2KeyToSeq(t *testing.T) {
280269
t.Parallel()
281270
is := assert.New(t)

it/seq.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,21 @@ func TrimSuffix[T comparable, I ~func(func(T) bool)](collection I, suffix []T) I
11641164
}
11651165
}
11661166

1167+
// SeqToSeq2 converts a sequence into a sequence of key-value pairs, where the first
1168+
// element of iter.Seq2 is the index (starting from 0 and incrementing by 1 for each item).
1169+
// Play: https://go.dev/play/p/V5wL9xY8nQr
1170+
func SeqToSeq2[T any](in iter.Seq[T]) iter.Seq2[int, T] {
1171+
return func(yield func(int, T) bool) {
1172+
var i int
1173+
for item := range in {
1174+
if !yield(i, item) {
1175+
return
1176+
}
1177+
i++
1178+
}
1179+
}
1180+
}
1181+
11671182
// Buffer returns a sequence of slices, each containing up to size items read from the channel.
11681183
// The last slice may be smaller if the channel closes before filling the buffer.
11691184
// Play: https://go.dev/play/p/zDZdcCA20ut

it/seq_example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package it
55
import (
66
"fmt"
77
"iter"
8+
"maps"
89
"math"
910
"slices"
1011
"strconv"
@@ -771,3 +772,10 @@ func ExampleTrimSuffix() {
771772
// TrimSuffix with string suffix: [hello world]
772773
// TrimSuffix with suffix {5,6} (not present): [1 2 1 2 3]
773774
}
775+
776+
func ExampleSeqToSeq2() {
777+
result := maps.Collect(SeqToSeq2(slices.Values([]string{"foo", "bar", "baz"})))
778+
779+
fmt.Printf("%v %v %v %v", len(result), result[0], result[1], result[2])
780+
// Output: 3 foo bar baz
781+
}

it/seq_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package it
55
import (
66
"fmt"
77
"iter"
8+
"maps"
89
"math"
910
"slices"
1011
"strconv"
@@ -1820,3 +1821,14 @@ func TestBuffer(t *testing.T) {
18201821
batches4 := slices.Collect(Take(Buffer(RangeFrom(1, 6), 2), 1))
18211822
is.Equal([][]int{{1, 2}}, batches4)
18221823
}
1824+
1825+
func TestSeqToSeq2(t *testing.T) {
1826+
t.Parallel()
1827+
is := assert.New(t)
1828+
1829+
r1 := maps.Collect(SeqToSeq2(values("foo", "bar")))
1830+
is.Equal(map[int]string{0: "foo", 1: "bar"}, r1)
1831+
1832+
r2 := maps.Collect(SeqToSeq2(values[string]()))
1833+
is.Empty(r2)
1834+
}

0 commit comments

Comments
 (0)