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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
package a
import "fmt"
type ImplicitOrd interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 |
~string
}
func LessGiven[T ImplicitOrd]() Ord[T] {
return LessFunc[T](func(a, b T) bool {
return a < b
})
}
type Eq[T any] interface {
Eqv(a T, b T) bool
}
type Ord[T any] interface {
Eq[T]
Less(a T, b T) bool
}
type LessFunc[T any] func(a, b T) bool
func (r LessFunc[T]) Eqv(a, b T) bool {
return r(a, b) == false && r(b, a) == false
}
func (r LessFunc[T]) Less(a, b T) bool {
return r(a, b)
}
type Option[T any] struct {
v *T
}
func (r Option[T]) IsDefined() bool {
return r.v != nil
}
func (r Option[T]) IsEmpty() bool {
return !r.IsDefined()
}
func (r Option[T]) Get() T {
return *r.v
}
func (r Option[T]) String() string {
if r.IsDefined() {
return fmt.Sprintf("Some(%v)", r.v)
} else {
return "None"
}
}
func (r Option[T]) OrElse(t T) T {
if r.IsDefined() {
return *r.v
}
return t
}
func (r Option[T]) Recover(f func() T) Option[T] {
if r.IsDefined() {
return r
}
t := f()
return Option[T]{&t}
}
type Func1[A1, R any] func(a1 A1) R
type Func2[A1, A2, R any] func(a1 A1, a2 A2) R
func (r Func2[A1, A2, R]) Curried() Func1[A1, Func1[A2, R]] {
return func(a1 A1) Func1[A2, R] {
return Func1[A2, R](func(a2 A2) R {
return r(a1, a2)
})
}
}
type HList interface {
sealed()
}
// Header is constrains interface type, enforce Head type of Cons is HT
type Header[HT any] interface {
HList
Head() HT
}
// Cons means H :: T
// zero value of Cons[H,T] is not allowed.
// so Cons defined as interface type
type Cons[H any, T HList] interface {
HList
Head() H
Tail() T
}
type Nil struct {
}
func (r Nil) Head() Nil {
return r
}
func (r Nil) Tail() Nil {
return r
}
func (r Nil) String() string {
return "Nil"
}
func (r Nil) sealed() {
}
type hlistImpl[H any, T HList] struct {
head H
tail T
}
func (r hlistImpl[H, T]) Head() H {
return r.head
}
func (r hlistImpl[H, T]) Tail() T {
return r.tail
}
func (r hlistImpl[H, T]) String() string {
return fmt.Sprintf("%v :: %v", r.head, r.tail)
}
func (r hlistImpl[H, T]) sealed() {
}
func hlist[H any, T HList](h H, t T) Cons[H, T] {
return hlistImpl[H, T]{h, t}
}
func Concat[H any, T HList](h H, t T) Cons[H, T] {
return hlist(h, t)
}
func Empty() Nil {
return Nil{}
}
func Some[T any](v T) Option[T] {
return Option[T]{}.Recover(func() T {
return v
})
}
func None[T any]() Option[T] {
return Option[T]{}
}
func Ap[T, U any](t Option[Func1[T, U]], a Option[T]) Option[U] {
return FlatMap(t, func(f Func1[T, U]) Option[U] {
return Map(a, f)
})
}
func Map[T, U any](opt Option[T], f func(v T) U) Option[U] {
return FlatMap(opt, func(v T) Option[U] {
return Some(f(v))
})
}
func FlatMap[T, U any](opt Option[T], fn func(v T) Option[U]) Option[U] {
if opt.IsDefined() {
return fn(opt.Get())
}
return None[U]()
}
type ApplicativeFunctor1[H Header[HT], HT, A, R any] struct {
h Option[H]
fn Option[Func1[A, R]]
}
func (r ApplicativeFunctor1[H, HT, A, R]) ApOption(a Option[A]) Option[R] {
return Ap(r.fn, a)
}
func (r ApplicativeFunctor1[H, HT, A, R]) Ap(a A) Option[R] {
return r.ApOption(Some(a))
}
func Applicative1[A, R any](fn Func1[A, R]) ApplicativeFunctor1[Nil, Nil, A, R] {
return ApplicativeFunctor1[Nil, Nil, A, R]{Some(Empty()), Some(fn)}
}
type ApplicativeFunctor2[H Header[HT], HT, A1, A2, R any] struct {
h Option[H]
fn Option[Func1[A1, Func1[A2, R]]]
}
func (r ApplicativeFunctor2[H, HT, A1, A2, R]) ApOption(a Option[A1]) ApplicativeFunctor1[Cons[A1, H], A1, A2, R] {
nh := FlatMap(r.h, func(hv H) Option[Cons[A1, H]] {
return Map(a, func(av A1) Cons[A1, H] {
return Concat(av, hv)
})
})
return ApplicativeFunctor1[Cons[A1, H], A1, A2, R]{nh, Ap(r.fn, a)}
}
func (r ApplicativeFunctor2[H, HT, A1, A2, R]) Ap(a A1) ApplicativeFunctor1[Cons[A1, H], A1, A2, R] {
return r.ApOption(Some(a))
}
func Applicative2[A1, A2, R any](fn Func2[A1, A2, R]) ApplicativeFunctor2[Nil, Nil, A1, A2, R] {
return ApplicativeFunctor2[Nil, Nil, A1, A2, R]{Some(Empty()), Some(fn.Curried())}
}
func OrdOption[T any](m Ord[T]) Ord[Option[T]] {
return LessFunc[Option[T]](func(t1 Option[T], t2 Option[T]) bool {
if !t1.IsDefined() && !t2.IsDefined() {
return false
}
return Applicative2(m.Less).ApOption(t1).ApOption(t2).OrElse(!t1.IsDefined())
})
}
func Given[T ImplicitOrd]() Ord[T] {
return LessGiven[T]()
}
|