Skip to content

Instantly share code, notes, and snippets.

@fogfish
Created March 27, 2022 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogfish/fc53cbf0127501ac6c27037fe5443f35 to your computer and use it in GitHub Desktop.
Save fogfish/fc53cbf0127501ac6c27037fe5443f35 to your computer and use it in GitHub Desktop.
/*
UnApply2 is like contra-map function for data type T that unwrap product type
*/
type UnApply2[T, A, B any] func(T) (A, B)
/*
ProductEq2 is a container, product of type trait instances.
Here, the implementation is a shortcut due to the absence of heterogeneous lists in Golang.
*/
type ProductEq2[T, A, B any] struct {
Eq1 Eq[A]
Eq2 Eq[B]
UnApply2[T, A, B]
}
// implementation of Eq type class for the product
func (eq ProductEq2[T, A, B]) Equal(a, b T) bool {
a0, a1 := eq.UnApply2(a)
b0, b1 := eq.UnApply2(b)
return eq.Eq1.Equal(a0, b0) && eq.Eq2.Equal(a1, b1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment