Skip to content

Commit

Permalink
Add Episode 23 (count)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricss committed Feb 19, 2018
1 parent 702db73 commit 0e8d138
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion _data/course.yml
Expand Up @@ -146,4 +146,11 @@ lessons:
date: Monday, 12 Feb.
videos:
-
- 255356781
- 255356781

- name: Four ways to count
card: count
date: Monday, 19 Feb.
videos:
-
- 256387349
47 changes: 47 additions & 0 deletions count.md
@@ -0,0 +1,47 @@
---
id: count
lesson: 23
title: Reactive Programming - Count
layout: default
class: post
preview_image: count/content_preview.jpg
preview_image_alt:
---

Last week, you learned about [`❚ scan`](/scan) and [`❚ reduce`](/reduce). Here is how you can use them to count the number of values emitted on an input stream:

{% include card_player.html video=256387874 episode=true %}

In [Episode 21](/scan), the **accumulator** function accumulated _values_. In the example above, it only increments the <code class="two">acc</code> (accumulation) and the values are ignored. The <code class="two">acc</code> is emitted as a count value.

On the one hand, [`❚ scan`](/scan) progressively emits on the output stream the current number of values emitted on the input stream. On the other hand, [`❚ reduce`](/reduce) emits the count only when the input stream completes. If the input stream never completes, the output stream will never emit the count.

In RxJS, you actually have a `❚ count` operator. It is equivalent to the `❚ reduce` card seen above:

{% include card_player.html video=256388299 episode=true %}

This is how `❚ count` operates with one input stream:

- **When the input stream completes**, the output stream:
- emits the number of values emitted on the input stream
- and immediately completes

## Count values that satisfy a predicate

Can you update the cards above to count the number of ``?

You can slightly modify the **accumulator** function to increment the <code class="two">acc</code> only when the input value is ``:
> ![](/img/count/accumulator2.png){:.w400}
![](/img/count/scan_predicate-reduce_predicate.png)

In RxJS, you can simply use `❚ count` with a [predicate](filter#predicate):

![](/img/count/count_predicate.png)

This is how `❚ count` operates with one input stream and a **predicate**:

- When the input stream completes, the output stream:
- emits the number of values emitted on the input stream that satisfied the **predicate**
- and immediately completes

Binary file added img/count/accumulator2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/count/content_preview.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/count/count_predicate.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/count/scan_predicate-reduce_predicate.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0e8d138

Please sign in to comment.