Skip to content

Commit 84a1ee2

Browse files
committed
Document Zip iterators
1 parent e7174d8 commit 84a1ee2

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Go Report Card](https://goreportcard.com/badge/github.com/BooleanCat/go-functional/v2)](https://goreportcard.com/report/github.com/BooleanCat/go-functional/v2)
77
[![codecov](https://codecov.io/gh/BooleanCat/go-functional/branch/main/graph/badge.svg?token=N2E43RSR14)](https://codecov.io/gh/BooleanCat/go-functional)
88

9-
A library of iterators for use with `iter.Seq`. Requires Go 1.23+.
9+
A library of iterators for use with [iter.Seq](https://pkg.go.dev/iter#Seq). Requires Go 1.23+.
1010

1111
```go
1212
// The first 5 natural numbers
@@ -655,3 +655,44 @@ maps.Collect(it.Take2(slices.All([]int{1, 2, 3}), 2))
655655
// As above, but chainable
656656
itx.FromSlice([]int{1, 2, 3}).Enumerate().Take(2).Collect()
657657
```
658+
659+
### Zip, Left & Right
660+
661+
`Zip` yields pairs of values from two iterators. It is exhausted when one of the two provided
662+
iterators is exhausted.
663+
664+
```go
665+
numbers := []int{1, 2, 3}
666+
strings := []string{"one", "two", "three"}
667+
668+
maps.Collect(it.Zip(slices.Values(numbers), slices.Values(strings)))
669+
```
670+
671+
`Left` and `Right` are functions that discard the left or right values of an
672+
[iter.Seq2](https://pkg.go.dev/iter#Seq2).
673+
674+
```go
675+
slices.Collect(it.Left(slices.All([]int{1, 2, 3})))
676+
slices.Collect(it.Right(slices.All([]int{1, 2, 3})))
677+
678+
// Chainable
679+
itx.FromSlice([]int{1, 2, 3}).Enumerate().Left().Collect()
680+
itx.FromSlice([]int{1, 2, 3}).Enumerate().Right().Collect()
681+
```
682+
683+
<!-- prettier-ignore -->
684+
> [!TIP]
685+
> A common pattern when working with `Zip` iterators in other languages is to fill excess values
686+
> with some constant value. This pattern can easily to replicated here by using [Chain](#chain) and
687+
> [Repeat](#repeat):
688+
>
689+
> ```go
690+
> numbers := itx.FromSlice([]int{1, 2, 3, 4})
691+
> strings := itx.FromSlice([]string{"one", "two"})
692+
>
693+
> it.Zip(numbers, strings.Chain(it.Repeat("missing")))
694+
> ```
695+
696+
<!-- prettier-ignore -->
697+
> [!NOTE]
698+
> The `itx` package does not contain `Zip` due to limitations with Go's type system.

0 commit comments

Comments
 (0)