Extra Credit - Observables
Please download the homework here.
Overview
In this assignment, you will work with observables.
Learning Objectives
- Practice writing and reasoning about observables and observers
Student Expectations
Students will be graded on their ability to:
- Correctly implement the functions specified below
- Follow the coding, bad practice, and testing guidelines
- Design full-coverage unit tests for the functions they implemented
- Note that you can delete the uncredited functions should you choose not to implement them to avoid writing tests for them.
Programming Tasks
For all functions below, use the provided Observable class.
Write a function
classifyObservables
that takes in an arrayobsArr
of Observables where each Observable updates with a typestring
,number
, orboolean
. Return an object with three observables, one for each named type. Each of the three observables updates anytime an Observable of that type in the input array updates.Write a function
obsStrCond
that takes a nonempty arrayfuncArr
of functions with typestring => string
, a function f with typestring => boolean
, and an Observableo
with typeObservable<string>
. It returns anObservable<string>
that updates wheno
updates, in the following way. Iff
returns true for the string obtained by applying the composition of the functions infuncArr
(with the function at index 0 being applied first) to the update value ofo
, then the returned observable should update with that string. Iff
returns false, update the returned observable with the unchanged update value ofo
.Write a function
statefulObserver
that takes anObservable<number>
o
as input and returns a newObservable<number>
which only updates if the current update value fromo
is divisible by the previous update value fromo
.
Optional Programming Tasks
The following questions were taken directly from past exams and do not count for credit. They are provided for additional practice only. You can see the autograder tests for these on gradescope, but they are uncredited. Solutions are on Canvas under Exams and Solutions.
[FALL 2022 MIDTERM 2] Write a function
mergeMax
that takes two Observableso1
ando2
with typeObservable<number>
, subscribes functions to each, and returns a new Observable which updates its subscribers whenever it receives a value which is no smaller than any value received so far from o1 or o2. Subscribers should be sent objects of the type { obs: number, v: number }, where v is the value received, and obs is either 1 or 2, indicating the source of the value.[SPRING 2022 FINAL EXAM] Write a function
merge(o1: Observable<string>, o2: Observable<string>): Observable<string>
returning a newObservable<string>
which, whenever either o1 or o2 are updated, will be updated with the same value.[SPRING 2022 MIDTERM 2] Consider the Observable class from lecture, receiving updates with positive values. Implement a method
greaterAvg
returning a newObservable<number>
that is updated with every number that is at least 50% larger than the average of the previous two numbers. Hint: usethis
.[FALL 2021 FINAL EXAM] Consider the Observable class provided in observable.ts, receiving updates with numeric values. In observables.ts implement a method
signChange()
that returns a newObservable<number>
which will be updated with every nonzero value v that is not preceded by a nonzero value of the same sign. Hint: usethis
.- Write a function
usingSignChange
that takes an array of numbersnumArr
and an observer functionf
. You should subscribef
to the observer returned by thesignChange
method and then update the observer returned bySignChangeObservable
with every number innumArr
starting at index 0. The function should return nothing.
- Write a function