Skip to content

Commit a482eff

Browse files
committed
feat: adding tuple types (from 2 to 9) and zip+unzip functions
1 parent 4edabde commit a482eff

File tree

6 files changed

+549
-6
lines changed

6 files changed

+549
-6
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Supported helpers for maps:
6969
- FromEntries
7070
- Assign (maps merge)
7171

72+
Supported helpers for tuples:
73+
74+
- Zip2 -> Zip9
75+
- Unzip2 -> Unzip9
76+
7277
Supported intersection helpers:
7378

7479
- Contains
@@ -391,6 +396,27 @@ mergedMaps := lo.Assign[string, int](
391396
// map[string]int{"a": 1, "b": 3, "c": 4}
392397
```
393398

399+
### Zip2 -> Zip9
400+
401+
Zip creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
402+
403+
When collections have different size, the Tuple attributes are filled with zero value.
404+
405+
```go
406+
tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2})
407+
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
408+
```
409+
410+
### Unzip2 -> Unzip9
411+
412+
Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
413+
414+
```go
415+
a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
416+
// []string{"a", "b"}
417+
// []int{1, 2}
418+
```
419+
394420
### Every
395421

396422
Returns true if all elements of a subset are contained into a collection.

constraints.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package lo
22

3+
// Clonable defines a constraint of types having Clone() T method.
34
type Clonable[T any] interface {
45
Clone() T
56
}

map.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ func Values[K comparable, V any](in map[K]V) []V {
2222
return result
2323
}
2424

25-
// Entry defines a key/value pairs.
26-
type Entry[K comparable, V any] struct {
27-
Key K
28-
Value V
29-
}
30-
3125
// Entries transforms a map into array of key/value pairs.
3226
func Entries[K comparable, V any](in map[K]V) []Entry[K, V] {
3327
entries := make([]Entry[K, V], 0, len(in))

0 commit comments

Comments
 (0)