-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheq.go
51 lines (41 loc) · 1.14 KB
/
eq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//
package eq
import (
"github.com/fogfish/golem/pure"
)
// Eq : T ⟼ T ⟼ bool
// Each trait implements mapping pair of values to bool category using own
// equality rules
type Eq[T any] interface {
Equal(T, T) bool
}
// eq generic implementation for built-in types
type eq[T comparable] string
func (eq[T]) Equal(a, b T) bool { return a == b }
const (
Int = eq[int]("eq.int")
String = eq[string]("eq.string")
)
// From is a combinator that lifts T ⟼ T ⟼ bool function to
// an instance of Eq type trait
type From[T any] func(T, T) bool
func (f From[T]) Equal(a, b T) bool { return f(a, b) }
// ContraMap is a combinator that build a new instance of type trait Eq[B] using
// existing instance of Eq[A] and f: b ⟼ a
type ContraMap[A, B any] struct {
Eq[A]
pure.ContraMap[A, B]
}
// Equal implementation of contra variant functor
func (f ContraMap[A, B]) Equal(a, b B) bool {
return f.Eq.Equal(
f.ContraMap(a),
f.ContraMap(b),
)
}