What is different between `FlatMap` and `Map`
amikai opened this issue · comments
By FlatMap
Documentation
FlatMap applies a function to each item in an input channel, where the function returns a channel of items.
These items are then flattened into a single output channel using n goroutines for concurrency.
However, Map
also return a channel Try
. We also use a single output channel of Try
and iterate the value.
As a user, I don't know what is difference?
The main difference between Map and FlatMap lies in the type of function they accept and how the output is handled:
Map takes a function f that transforms an item of type A into an item of type B directly, i.e., f(A) (B, error). The resulting items are then sent to the output channel.
FlatMap, on the other hand, takes a function f that transforms an item of type A into a channel of items of type B, i.e., f(A) <-chan Try[B]. This allows for more complex transformations where each input item can potentially produce multiple output items. FlatMap then flattens the resulting channels into a single output channel.
Consider the following example:
Suppose you have a hypothetical API that returns a stream of products for a given category ID:
func getProducts(categoryID int) <-chan rill.Try[*Product] {...}
Now, you have a stream of category IDs (categoryIDs) and want to get all products from those categories. If you used a regular Map, you'd end up with a stream of streams, but with FlatMap all product response will flattened into a single stream:
allProducts := rill.FlatMap(categoryIDs, 5, getProducts)
FlatMap is a common operation in functional programming and is not unique to this library. You can find more information about FlatMap in various programming languages, such as this explanation for JavaScript: FlatMap - JavaScript | MDN
Thanks for pointing out the lack of clarity in the current FlatMap documentation. I appreciate your feedback, and I'll make sure to update the documentation in the next release.