-
Notifications
You must be signed in to change notification settings - Fork 272
/
zip.go
44 lines (38 loc) · 981 Bytes
/
zip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package funk
import (
"reflect"
)
// Tuple is the return type of Zip
type Tuple struct {
Element1 interface{}
Element2 interface{}
}
// Zip returns a list of tuples, where the i-th tuple contains the i-th element
// from each of the input iterables. The returned list is truncated in length
// to the length of the shortest input iterable.
func Zip(slice1 interface{}, slice2 interface{}) []Tuple {
if !IsCollection(slice1) || !IsCollection(slice2) {
panic("First parameter must be a collection")
}
var (
minLength int
inValue1 = reflect.ValueOf(slice1)
inValue2 = reflect.ValueOf(slice2)
result = []Tuple{}
length1 = inValue1.Len()
length2 = inValue2.Len()
)
if length1 <= length2 {
minLength = length1
} else {
minLength = length2
}
for i := 0; i < minLength; i++ {
newTuple := Tuple{
Element1: inValue1.Index(i).Interface(),
Element2: inValue2.Index(i).Interface(),
}
result = append(result, newTuple)
}
return result
}