-
Notifications
You must be signed in to change notification settings - Fork 3
/
maybe_error_monad.go
252 lines (235 loc) · 7.67 KB
/
maybe_error_monad.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
package fp
import (
"reflect"
"sync"
)
type Monad interface {
// Map func(type1) (type2,&optional error/bool)
Map(fn interface{}) Monad
// ExpectPass func(type1) (bool)
ExpectPass(fn interface{}) Monad
// ExpectNoError func(type1) (error)
ExpectNoError(fn interface{}) Monad
// StreamOf func(type1) (type2,&optional error/bool)
StreamOf(fn interface{}) Stream
// Zip monads
Zip(interface{}, ...Monad) Monad
// Once monad
Once() Monad
// Value of monad
Val() Value
// fnContainer return error_boolean_monad real container
fnContainer() func() (interface{}, bool, error)
}
func M(v ...interface{}) Monad {
if isNilObject(v[0]) {
if len(v) > 1 {
if _, ok := v[len(v)-1].(error); ok {
return newNilMonad(v[len(v)-1].(error))
}
}
return newNilMonad(nil)
}
typ := reflect.FuncOf(nil, []reflect.Type{reflect.TypeOf(v[0]), boolType, errType}, false)
if len(v) > 1 {
if _, ok := v[len(v)-1].(error); ok {
return newErrorMonad(reflect.MakeFunc(typ, func(in []reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(v[0]), reflect.ValueOf(true), reflect.ValueOf(v[len(v)-1])}
}))
}
if _, ok := v[len(v)-1].(bool); ok {
return newErrorMonad(reflect.MakeFunc(typ, func(in []reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(v[0]), reflect.ValueOf(v[len(v)-1]), reflect.Zero(errType)}
}))
}
}
return newErrorMonad(reflect.MakeFunc(typ, func(in []reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(v[0]), reflect.ValueOf(true), reflect.Zero(errType)}
}))
}
type errorMonad struct {
/* fn is kind of func() (any,bool,error) */
fn reflect.Value
}
func newErrorMonad(fn reflect.Value) errorMonad {
return errorMonad{fn: fn}
}
func (em errorMonad) Map(fn interface{}) Monad {
fnVal := toErrMonadFunc(fn)
outTyp := reflect.FuncOf(nil, outTypes(fnVal.Type()), false)
return newErrorMonad(reflect.MakeFunc(outTyp, func(in []reflect.Value) []reflect.Value {
out := em.fn.Call(in)
if e := out[2].Interface(); e != nil && e.(error) != nil {
return []reflect.Value{reflect.Zero(fnVal.Type().Out(0)), reflect.ValueOf(false), out[2]}
}
if !out[1].Bool() {
return []reflect.Value{reflect.Zero(fnVal.Type().Out(0)), reflect.ValueOf(false), reflect.Zero(errType)}
}
return fnVal.Call(out[:1])
}))
}
func (em errorMonad) fnContainer() func() (interface{}, bool, error) {
return func() (interface{}, bool, error) {
out := em.fn.Call(nil)
if e := out[2].Interface(); e != nil && e.(error) != nil {
return nil, false, e.(error)
}
if !out[1].Bool() {
return nil, false, nil
}
return out[0].Interface(), true, nil
}
}
func (em errorMonad) Zip(fn interface{}, others ...Monad) Monad {
fnVal := toErrMonadFunc(fn)
outTyp := reflect.FuncOf(nil, outTypes(fnVal.Type()), false)
return newErrorMonad(reflect.MakeFunc(outTyp, func(in []reflect.Value) []reflect.Value {
monads := append([]Monad{em}, others...)
var input []reflect.Value
for _, m := range monads {
out, ok, err := m.fnContainer()()
if err != nil {
return []reflect.Value{reflect.Zero(fnVal.Type().Out(0)), reflect.ValueOf(false), reflect.ValueOf(err)}
}
if !ok {
return []reflect.Value{reflect.Zero(fnVal.Type().Out(0)), reflect.ValueOf(false), reflect.Zero(errType)}
}
input = append(input, reflect.ValueOf(out))
}
return fnVal.Call(input)
}))
}
func (em errorMonad) Once() Monad {
var out []reflect.Value
var once sync.Once
return newErrorMonad(reflect.MakeFunc(em.fn.Type(), func(in []reflect.Value) []reflect.Value {
once.Do(func() {
out = em.fn.Call(nil)
})
return out
}))
}
func (em errorMonad) ExpectPass(fn interface{}) Monad {
return em.Expect(fn)
}
func (em errorMonad) ExpectNoError(fn interface{}) Monad {
return em.Expect(fn)
}
func (em errorMonad) Expect(fn interface{}) Monad {
typ := reflect.TypeOf(fn)
wrapTyp := reflect.FuncOf(nil, outTypes(em.fn.Type()), false)
if typ.NumOut() == 1 && typ.Out(0).AssignableTo(errType) {
return newErrorMonad(reflect.MakeFunc(wrapTyp, func(in []reflect.Value) []reflect.Value {
out := em.fn.Call(in)
if e := out[2].Interface(); e != nil && e.(error) != nil {
return []reflect.Value{reflect.Zero(em.fn.Type().Out(0)), reflect.ValueOf(false), out[2]}
}
if !out[1].Bool() {
return []reflect.Value{reflect.Zero(em.fn.Type().Out(0)), reflect.ValueOf(false), reflect.Zero(errType)}
}
out1 := reflect.ValueOf(fn).Call(out[:1])
return []reflect.Value{out[0], reflect.ValueOf(true), out1[0]}
}))
} else if typ.NumOut() == 1 && typ.Out(0).AssignableTo(boolType) {
return newErrorMonad(reflect.MakeFunc(wrapTyp, func(in []reflect.Value) []reflect.Value {
out := em.fn.Call(in)
if e := out[2].Interface(); e != nil && e.(error) != nil {
return []reflect.Value{reflect.Zero(em.fn.Type().Out(0)), reflect.ValueOf(false), out[2]}
}
if !out[1].Bool() {
return []reflect.Value{reflect.Zero(em.fn.Type().Out(0)), reflect.ValueOf(false), reflect.Zero(errType)}
}
out1 := reflect.ValueOf(fn).Call(out[:1])
return []reflect.Value{out[0], out1[0], reflect.Zero(errType)}
}))
} else {
panic("bad expect function " + typ.String())
}
}
func (em errorMonad) StreamOf(fn interface{}) Stream {
fnVal := toErrMonadFunc(fn)
ctx := newCtx(nil)
evalM := func() (reflect.Value, bool, error) {
out := em.fn.Call(nil)
if e := out[2].Interface(); e != nil && e.(error) != nil {
return reflect.Value{}, false, e.(error)
}
if !out[1].Bool() {
return reflect.Value{}, false, nil
}
out = fnVal.Call(out[:1])
if e := out[2].Interface(); e != nil && e.(error) != nil {
return reflect.Value{}, false, e.(error)
}
if !out[1].Bool() {
return reflect.Value{}, false, nil
}
return out[0], true, nil
}
if typ := fnVal.Type().Out(0); typ != streamType {
} else if out, ok, err := evalM(); err != nil {
sc := newNilSource()
ctx.SetErr(err)
return newStream(ctx, sc.ElemType(), sc.Next)
} else if !ok {
return newNilStream()
} else {
return out.Interface().(Stream)
}
elemTyp := fnVal.Type().Out(0).Elem()
var iter iterator
return newStream(ctx, elemTyp, func() (reflect.Value, bool) {
if iter == nil {
out, ok, err := evalM()
if err != nil {
ctx.SetErr(err)
return reflect.Value{}, false
} else if !ok {
return reflect.Value{}, false
}
_, iter = makeIter(ctx, out)
}
return iter()
})
}
func (em errorMonad) Val() Value {
out := em.fn.Call(nil)
if e := out[2].Interface(); e != nil && e.(error) != nil {
return Value{err: e.(error)}
}
if out[1].Bool() {
return Value{typ: out[0].Type(), val: out[0]}
}
/* out[0] would always valid */
return Value{typ: out[0].Type()}
}
func toErrMonadFunc(fn interface{}) reflect.Value {
typ := reflect.TypeOf(fn)
ntyp := reflect.FuncOf(inTypes(typ), []reflect.Type{typ.Out(0), boolType, errType}, false)
if typ.NumOut() == 2 && typ.Out(1).AssignableTo(errType) {
return reflect.MakeFunc(ntyp, func(in []reflect.Value) []reflect.Value {
out := reflect.ValueOf(fn).Call(in)
return []reflect.Value{out[0], reflect.ValueOf(true), out[1]}
})
} else if typ.NumOut() == 2 && typ.Out(1).AssignableTo(boolType) {
return reflect.MakeFunc(ntyp, func(in []reflect.Value) []reflect.Value {
out := reflect.ValueOf(fn).Call(in)
return []reflect.Value{out[0], out[1], reflect.Zero(errType)}
})
}
return reflect.MakeFunc(ntyp, func(in []reflect.Value) []reflect.Value {
out := reflect.ValueOf(fn).Call(in)
return []reflect.Value{out[0], reflect.ValueOf(true), reflect.Zero(errType)}
})
}
func isNilObject(v interface{}) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return rv.IsNil()
}
return false
}