Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 1.17 KB

get_last_N_from_channel.md

File metadata and controls

39 lines (33 loc) · 1.17 KB

Get last N elements from a channel

Imagine you want to consume the last N elements from a channel and don't know how to do it. Differently from the first N elements that you can use the take operator (check details here), there is no operator that does this by itself.

Here, we'll use the buffer channel operator (details here) that splits a channel into subsets of a specific size and then we will use the last channel operator to consume this last subset that contains the last N elements from the channel.

Channel
  .of(1..100)
  .buffer(size: 5)
  .last()
  .flatten()
  .view()

Or

Channel
  .of('a'..'z')
  .buffer(size: 5)
  .last()
  .flatten()
  .view()

Notice that the buffer operator will create subsets of size 5, where each subset is a single element. Then, the last operator will consume the last element, which is this subset with 5 items. The flatten operator will turn this single element with five items into a channel with five elements. The view operator will print the channel to the screen :party: