feat: adding Unpack() method to TupleX

This commit is contained in:
Samuel Berthe
2022-10-10 22:01:46 +02:00
parent aca846b80d
commit eb5207b4b3
2 changed files with 48 additions and 0 deletions

View File

@@ -1232,6 +1232,14 @@ r1, r2 := lo.Unpack2[string, int](lo.Tuple2[string, int]{"a", 1})
// "a", 1 // "a", 1
``` ```
Unpack is also available as a method of TupleX.
```go
tuple2 := lo.T2("a", 1)
a, b := tuple2.Unpack()
// "a" 1
```
[[play](https://go.dev/play/p/xVP_k0kJ96W)] [[play](https://go.dev/play/p/xVP_k0kJ96W)]
### Zip2 -> Zip9 ### Zip2 -> Zip9

View File

@@ -12,6 +12,11 @@ type Tuple2[A any, B any] struct {
B B B B
} }
// Unpack returns values contained in tuple.
func (t Tuple2[A, B]) Unpack() (A, B) {
return t.A, t.B
}
// Tuple3 is a group of 3 elements. // Tuple3 is a group of 3 elements.
type Tuple3[A any, B any, C any] struct { type Tuple3[A any, B any, C any] struct {
A A A A
@@ -19,6 +24,11 @@ type Tuple3[A any, B any, C any] struct {
C C C C
} }
// Unpack returns values contained in tuple.
func (t Tuple3[A, B, C]) Unpack() (A, B, C) {
return t.A, t.B, t.C
}
// Tuple4 is a group of 4 elements. // Tuple4 is a group of 4 elements.
type Tuple4[A any, B any, C any, D any] struct { type Tuple4[A any, B any, C any, D any] struct {
A A A A
@@ -27,6 +37,11 @@ type Tuple4[A any, B any, C any, D any] struct {
D D D D
} }
// Unpack returns values contained in tuple.
func (t Tuple4[A, B, C, D]) Unpack() (A, B, C, D) {
return t.A, t.B, t.C, t.D
}
// Tuple5 is a group of 5 elements. // Tuple5 is a group of 5 elements.
type Tuple5[A any, B any, C any, D any, E any] struct { type Tuple5[A any, B any, C any, D any, E any] struct {
A A A A
@@ -36,6 +51,11 @@ type Tuple5[A any, B any, C any, D any, E any] struct {
E E E E
} }
// Unpack returns values contained in tuple.
func (t Tuple5[A, B, C, D, E]) Unpack() (A, B, C, D, E) {
return t.A, t.B, t.C, t.D, t.E
}
// Tuple6 is a group of 6 elements. // Tuple6 is a group of 6 elements.
type Tuple6[A any, B any, C any, D any, E any, F any] struct { type Tuple6[A any, B any, C any, D any, E any, F any] struct {
A A A A
@@ -46,6 +66,11 @@ type Tuple6[A any, B any, C any, D any, E any, F any] struct {
F F F F
} }
// Unpack returns values contained in tuple.
func (t Tuple6[A, B, C, D, E, F]) Unpack() (A, B, C, D, E, F) {
return t.A, t.B, t.C, t.D, t.E, t.F
}
// Tuple7 is a group of 7 elements. // Tuple7 is a group of 7 elements.
type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct { type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct {
A A A A
@@ -57,6 +82,11 @@ type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct {
G G G G
} }
// Unpack returns values contained in tuple.
func (t Tuple7[A, B, C, D, E, F, G]) Unpack() (A, B, C, D, E, F, G) {
return t.A, t.B, t.C, t.D, t.E, t.F, t.G
}
// Tuple8 is a group of 8 elements. // Tuple8 is a group of 8 elements.
type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct { type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct {
A A A A
@@ -69,6 +99,11 @@ type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct {
H H H H
} }
// Unpack returns values contained in tuple.
func (t Tuple8[A, B, C, D, E, F, G, H]) Unpack() (A, B, C, D, E, F, G, H) {
return t.A, t.B, t.C, t.D, t.E, t.F, t.G, t.H
}
// Tuple9 is a group of 9 elements. // Tuple9 is a group of 9 elements.
type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struct { type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struct {
A A A A
@@ -81,3 +116,8 @@ type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struc
H H H H
I I I I
} }
// Unpack returns values contained in tuple.
func (t Tuple9[A, B, C, D, E, F, G, H, I]) Unpack() (A, B, C, D, E, F, G, H, I) {
return t.A, t.B, t.C, t.D, t.E, t.F, t.G, t.H, t.I
}