Skip to content

Commit

Permalink
feat: add PairSwap, PairBoth
Browse files Browse the repository at this point in the history
  • Loading branch information
Primetalk committed Oct 16, 2022
1 parent ab30b92 commit e3058bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions fun/pair.go
Expand Up @@ -14,3 +14,14 @@ func PairV1[A any, B any](p Pair[A, B]) A { return p.V1 }

// PairV2 returns the second element of the pair.
func PairV2[A any, B any](p Pair[A, B]) B { return p.V2 }

// PairBoth returns both parts of the pair.
func PairBoth[A any, B any](p Pair[A, B]) (A, B) {
return p.V1, p.V2
}

// PairSwap returns a pair with swapped parts.
func PairSwap[A any, B any](p Pair[A, B]) Pair[B, A] {
a, b := PairBoth(p)
return NewPair(b, a)
}
14 changes: 14 additions & 0 deletions fun/pair_test.go
@@ -0,0 +1,14 @@
package fun_test

import (
"testing"

"github.com/primetalk/goio/fun"
"github.com/stretchr/testify/assert"
)

func TestPairBoth(t *testing.T) {
one, a := fun.PairBoth(fun.NewPair(1, "A"))
assert.Equal(t, 1, one)
assert.Equal(t, "A", a)
}

0 comments on commit e3058bb

Please sign in to comment.