by Ahmed Rizwan

Rx — If the Operators could speak!

If the operators could talk, how exactly would they tell us what they do?

In order to take full advantage of Rx, you need a clear understanding of what Rx Operators are and what they do.

This is what the Operators would be telling the observables if they could talk when we use them.

For this article, I’ll assume that you already know what Rx is. If not, go read this. Or just simply google Rx and you’ll find a ton of helpful articles, tutorials, and videos.

Creational Operators

Create

I tell you what to emit, when to terminate, and what error to throw. ‘Cause I’m the boss.

Defer

You only get to “create” yourself once someone subscribes to you. And it’ll be a brand new version of yourself every single time.

Empty

Hm. Emit nothing. And then die, please.

Never

Emit nothing. And don’t… ever… terminate.

Throw

Emit nothing, and then throw an error, OK?

From

I’ll give you some objects, then you emit them right back at me.

Interval

How about this: you emit items. But not immediately. Send them back, one by one, after certain “intervals.”

Just

I need just one thing back from you. Just one.

Range

I give you a range of integers, then you emit back all the values in that range.

Repeat

How about you emit that same object repeatedly.

Start

Ok. I have a function. When it returns, you start emitting. But only when it returns. Got it?

Timer

So you got an item. Don’t emit it just yet. I’ll tell you the exact time when you should emit it. Don’t jump the gun.

Transformational Operators

Buffer

Ok, so here’s the deal. Whatever it is you normally emit, well don’t emit that. Instead ,collect the items into bundles over time. And send bundles instead. ‘Cause I want bundles!

FlatMap

So, like, if you have lists of items and there’s another observable that’s full of items, can you please “flatten” yourself and that observable so you can just send items?

Map

Transform each item into another item.

Scan

Transform each item into another item, like you did with map. But also include the “previous” item when you get around to doing a transform.

Filtering Operators

Debounce

Only emit if a certain amount of time is passed.

Distinct

Emit only distinct items. All right?

ElementAt

I tell you the index. You emit the item at that index.

Filter

I give you a criteria. You give me items that pass the criteria.

First

Just give me back the first item.

IgnoreElements

Do not, I repeat, do not emit a single item. And then die.

Last

Just give me back the last item.

Sample

I give you an interval. You give me only the most recent items from that that interval.

Skip

OK, skip the first n items, would you?

SkipLast

Skip the last n item s. Yeah, those ones.

Take

Emit only the first n items.

TakeLast

Emit only the last n items.

Combining Operators

Merge

Here are two observables. Let’s pretend they’re only one observable.

StartWith

Here are two observables. But I get to tell you which one to start with.

CombineLatest

Here are two observables. Between the two, make pairs of the latest items.

Zip

Here are two observables. But I tell you how to combine their items (through a function, of course).

Handling Errors

Catch

After an error is thrown, continue on with the emits.

Retry

After an error is thrown, restart from the very beginning.

Utility

Delay

Just add a delay before you start emitting, OK?

ObserveOn

“Observational” code should run on this particular thread.

SubscribeOn

“Subscription” code should run on this particular thread.

Subscribe

You can start emitting now. *music intensifies*

TimeInterval

OK, so observables send back items, right? Instead, I want you to send the time intervals back. Like the time differences between each emission.

TimeOut

Set a TimeOut on each emission. And if an item doesn’t get emitted within that time, just throw an error ?

Conditional and Boolean

All

If all items fulfill a certain criteria, return true.

Amb

Here are at least two observables. Give me the one that starts emitting first.

Contains

If I ask for an item, can you tell me whether you already have it?

DefaultIfEmpty

When you have nothing to emit, here’s a default value that you can send back.

SequenceEqual

Here are two observables. Return true if their items (and their sequence) are the same.

SkipUntil

Here are two observables. Skip the items of the first one until the second one starts emitting.

SkipWhile

I give you a condition. You emit items until that condition becomes false.

TakeUntil

Here are two observables. Only give me the items of the first one until the second one starts emitting.

Mathematical Operators

Average

Give me an average of your Integer items.

Count

Give me a count of your items.

Max

Emit only the maximum-valued item.

Min

Emit only the minimum-valued item.

Reduce

Do a scan, but only emit the final value.

Sum

Return the sum of all your items.

Conversion Operators

To

Convert an observable into a List, Map or Array, or whatever I tell you to.

That’s it for now. There are other operators as well, which you can find here. You can also check out RxMarbles, which has cool diagrams for demonstrating each operator.

Anyway, thank you for reading. I hope the article helped you better understand what each of these commands does in a fun way.

Happy coding!