-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add
PipeCallback
and test case
- Loading branch information
1 parent
93b1aa3
commit add1d6a
Showing
4 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// PipeCallback executes a series of functions where the output of each function is passed as input to the next function. | ||
// Returns a function that can be invoked to run the entire execution process. | ||
func PipeCallback[T any](fns ...func(args T) T) func(args T) T { | ||
return func(args T) T { | ||
result := args | ||
for _, fn := range fns { | ||
result = fn(result) | ||
} | ||
return result | ||
} | ||
} | ||
|
||
func Times(cb func(...any) any, x int) func(...any) any { | ||
value := any(nil) | ||
times := 0 | ||
return func(args ...any) any { | ||
if times < x { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err, ok := r.(error) | ||
if !ok { | ||
err = errors.New(fmt.Sprint(r)) | ||
} | ||
fmt.Println("Error occurred:", err) | ||
} | ||
}() | ||
value = cb(args...) | ||
times++ | ||
} | ||
|
||
return value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package utils_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
utils "jingyuexing.com/utils" | ||
) | ||
|
||
func TestPipelCallback(t *testing.T){ | ||
addOne := func(x int) int { | ||
return x + 1 | ||
} | ||
|
||
double := func(x int) int { | ||
return x * 2 | ||
} | ||
|
||
square := func(x int) int { | ||
return x * x | ||
} | ||
composeFunc := utils.PipeCallback(addOne,double,square) | ||
|
||
result := composeFunc(55) | ||
if result != 12544 { | ||
t.Error("no pass") | ||
}else{ | ||
fmt.Printf("%d",result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters