-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathiso.go
155 lines (129 loc) · 3.77 KB
/
iso.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Copyright (C) 2022 - 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/fogfish/golem
//
package optics
// Getter composes lense with transform function, making read-only lense
func Getter[S, A, B any](lens Lens[S, A], f func(A) B) Lens[S, B] {
return fmap[S, A, B]{lens, f}
}
type fmap[S, A, B any] struct {
lens Lens[S, A]
f func(A) B
}
func (c fmap[S, A, B]) Put(s *S, b B) *S { return s }
func (c fmap[S, A, B]) Get(s *S) B { return c.f(c.lens.Get(s)) }
// Setter composes lense with inverse transformer, making write-only lense
func Setter[S, A, B any](lens Lens[S, A], f func(B) A) Lens[S, B] {
return cmap[S, A, B]{lens, f}
}
type cmap[S, A, B any] struct {
lens Lens[S, A]
f func(B) A
}
func (c cmap[S, A, B]) Put(s *S, b B) *S { return c.lens.Put(s, c.f(b)) }
func (c cmap[S, A, B]) Get(s *S) B { return *new(B) }
// Transformer lens is a structure-preserving mapping between two categories A, B.
func BiMap[S, A, B any](
lens Lens[S, A],
fmap func(A) B,
cmap func(B) A,
) Lens[S, B] {
return codec[S, A, B]{lens: lens, fmap: fmap, cmap: cmap}
}
type codec[S, A, B any] struct {
lens Lens[S, A]
fmap func(A) B
cmap func(B) A
}
func (c codec[S, A, B]) Put(s *S, b B) *S { return c.lens.Put(s, c.cmap(b)) }
func (c codec[S, A, B]) Get(s *S) B { return c.fmap(c.lens.Get(s)) }
// Automatic transformer for string superset, preserve mapping between
// two categories A, B, where both rooted to strings
func BiMapS[S any, A, B String](attr ...string) Lens[S, B] {
return BiMap(
ForProduct1[S, A](attr...),
func(a A) B { return B(a) },
func(b B) A { return A(b) },
)
}
type String interface {
~string
}
// Automatic transformer for []byte superset, preserve mapping between
// two categories A, B, where both rooted to []byte
func BiMapB[S any, A, B Byte](attr ...string) Lens[S, B] {
return BiMap(
ForProduct1[S, A](attr...),
func(a A) B { return B(a) },
func(b B) A { return A(b) },
)
}
type Byte interface {
~[]byte
}
// Automatic transformer for int superset, preserve mapping between
// two categories A, B, where both rooted to int
func BiMapI[S any, A, B Int](attr ...string) Lens[S, B] {
return BiMap(
ForProduct1[S, A](attr...),
func(a A) B { return B(a) },
func(b B) A { return A(b) },
)
}
type Int interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
// Automatic transformer for float32 superset, preserve mapping between
// two categories A, B, where both rooted to float
func BiMapF[S any, A, B Float](attr ...string) Lens[S, B] {
return BiMap(
ForProduct1[S, A](attr...),
func(a A) B { return B(a) },
func(b B) A { return A(b) },
)
}
type Float interface {
~float32 | ~float64
}
// An isomorphism is a structure-preserving mapping between two structures
// of the same shape that can be reversed by an inverse mapping.
type Isomorphism[S, T any] interface {
Forward(*S, *T)
Inverse(*T, *S)
}
// Building Isomorphism from joining lenses
func Iso[S, T, A any](
sa Lens[S, A],
ta Lens[T, A],
) Isomorphism[S, T] {
return iso[S, T, A]{sa, ta}
}
type iso[S, T, A any] struct {
sa Lens[S, A]
ta Lens[T, A]
}
func (iso iso[S, T, A]) Forward(s *S, t *T) { iso.ta.Put(t, iso.sa.Get(s)) }
func (iso iso[S, T, A]) Inverse(t *T, s *S) { iso.sa.Put(s, iso.ta.Get(t)) }
// Build a structure-preserving mapping between two structures
func Morphism[S, T any](seq ...Isomorphism[S, T]) Isomorphism[S, T] {
return morphism[S, T](seq)
}
type morphism[S, T any] []Isomorphism[S, T]
func (seq morphism[S, T]) Forward(s *S, t *T) {
for _, iso := range seq {
if iso != nil {
iso.Forward(s, t)
}
}
}
func (seq morphism[S, T]) Inverse(t *T, s *S) {
for _, iso := range seq {
if iso != nil {
iso.Inverse(t, s)
}
}
}