Episode 31 - takeLast vs take Monday, 16 Apr.
This Monday I drew a new card:
❚ takeLast. Let’s compare it with
▬ number on these cards, such as
3, to set a maximum amount of events to emit.
❚ take and
❚ takeLast share identical behavior:
- they both return a new stream of at most amount values.
- if the input stream source emits fewer than amount values then all of its values are emitted on the output stream.
The stream returned by
❚ take emits the values at the same time as their counterpart on the input stream. This is not the case for
❚ takeLast. Remember: a stream is a sequence of events over time. And we can’t see the future! takeLast must wait for the
◉ complete notification. Only then, we know what are the last values.
❚ takeLast returns a new stream of at most amount values:
- When the input stream completes, the output stream:
- emits each of the last amount values emitted by the input stream
- and immediately completes
- If the input stream never completes, the output stream never emits
Note: the last values are delivered as several separate event values.