-
-
Save mingkwind/fdbf7d076a6677d3d89bdc06c49053bd to your computer and use it in GitHub Desktop.
multiJudge
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
) | |
func init() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
} | |
type JudgeFunc func(any) bool | |
func MultiJudge(value interface{}, judgeFuncs []JudgeFunc) bool { | |
funcNum := len(judgeFuncs) | |
resultChan := make(chan bool, funcNum) | |
for _, judgeFunc := range judgeFuncs { | |
go func(value any, judgeFunc JudgeFunc, resultChan chan bool) { | |
resultChan <- judgeFunc(value) | |
}(value, judgeFunc, resultChan) | |
} | |
count := 0 | |
for result := range resultChan { | |
count++ | |
if result { | |
return true | |
} | |
if count == funcNum { | |
return false | |
} | |
} | |
return false | |
} | |
func main() { | |
result := MultiJudge(4, []JudgeFunc{ | |
func(a any) bool { | |
fmt.Println("judge 1 called") | |
return a.(int) == 1 | |
}, | |
func(a any) bool { | |
fmt.Println("judge 2 called") | |
return a.(int) == 2 | |
}, | |
func(a any) bool { | |
fmt.Println("judge 3 called") | |
return a.(int) == 3 | |
}, | |
func(a any) bool { | |
fmt.Println("judge 4 called") | |
return a.(int) == 4 | |
}, | |
}) | |
fmt.Println("multiJudge result", result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment