Skip to content

Instantly share code, notes, and snippets.

@fogfish
Created March 27, 2022 19:10
Show Gist options
  • Save fogfish/c45374b78024128ede0cc2b462786e1d to your computer and use it in GitHub Desktop.
Save fogfish/c45374b78024128ede0cc2b462786e1d to your computer and use it in GitHub Desktop.
/*
ContraMapEq is a combinator that build a new instance of type trait Eq[B] using
existing instance of Eq[A] and f: b ⟼ a
*/
type ContraMapEq[A, B any] struct{ Eq[A] }
// implementation of contra variant functor
func (c ContraMapEq[A, B]) FMap(f func(B) A) Eq[B] {
return FromEq[B](func(a, b B) bool {
return c.Eq.Equal(f(a), f(b))
})
}
/*
Use the combinator to make an instance of `Eq[ExampleType]`
*/
ContraMapEq[int, ExampleType]{Int}.FMap(
func(x ExampleType) int { /* ... */ },
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment