Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does concatMap do? [duplicate]

Tags:

haskell

What does concatMap do? I know what concat and map do. Is it just both of them put together or is it a completely different function?

like image 857
Ahsan Avatar asked Mar 07 '11 21:03

Ahsan


People also ask

What does concatMap do in angular?

The Angular ConcatMap maps each value from the source observable into an inner observable, subscribes to it, and then starts emitting the values from it replacing the original value. It creates a new inner observable for every value it receives from the Source.

What is difference between mergeMap and concatMap?

concatMap : behaves like a queue: It stores all calls and sends one after another. If one is completed, the next one is being processed. mergeMap : Also sends all requests, like concatMap but does not wait until the response is coming back. It sends them out as they come.

What are switchMap mergeMap and concatMap?

Use mergeMap if you simply want to flatten the data into one Observable, use switchMap if you need to flatten the data into one Observable but only need the latest value and use concatMap if you need to flatten the data into one Observable and the order is important to you.


1 Answers

Yes, the concatMap function is just concat and map put together. Hence the name. Putting functions together simply means composing them:

(.) :: (b -> c) -> (a -> b) -> a -> c 

However concat and map cannot be put together by simply using function composition because of the type signature of map:

map :: (a -> b) -> [a] -> [b]        --------    ---    ---           a         b      c 

As you can see function composition expects a function of type a -> b, but map is of type a -> b -> c. To compose concat with map you need to use the .: operator instead:

(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d 

The concat function has a type signature of:

concat :: [[a]] -> [a]           -----    ---             c       d 

Hence concat .: map is of type:

concat .: map :: (a -> [b]) -> [a] -> [b]                  ----------    ---    ---                      a          b      d 

Which is the same as that of concatMap:

concatMap :: (a -> [b]) -> [a] -> [b] 

The .: operator itself can be written in terms of function composition:

(.:) = (.) (.) (.)  -- or  (.:) = (.) . (.) 

Hence concatMap can be written as:

concatMap = (.) (.) (.) concat map  -- or  concatMap = (concat .) . map  -- or  concatMap = concat .: map 

If you flip the arguments of concatMap you get the >>= (bind) function of the list monad:

instance Monad [] where     return x = [x]     (>>=) = flip concatMap     fail _ = []  flip concatMap :: [a] -> (a -> [b]) -> [b]  >>= :: Monad m => m a -> (a -> m b) -> m b 

This makes it the same as the =<< function of the list monad:

concatMap :: (a -> [b]) -> [a] -> [b]  =<< :: Monad m => (a -> m b) -> m a -> m b 

So now you know everything that there is to know about concatMap. It's just concat applied to the result of a map. Hence the name.

like image 143
Aadit M Shah Avatar answered Sep 22 '22 02:09

Aadit M Shah