Skip to content

Commit

Permalink
Add convenience functions for getting Go native tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
w0rp committed Aug 5, 2023
1 parent ba85253 commit 9c3f998
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ type Pair[A, B any] struct {
B B
}

// Get returns items as a Go tuple
func (p Pair[A, B]) Get() (A, B) {
return p.A, p.B
}

// MakePair creates a Pair
func MakePair[A, B any](a A, b B) Pair[A, B] {
return Pair[A, B]{a, b}
Expand All @@ -20,6 +25,11 @@ type Triplet[A, B, C any] struct {
C C
}

// Get returns items as a Go tuple
func (t Triplet[A, B, C]) Get() (A, B, C) {
return t.A, t.B, t.C
}

// MakeTriplet creates a Triplet
func MakeTriplet[A, B, C any](a A, b B, c C) Triplet[A, B, C] {
return Triplet[A, B, C]{a, b, c}
Expand All @@ -33,6 +43,11 @@ type Quartet[A, B, C, D any] struct {
D D
}

// Get returns items as a Go tuple
func (q Quartet[A, B, C, D]) Get() (A, B, C, D) {
return q.A, q.B, q.C, q.D
}

// MakeQuartet creates a Quartet
func MakeQuartet[A, B, C, D any](a A, b B, c C, d D) Quartet[A, B, C, D] {
return Quartet[A, B, C, D]{a, b, c, d}
Expand All @@ -47,6 +62,11 @@ type Quintet[A, B, C, D, E any] struct {
E E
}

// Get returns items as a Go tuple
func (q Quintet[A, B, C, D, E]) Get() (A, B, C, D, E) {
return q.A, q.B, q.C, q.D, q.E
}

// MakeQuintet creates a Quintet
func MakeQuintet[A, B, C, D, E any](a A, b B, c C, d D, e E) Quintet[A, B, C, D, E] {
return Quintet[A, B, C, D, E]{a, b, c, d, e}
Expand All @@ -62,6 +82,11 @@ type Sextet[A, B, C, D, E, F any] struct {
F F
}

// Get returns items as a Go tuple
func (s Sextet[A, B, C, D, E, F]) Get() (A, B, C, D, E, F) {
return s.A, s.B, s.C, s.D, s.E, s.F
}

// MakeSextet creates a Sextet
func MakeSextet[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) Sextet[A, B, C, D, E, F] {
return Sextet[A, B, C, D, E, F]{a, b, c, d, e, f}
Expand All @@ -78,6 +103,11 @@ type Septet[A, B, C, D, E, F, G any] struct {
G G
}

// Get returns items as a Go tuple
func (s Septet[A, B, C, D, E, F, G]) Get() (A, B, C, D, E, F, G) {
return s.A, s.B, s.C, s.D, s.E, s.F, s.G
}

// MakeSeptet creates a Septet
func MakeSeptet[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) Septet[A, B, C, D, E, F, G] {
return Septet[A, B, C, D, E, F, G]{a, b, c, d, e, f, g}
Expand All @@ -95,6 +125,11 @@ type Octet[A, B, C, D, E, F, G, H any] struct {
H H
}

// Get returns items as a Go tuple
func (o Octet[A, B, C, D, E, F, G, H]) Get() (A, B, C, D, E, F, G, H) {
return o.A, o.B, o.C, o.D, o.E, o.F, o.G, o.H
}

// MakeOctet creates a Octet
func MakeOctet[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) Octet[A, B, C, D, E, F, G, H] {
return Octet[A, B, C, D, E, F, G, H]{a, b, c, d, e, f, g, h}
Expand All @@ -113,6 +148,11 @@ type Ennead[A, B, C, D, E, F, G, H, I any] struct {
I I
}

// Get returns items as a Go tuple
func (e Ennead[A, B, C, D, E, F, G, H, I]) Get() (A, B, C, D, E, F, G, H, I) {
return e.A, e.B, e.C, e.D, e.E, e.F, e.G, e.H, e.I
}

// MakeEnnead creates a Ennead
func MakeEnnead[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Ennead[A, B, C, D, E, F, G, H, I] {
return Ennead[A, B, C, D, E, F, G, H, I]{a, b, c, d, e, f, g, h, i}
Expand All @@ -132,6 +172,11 @@ type Decade[A, B, C, D, E, F, G, H, I, J any] struct {
J J
}

// Get returns items as a Go tuple
func (d Decade[A, B, C, D, E, F, G, H, I, J]) Get() (A, B, C, D, E, F, G, H, I, J) {
return d.A, d.B, d.C, d.D, d.E, d.F, d.G, d.H, d.I, d.J
}

// MakeDecade creates a Decade
func MakeDecade[A, B, C, D, E, F, G, H, I, J any](a A, b B, c C, d D, e E, f F, g G, h H, i I, j J) Decade[A, B, C, D, E, F, G, H, I, J] {
return Decade[A, B, C, D, E, F, G, H, I, J]{a, b, c, d, e, f, g, h, i, j}
Expand Down
120 changes: 120 additions & 0 deletions tuple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package ranges

import "testing"

func TestPair(t *testing.T) {
t.Parallel()

a, b := MakePair('a', 2).Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
}

func TestTriplet(t *testing.T) {
t.Parallel()

a, b, c := MakeTriplet('a', 2, 'c').Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
}

func TestQuartet(t *testing.T) {
t.Parallel()

a, b, c, d := MakeQuartet('a', 2, 'c', 4).Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
}

func TestQuintet(t *testing.T) {
t.Parallel()

a, b, c, d, e := MakeQuintet('a', 2, 'c', 4, 'e').Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
assertEqual(t, e, 'e')
}

func TestSextet(t *testing.T) {
t.Parallel()

a, b, c, d, e, f := MakeSextet('a', 2, 'c', 4, 'e', 6).Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
assertEqual(t, e, 'e')
assertEqual(t, f, 6)
}

func TestSeptet(t *testing.T) {
t.Parallel()

a, b, c, d, e, f, g := MakeSeptet('a', 2, 'c', 4, 'e', 6, 'g').Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
assertEqual(t, e, 'e')
assertEqual(t, f, 6)
assertEqual(t, g, 'g')
}

func TestOctet(t *testing.T) {
t.Parallel()

a, b, c, d, e, f, g, h := MakeOctet('a', 2, 'c', 4, 'e', 6, 'g', 8).Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
assertEqual(t, e, 'e')
assertEqual(t, f, 6)
assertEqual(t, g, 'g')
assertEqual(t, h, 8)
}

func TestEnnead(t *testing.T) {
t.Parallel()

a, b, c, d, e, f, g, h, i := MakeEnnead('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i').Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
assertEqual(t, e, 'e')
assertEqual(t, f, 6)
assertEqual(t, g, 'g')
assertEqual(t, h, 8)
assertEqual(t, i, 'i')
}

func TestDecade(t *testing.T) {
t.Parallel()

a, b, c, d, e, f, g, h, i, j := MakeDecade('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10).Get()

assertEqual(t, a, 'a')
assertEqual(t, b, 2)
assertEqual(t, c, 'c')
assertEqual(t, d, 4)
assertEqual(t, e, 'e')
assertEqual(t, f, 6)
assertEqual(t, g, 'g')
assertEqual(t, h, 8)
assertEqual(t, i, 'i')
assertEqual(t, j, 10)
}

0 comments on commit 9c3f998

Please sign in to comment.