|
6 | 6 | [](https://goreportcard.com/report/github.com/BooleanCat/go-functional/v2) |
7 | 7 | [](https://codecov.io/gh/BooleanCat/go-functional) |
8 | 8 |
|
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+. |
10 | 10 |
|
11 | 11 | ```go |
12 | 12 | // The first 5 natural numbers |
@@ -655,3 +655,44 @@ maps.Collect(it.Take2(slices.All([]int{1, 2, 3}), 2)) |
655 | 655 | // As above, but chainable |
656 | 656 | itx.FromSlice([]int{1, 2, 3}).Enumerate().Take(2).Collect() |
657 | 657 | ``` |
| 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