-
Notifications
You must be signed in to change notification settings - Fork 0
/
curry.go
48 lines (35 loc) · 988 Bytes
/
curry.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 curry
import "reflect"
type Wrapper func(partialArgs ...interface{}) (Wrapper, []reflect.Value)
func Func(fn interface{}) (Wrapper, []reflect.Value) {
return wrap(reflect.ValueOf(fn))
}
func Method(instance interface{}, method string) (Wrapper, []reflect.Value) {
return wrap(reflect.ValueOf(instance).MethodByName(method))
}
func wrap(r reflect.Value) (Wrapper, []reflect.Value) {
numIn := r.Type().NumIn()
return func(partialArgs ...interface{}) (Wrapper, []reflect.Value) {
var partial Wrapper
args := make([]reflect.Value, numIn)
idx := 0
for _, arg := range partialArgs {
args[idx] = reflect.ValueOf(arg)
idx++
}
if idx == numIn {
return nil, r.Call(args)
}
partial = func(partialArgs ...interface{}) (Wrapper, []reflect.Value) {
for _, arg := range partialArgs {
args[idx] = reflect.ValueOf(arg)
idx++
}
if idx == numIn {
return nil, r.Call(args)
}
return partial, nil
}
return partial, nil
}, nil
}