Skip to content

Conversation

carter-ya
Copy link

I also have another way of implementation:

// FlatMap returns a Stream that flattens the result of the given FlatMapFunc, which means it's a 1:N model.
func (s Stream) FlatMap(fn FlatMapFunc, opts ...Option) Stream {
	source := make(chan interface{})
	
	go func() {
		group := NewRoutineGroup()
		for item := range s.source {
			each := item
			group.Run(func() {
				otherStream := fn(each)
				for other := range otherStream.source {
					source <- other
				}
			})
		}
		group.Wait()
		close(source)
	}()
	
	return Range(source)
}

Ref: #4

@kevwan kevwan requested a review from Copilot March 22, 2025 08:35
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant