Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to create tuple from a list of values #93

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Supported helpers for maps:

Supported helpers for tuples:

- T2 -> T9
- Zip2 -> Zip9
- Unzip2 -> Unzip9

Expand Down Expand Up @@ -647,6 +648,19 @@ m2 := lo.MapValues[int, int64, string](m, func(x int64, _ int) string {
// map[int]string{1: "1", 2: "2", 3: "3"}
```

### T2 -> T9

Creates a tuple from a list of values.

```go
tuple1 := lo.T2[string, int]("x", 1)
// Tuple2[string, int]{A: "x", B: 1}

func example() (string, int) { return "y", 2 }
tuple2 := lo.T2[string, int](example())
// Tuple2[string, int]{A: "y", B: 2}
```

### Zip2 -> Zip9

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.
Expand Down
40 changes: 40 additions & 0 deletions tuples.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package lo

// T2 creates a tuple from a list of values.
func T2[A any, B any](a A, b B) Tuple2[A, B] {
return Tuple2[A, B]{A: a, B: b}
}

// T3 creates a tuple from a list of values.
func T3[A any, B any, C any](a A, b B, c C) Tuple3[A, B, C] {
return Tuple3[A, B, C]{A: a, B: b, C: c}
}

// T4 creates a tuple from a list of values.
func T4[A any, B any, C any, D any](a A, b B, c C, d D) Tuple4[A, B, C, D] {
return Tuple4[A, B, C, D]{A: a, B: b, C: c, D: d}
}

// T5 creates a tuple from a list of values.
func T5[A any, B any, C any, D any, E any](a A, b B, c C, d D, e E) Tuple5[A, B, C, D, E] {
return Tuple5[A, B, C, D, E]{A: a, B: b, C: c, D: d, E: e}
}

// T6 creates a tuple from a list of values.
func T6[A any, B any, C any, D any, E any, F any](a A, b B, c C, d D, e E, f F) Tuple6[A, B, C, D, E, F] {
return Tuple6[A, B, C, D, E, F]{A: a, B: b, C: c, D: d, E: e, F: f}
}

// T7 creates a tuple from a list of values.
func T7[A any, B any, C any, D any, E any, F any, G any](a A, b B, c C, d D, e E, f F, g G) Tuple7[A, B, C, D, E, F, G] {
return Tuple7[A, B, C, D, E, F, G]{A: a, B: b, C: c, D: d, E: e, F: f, G: g}
}

// T8 creates a tuple from a list of values.
func T8[A any, B any, C any, D any, E any, F any, G any, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H] {
return Tuple8[A, B, C, D, E, F, G, H]{A: a, B: b, C: c, D: d, E: e, F: f, G: g, H: h}
}

// T8 creates a tuple from a list of values.
func T9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Tuple9[A, B, C, D, E, F, G, H, I] {
return Tuple9[A, B, C, D, E, F, G, H, I]{A: a, B: b, C: c, D: d, E: e, F: f, G: g, H: h, I: i}
}

// Zip2 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.
// When collections have different size, the Tuple attributes are filled with zero value.
Expand Down
22 changes: 22 additions & 0 deletions tuples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ import (
"github.com/stretchr/testify/assert"
)

func TestT(t *testing.T) {
is := assert.New(t)

r1 := T2[string, int]("a", 1)
r2 := T3[string, int, float32]("b", 2, 3.0)
r3 := T4[string, int, float32, bool]("c", 3, 4.0, true)
r4 := T5[string, int, float32, bool, string]("d", 4, 5.0, false, "e")
r5 := T6[string, int, float32, bool, string, int]("f", 5, 6.0, true, "g", 7)
r6 := T7[string, int, float32, bool, string, int, float64]("h", 6, 7.0, false, "i", 8, 9.0)
r7 := T8[string, int, float32, bool, string, int, float64, bool]("j", 7, 8.0, true, "k", 9, 10.0, false)
r8 := T9[string, int, float32, bool, string, int, float64, bool, string]("l", 8, 9.0, false, "m", 10, 11.0, true, "n")

is.Equal(r1, Tuple2[string, int]{A: "a", B: 1})
is.Equal(r2, Tuple3[string, int, float32]{A: "b", B: 2, C: 3.0})
is.Equal(r3, Tuple4[string, int, float32, bool]{A: "c", B: 3, C: 4.0, D: true})
is.Equal(r4, Tuple5[string, int, float32, bool, string]{A: "d", B: 4, C: 5.0, D: false, E: "e"})
is.Equal(r5, Tuple6[string, int, float32, bool, string, int]{A: "f", B: 5, C: 6.0, D: true, E: "g", F: 7})
is.Equal(r6, Tuple7[string, int, float32, bool, string, int, float64]{A: "h", B: 6, C: 7.0, D: false, E: "i", F: 8, G: 9.0})
is.Equal(r7, Tuple8[string, int, float32, bool, string, int, float64, bool]{A: "j", B: 7, C: 8.0, D: true, E: "k", F: 9, G: 10.0, H: false})
is.Equal(r8, Tuple9[string, int, float32, bool, string, int, float64, bool, string]{A: "l", B: 8, C: 9.0, D: false, E: "m", F: 10, G: 11.0, H: true, I: "n"})
}

func TestZip(t *testing.T) {
is := assert.New(t)

Expand Down