Created
March 27, 2022 19:10
-
-
Save fogfish/c45374b78024128ede0cc2b462786e1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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