elliotchance / pie

🍕 Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.
https://pkg.go.dev/github.com/elliotchance/pie/v2
MIT License
1.96k stars 91 forks source link

Add Zip function #193

Closed chocolacula closed 1 year ago

chocolacula commented 1 year ago

With Zip and ZipLongest function we can do:

a := []int{1, 2, 3, 4, 5}
b := []float32{1.1, 2.2, 3.3, 4.4, 5.5, 6.6}

for _, p := range pie.Zip(a, b) {
    fmt.Println(p)
}
for _, p := range pie.ZipLongest(a, b) {
    fmt.Println(p)
}
// {1 1.1}
// {2 2.2}
// {3 3.3}
// {4 4.4}
// {5 5.5}
// {0 6.6} <- 0 is default value not in original slice

It is the same as zip() and itertools.zip_longest in Python

a = [1, 2, 3, 4, 5]
b = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]

for p in zip(a, b):
    print(p)
a = [1, 2, 3, 4, 5]
b = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]

for p in itertools.zip_longest(a, b, fillvalue=0):
    print(p)
chocolacula commented 1 year ago

Unfortunately I don't see a possibility to add Zip to OfSlice because we need two generic parameters.

chocolacula commented 1 year ago

Didn't see your last comment, sorry. I forget about that test, will fix it soon

elliotchance commented 1 year ago

Thanks! https://github.com/elliotchance/pie/releases/tag/v2.6.0