// {f1,f2} -> f2(f1(
func getComposeIndexFuncsLeft(funcs []string) string {
if nil == funcs || len(funcs) == 0 {
return ""
}
// reverse
sort.Sort(sort.Reverse(sort.StringSlice(funcs))) // 有点绕
bb := new(bytes.Buffer)
// foreach
for _, f := range funcs {
bb.WriteString(f)
bb.WriteString("(")
}
return bb.String()
}
// {f1,f2} -> ))
func getComposeIndexFuncsRight(funcs []string) string {
if nil == funcs || len(funcs) == 0 {
return ""
}
bb := new(bytes.Buffer)
// foreach
size := len(funcs)
for i := 0; i < size; i++ {
bb.WriteString(")")
}
return bb.String()
}
Reverse 逆序实现:
func reverse(s [] interface) [] interface {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}