I was browsing through RxMarbles and was totally baffled by the
sample function. The marble diagram looks pretty random at first sight:
At first I thought - “Hey, that second sequence is getting totally ignored!”. But after I read the description I figured it out:
The first sequence’s elements is what
sampleemits, while the second sequence’s elements determine when
sampleemits. So in a way yes - the actual values A, B, C, D do get totally ignored.
When it was clear to me what
sample does I started wondering if this function has any practical application :]
This brought me to creating a split lap timer app to test what
sample can do for me. In the finished project I have a timer emitting time values (aka the first sequence) and I want to grab (or sample) the values whenever the user taps a button (aka the second sequence).
Here’s how the marble diagram looks like for the app setup:
And this is how the app looks like when finished:
Let’s build that app :]