These are chat archives for ReactiveX/RxJava

26th
Mar 2016
Artem Kholodnyi
@defHLT
Mar 26 2016 09:59
hey guys, do you have any data on whether it faster to use map + filter combo or flatMap instead?
Artem Kholodnyi
@defHLT
Mar 26 2016 10:27
it actually makes more sense to compare against concatMap as it preserved order
Dorus
@Dorus
Mar 26 2016 10:43

@mlatu Why do you need map+filter? Just filter can already do the filtering and possible data manipulations (just nothing persistent).

flatMap is for one to many tranformations, map for one to one.

About preserving order, do you really need that in a reactive system? concatMap also evaluate everything serial i think, flatMap has the possibility to go concurrent if you need it.

Artem Kholodnyi
@defHLT
Mar 26 2016 21:44
@Dorus One pattern I saw that instead of
o.map( x -> f(x)).filter(x -> x != null)
the following is used:
o.flatMap(x -> y = f(x); y != null ? Observable.just(y) : Observable.empty())
It helps greatly with boilerplate on Android where lambda is not (easily) available
Dorus
@Dorus
Mar 26 2016 22:01
flatMap like this isn't doing anything concurrent, so it wont change order.
Identical code, just schematic difference.
Oh right, flatMap is one to zero or more, here it's used for 0 or 1 element. Map is also one on one.
Very hard to say what's faster. You're probably talking about nanoseconds here, but you can run it 10.000.000 times in a loop and see.
Just measure Observable.range(10000000)./* map or flatmap */.subscribe(() -> endMeasure())
@mlatu
Dorus
@Dorus
Mar 26 2016 22:09
Anyway i think i like map + filter more in this instance. It communicate clearly what's intended.
I do have the feeling the flatMap one could be written cleaner also.
o.flatMap(x -> Observalbe.just(f(x)).filter(y -> y != null) )
lol :)
map is still cleaner.
Dorus
@Dorus
Mar 26 2016 22:16
or write an transformer .map(x -> f(x)).compose(ignoreNull()) or even .compose(ignoreNull(x -> f(x))).