1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

feat: add Take function to generate a chan of values taken from another chan

This commit is contained in:
dudaodong
2022-04-15 16:22:19 +08:00
parent a4900fecb4
commit f66c0938e5
2 changed files with 56 additions and 9 deletions

View File

@@ -52,3 +52,22 @@ func (c *Channel) Repeat(done <-chan any, values ...any) <-chan any {
}()
return dataStream
}
// Take return a chan whose values are tahken from another chan
func (c *Channel) Take(done <-chan any, valueStream <-chan any, number int) <-chan any {
takeStream := make(chan any)
go func() {
defer close(takeStream)
for i := 0; i < number; i++ {
select {
case <-done:
return
case takeStream <- <-valueStream:
}
}
}()
return takeStream
}