Skip to content

Adds example solutions for the observables homework #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion observables/exercise-1/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ const clickStream = Observable.fromEvent(theButton, "click");
Exercise 1:
We've created an Observable stream from the button's click event above.
Subscribe to the stream and print out the emitted value.
*/
*/

clickStream.subscribe(value => console.log('click event: ', value));
4 changes: 3 additions & 1 deletion observables/exercise-2/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ const clickStream = Observable.fromEvent(theButton, "click");
"Dog #1" after the first click, "Dog #2" after the second, and so on.
*/


clickStream
.map((value, index) => `Dog #${index + 1}`)
.subscribe(dog => console.log(dog));
5 changes: 5 additions & 0 deletions observables/exercise-3/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ const clickStream = Observable.fromEvent(theButton, "click");
No output after the fourth click,
"Dog #5" after the fith click, and so on.
*/

Copy link
Collaborator Author

@codebyuma codebyuma Mar 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: in reality, we may want to filter before we map to avoid doing extra work on the emissions we'll eventually ignore. But since the desired output adds the source stream's emission number to the string, we have to filter after we map.

For example, if we filtered first and then mapped, then the output would be Dog #1, Dog #2, etc. If we map and then filter, the output is Dog #1, Dog #3, etc.

If this doesn't make sense, try switching the order and you can see it in action :)

clickStream
.map((value, index) => `Dog #${index + 1}`)
.filter((value, index) => index % 2 === 0)
.subscribe(dog => console.log(dog));
4 changes: 4 additions & 0 deletions observables/exercise-4/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ const clickStream = Observable.fromEvent(theButton, "click");
If you click the button extra times before each 1000ms is up, we shouldn't see a generated dog.
*/

clickStream
.throttleTime(1000)
.map((value, index) => `Dog #${index + 1}`)
.subscribe(dog => console.log(dog));