Created
July 24, 2018 04:15
-
-
Save hauxe/86429a93510cba216c9d023cee54aaa8 to your computer and use it in GitHub Desktop.
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
// AND function combines all signal from channels into a single channel with And condition | |
func AND(channels ...<-chan interface{}) <-chan interface{} { | |
switch len(channels) { | |
case 0: | |
return nil | |
case 1: | |
return channels[0] | |
} | |
andDone := make(chan interface{}) | |
collector := make(chan interface{}, len(channels)) | |
go func() { | |
defer close(andDone) | |
switch len(channels) { | |
case 2: | |
select { | |
case collector <- <-channels[0]: | |
case collector <- <-channels[1]: | |
} | |
default: | |
select { | |
case collector <- <-channels[0]: | |
case collector <- <-channels[1]: | |
case collector <- <-channels[2]: | |
case collector <- <-AND(channels[3:]...): | |
} | |
} | |
}() | |
return andDone | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment