Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to use flatMap?

I am starting to use RxJS and I don't understand why in this example we need to use a function like flatMap or concatAll; where is the array of arrays here?

var requestStream = Rx.Observable.just('https://api.github.com/users');  var responseMetastream = requestStream   .flatMap(function(requestUrl) {     return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));   });  responseMetastream.subscribe(url => {console.log(url)}) 

If someone can visually explain what is happening, it will be very helpful.

like image 500
user233232 Avatar asked Nov 02 '15 05:11

user233232


People also ask

Why do we need flatMap?

It uses One-To-One mapping. It's mapper function produces multiple values (stream of values) for each input value. It's mapper function produces single values for each input value. Use the flatMap() method when the mapper function is producing multiple values for each input value.

Where is flatMap used?

flatMap() V/s map() : It does not flatten the stream. But flatMap() is the combination of a map and a flat operation i.e, it applies a function to elements as well as flatten them. 2) map() is used for transformation only, but flatMap() is used for both transformation and flattening.

Should I use map or flatMap?

You should use a map() if you just want to transform one Stream into another where each element gets converted to one single value. Use flatMap() if the function used by map operation returns multiple values and you want just one list containing all values.

What is the difference between map and flatMap and a good use case for each?

map returns RDD of equal number of elements while flatMap may not. An example use case for flatMap Filter out missing or incorrect data. An example use case for map Use in wide variety of cases where is the number of elements of input and output are the same.


1 Answers

['a','b','c'].flatMap(function(e) {     return [e, e+ 'x', e+ 'y',  e+ 'z'  ]; }); //['a', 'ax', 'ay', 'az', 'b', 'bx', 'by', 'bz', 'c', 'cx', 'cy', 'cz']   ['a','b','c'].map(function(e) {     return [e, e+ 'x', e+ 'y',  e+ 'z'  ]; }); //[Array[4], Array[4], Array[4]] 

You use flatMap when you have an Observable whose results are more Observables.

If you have an observable which is produced by an another observable you can not filter, reduce, or map it directly because you have an Observable not the data. If you produce an observable choose flatMap over map; then you are okay.

As in second snippet, if you are doing async operation you need to use flatMap.

var source = Rx.Observable.interval(100).take(10).map(function(num){      return num+1  });  source.subscribe(function(e){      console.log(e)  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.1/Rx.min.js"></script>

var source = Rx.Observable.interval(100).take(10).flatMap(function(num){      return Rx.Observable.timer(100).map(() => num)  });  source.subscribe(function(e){      console.log(e)  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.1/Rx.min.js"></script>
like image 97
serkan Avatar answered Oct 15 '22 11:10

serkan