Skip to content

Commit

Permalink
Simplify ZipLongest and fix typo (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-laforge authored Nov 26, 2024
1 parent d8f7169 commit 1696944
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions v2/zip_longest.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
package pie

// ZipLongest will return a new slice containing pairs with elements from input slices.
// If input slices have diffrent length, missing elements will be padded with default values.
// If input slices have different length, missing elements will be padded with default values.
func ZipLongest[T1, T2 any](ss1 []T1, ss2 []T2) []Zipped[T1, T2] {
var minLen, maxLen int
var small int8

if len(ss1) <= len(ss2) {
small = 1
minLen = len(ss1)
maxLen = len(ss2)
} else {
small = 2
minLen = len(ss2)
maxLen = len(ss1)
}

ss3 := []Zipped[T1, T2]{}
for i := 0; i < minLen; i++ {
ss3 = append(ss3, Zipped[T1, T2]{ss1[i], ss2[i]})
}

if small == 1 {
var t T1
for i := minLen; i < maxLen; i++ {
ss3 = append(ss3, Zipped[T1, T2]{t, ss2[i]})
}
} else {
var t T2
for i := minLen; i < maxLen; i++ {
ss3 = append(ss3, Zipped[T1, T2]{ss1[i], t})
ss3 := make([]Zipped[T1, T2], Max([]int{len(ss1), len(ss2)}))
for i := range ss3 {
ss3[i] = Zipped[T1, T2]{
A: First(ss1),
B: First(ss2),
}
Pop(&ss1)
Pop(&ss2)
}

return ss3
Expand Down

0 comments on commit 1696944

Please sign in to comment.