-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurry.go
82 lines (66 loc) · 2.05 KB
/
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
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
package functional
import (
"errors"
"reflect"
)
// GenericCurry creates a wrapper around a given function so the
// original function can be called a parameter at the time.
func GenericCurry(in []reflect.Value) []reflect.Value {
// Check number of input arguments
if len(in) != 1 {
panic(errors.New("Curry expects one argument"))
}
f := in[0]
ft := f.Type()
if ft.Kind() == reflect.Interface {
ft = reflect.TypeOf(f.Interface())
}
if ft.Kind() != reflect.Func {
panic(errors.New("Curry expects a function as the first argument"))
}
numIn := ft.NumIn()
// If number of inputs is 1 or 0 return the original function
if numIn <= 1 {
return []reflect.Value{f}
}
// Copy function input types
inputTypes := CopyFunctionInputTypes(ft)
// Copy function output types
outputTypes := CopyFunctionOutputTypes(ft)
// Create last output function type
outputFuncTypes := make([]reflect.Type, numIn, numIn)
outputFuncTypes[numIn-1] = reflect.FuncOf(
[]reflect.Type{inputTypes[numIn-1]},
outputTypes,
ft.IsVariadic(),
)
// Create rest of output function types
for i := numIn - 2; i >= 0; i-- {
outputFuncTypes[i] = reflect.FuncOf(
[]reflect.Type{inputTypes[i]},
[]reflect.Type{outputFuncTypes[i+1]},
false,
)
}
// Forward actual curry declaration
var curryFun func([]reflect.Value) []reflect.Value
// Create input values cache and initial count and encapsulate them in
// the curry func implementation
inputValues := make([]reflect.Value, numIn, numIn)
inputCount := 0
curryFun = func(args []reflect.Value) []reflect.Value {
inputValues[inputCount] = args[0]
inputCount++
// If input count is the same as number of input arguments return
// original function call
if inputCount == numIn {
return f.Call(inputValues)
}
// Return the next function step of the curry wrapper
returnFunc := reflect.MakeFunc(outputFuncTypes[inputCount], curryFun)
return []reflect.Value{returnFunc}
}
// Return the first curry wrapper
returnFunc := reflect.MakeFunc(outputFuncTypes[0], curryFun)
return []reflect.Value{returnFunc}
}