Skip to content

Commit 61d8ca0

Browse files
committed
3.5 Added takeLastBuffer
1 parent 35320df commit 61d8ca0

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Part 3 - Taming the sequence/5. Time-shifted sequences.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,61 @@ Output
181181

182182
We've created an `Observable.interval` that signals the opening of a new buffer every 250ms. Because observables created with `interval` do not immediately emit a value, the first values were lost. For the closing of a buffer, we provided a lambda function that took every value emitted by our buffer-opening observable. The values generated by `interval` are the natural progression 1,2,3... but that doesn't matter, because we discarded that value. Such an example would be too complicated. Instead, we just created an observable that waits 200ms and then emits a single value. That means that each buffer last exactly 200ms, similarily to when buffering by time.
183183

184+
### takeLastBuffer
185+
186+
We have already seen the [takeLast](/Part%202%20-%20Sequence%20Basics/2.%20Reducing%20a%20sequence.md#skiplast-and-takelast) operator, which returns the last N number of items. Internally, `takeLast` needs to buffer items and re-emits them when the source sequence ends. The `takeLastBuffer` operator returns the last elements as one buffer.
187+
188+
#### By count
189+
190+
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/takeLastBuffer.png)
191+
192+
`takeLastBuffer` by count will emit the last `N` elements in a list.
193+
194+
```java
195+
Observable.range(0, 5)
196+
.takeLastBuffer(2)
197+
.subscribe(System.out::println);
198+
```
199+
Output
200+
```
201+
[3, 4]
202+
```
203+
204+
#### By time
205+
206+
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/takeLastBuffer.tn.png)
207+
208+
`takeLastBuffer` by time will emit, as a buffer, the items that were received during the specified timespan. The timespan has the specified length and ends at the end of the source sequence.
209+
210+
```java
211+
Observable.interval(100, TimeUnit.MILLISECONDS)
212+
.take(5)
213+
.takeLastBuffer(200, TimeUnit.MILLISECONDS)
214+
.subscribe(System.out::println);
215+
```
216+
Output
217+
```
218+
[2, 3, 4]
219+
```
220+
221+
#### By count and time
222+
223+
The buffer emitted by this overload of `takeLastBuffer` will contain items that were emitted over the specified timespan before the end. If this window contains more than the specified number of items, the buffer will contain the last `N` items.
224+
225+
```java
226+
Observable.interval(100, TimeUnit.MILLISECONDS)
227+
.take(5)
228+
.takeLastBuffer(2, 200, TimeUnit.MILLISECONDS)
229+
.subscribe(System.out::println);
230+
```
231+
Output
232+
```
233+
[3, 4]
234+
```
235+
236+
As we saw in the previous example, the last 200ms include three values. With `.takeLastBuffer(2, 200, TimeUnit.MILLISECONDS)` we specified that we want values from the last 200ms, but no more than 2 values. For that reason, we only get the last two values.
237+
238+
184239
## Delay
185240

186241
`delay`, as the name suggests, will postpone the emission of values for a specified amount of time. The are two ways to do that. One is by storing values until you are ready to emit them. The other is to delay the subscription to observable.

0 commit comments

Comments
 (0)