-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
51 lines (43 loc) · 1.08 KB
/
types.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) 2024 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/kshard/vector
//
package vector
import (
"github.com/fogfish/golem/pure"
"github.com/fogfish/golem/pure/eq"
)
// Vector of float32
type F32 = []float32
// Generic trait to estimate "distance" between two vectors
type Distance[Vector any] interface {
Distance(Vector, Vector) float32
}
// Generic trait defines vector category
type Surface[Vector any] interface {
eq.Eq[Vector]
Distance[Vector]
}
// ContraMap is a combinator that build a new instance of type trait Surface[V] using
// existing instance of Distance[A] and f: b ⟼ a
type ContraMap[A, B any] struct {
Surface[A]
pure.ContraMap[A, B]
}
// Distance contra variant functor
func (f ContraMap[A, B]) Distance(a, b B) float32 {
return f.Surface.Distance(
f.ContraMap(a),
f.ContraMap(b),
)
}
// Equality contra variant functor
func (f ContraMap[A, B]) Equal(a, b B) bool {
return f.Surface.Equal(
f.ContraMap(a),
f.ContraMap(b),
)
}