mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
refactor: update Compose func in function.go
This commit is contained in:
@@ -48,8 +48,8 @@ func (f Fn) Curry(i interface{}) func(...interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compose compose the functions from right to left
|
// Compose compose the functions from right to left
|
||||||
func Compose(fnList ...func(...string) string) func(...string) string {
|
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} {
|
||||||
return func(s... string) string {
|
return func(s... interface{}) interface{} {
|
||||||
f := fnList[0]
|
f := fnList[0]
|
||||||
restFn := fnList[1:]
|
restFn := fnList[1:]
|
||||||
|
|
||||||
|
|||||||
@@ -71,19 +71,17 @@ func TestCurry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCompose(t *testing.T) {
|
func TestCompose(t *testing.T) {
|
||||||
toUpper := func(a... string) string {
|
toUpper := func(a ...interface{}) interface{} {
|
||||||
return strings.ToUpper(a[0])
|
return strings.ToUpper(a[0].(string))
|
||||||
}
|
}
|
||||||
|
toLower := func(a ...interface{}) interface{} {
|
||||||
toLower := func(a... string) string {
|
return strings.ToLower(a[0].(string))
|
||||||
return strings.ToLower(a[0])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expect := toUpper(toLower("aBCde"))
|
expect := toUpper(toLower("aBCde"))
|
||||||
cf := Compose(toUpper, toLower)
|
cf := Compose(toUpper, toLower)
|
||||||
res := cf("aBCde")
|
res := cf("aBCde")
|
||||||
|
|
||||||
fmt.Println(res, expect)
|
|
||||||
if res != expect {
|
if res != expect {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user