-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlifting.go
48 lines (36 loc) · 960 Bytes
/
lifting.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
package main
import "fmt"
/*
Eq : T ⟼ T ⟼ bool
Each type implements own equality, mapping pair of value to bool category
*/
type Eq[T any] interface {
Equal(T, T) bool
}
/*
FromEq is a combinator that lifts T ⟼ T ⟼ bool function to
an instance of Eq type trait
*/
type FromEq[T any] func(T, T) bool
// implementation of Eq type class
func (f FromEq[T]) Equal(a, b T) bool { return f(a, b) }
// Equal is a pure function that compares two integers
func Equal(a, b int) bool { return a == b }
/*
Haystack is an example of algorithms that uses type law
*/
type Haystack[T any] struct{ Eq[T] }
func (h Haystack[T]) Lookup(e T, seq []T) {
for _, x := range seq {
if h.Eq.Equal(e, x) {
fmt.Printf("needle %v found at %v\n", e, seq)
return
}
}
fmt.Printf("needle %v is not found at %v\n", e, seq)
}
func main() {
haystack := Haystack[int]{FromEq[int](Equal)}
haystack.Lookup(2, []int{1, 2, 3})
haystack.Lookup(5, []int{1, 2, 3})
}