Skip to content

Lecture 6 & Lecture 11 #9

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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
done lecture 11
  • Loading branch information
Quanghkim committed Feb 8, 2025
commit 706a6483cbcc175d0971b69508282b9247c5d6d9
2 changes: 1 addition & 1 deletion src/main/java/com/rxmobileteam/lecture11/Riddle1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Riddle1 {
*/
fun solve(value: Int): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.just(value)
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/rxmobileteam/lecture11/Riddle10.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ object Riddle10 {
*
* Use case: Get some user data and perform a network request with the user data and have both data accessible afterwards.
*/
fun solve(first: Observable<Int>, function: (Int) -> Observable<String>): Observable<Pair<Int, String>> {
// TODO: implement this method
throw ExerciseNotCompletedException()
fun solve(
first: Observable<Int>,
function: (Int) -> Observable<String>
): Observable<Pair<Int, String>> {
return first.flatMap(
{ value -> function(value) },
{ value: Int, functionResult: String -> Pair(value, functionResult) }
)
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

object Riddle11 {
/**
Expand All @@ -10,7 +11,6 @@ object Riddle11 {
* Use case: Handle the click of a button right away but prevent double clicking by not handling multiple click events within a given time window.
*/
fun solve(source: Observable<Unit>): Observable<Unit> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.throttleFirst(300L, TimeUnit.MILLISECONDS)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle12.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle12 {
* Use case: Getting a network error and you want to recover and show some default state.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.onErrorReturnItem(5)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle13 {
* Use case: You only want to observe changes of a value but don't care if the same value has been emitted consecutively.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.distinctUntilChanged()
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle14.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle14 {
* Use case: Retry an operation for a number of times or until a valid error occurred.
*/
fun solve(source: Single<Unit>): Single<Unit> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.retry(2) { t -> t !is IllegalArgumentException }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle15.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle15 {
* Use case: You have two sources of your data (cache & network request). You want to subscribe to both right away and keep the emission order.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.concatEager(listOf(first, second))
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle16.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ object Riddle16 {
* Use case: The [source] Observable is a TextField and you want to issue a network request while disposing the old requests in case the user has typed something new.
*/
fun solve(source: Observable<String>, function: (String) -> Single<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.switchMapSingle {
function(it)
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle17.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle17 {
* Use case: Reactive types are lazy by default. Hence, you might also want to get the value upon the subscription and not execution time.
*/
fun solve(function: () -> Int): Single<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Single.fromSupplier(function)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle18.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle18 {
* Use case: You have multiple sources and want to get the data from either one and then be consistent and not switch between multiple sources.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.amb(listOf(first, second))
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle19.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ object Riddle19 {
* Use case: Transform any listener into an Observable.
*/
fun solve(interaction: Interaction): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.create { emitter ->
interaction.listener = emitter::onNext
}
}

interface Interaction {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle2 {
* Use case: You want to transform the data.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.map { it + 1 }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle20.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle20 {
* Use case: There something you want to execute and in your UI you have multiple trigger points.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return first.mergeWith(second)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle21.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle21 {
* Use case: Sometimes you can't do everything reactively and need to break out of it.
*/
fun solve(source: Observable<Int>): Int {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.blockingFirst()
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle22.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle22 {
* Use case: Group related data while skipping over some of it.
*/
fun solve(source: Observable<Int>): Observable<List<Int>> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.buffer(2, 3)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle23.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle23 {
* Use case: You get some data from a bad source and know for sure it's of a certain type that you require.
*/
fun solve(source: Observable<Any>): Observable<String> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.map { it as String }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle24.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Riddle24 {
* Use case: Know how many emissions have been sent out.
*/
fun solve(source: Observable<Any>): Single<Long> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.count()
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle25.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle25 {
* Use case: Continue with data if the stream is empty.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.defaultIfEmpty(5)
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle26.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

object Riddle26 {
/**
Expand All @@ -10,7 +11,6 @@ object Riddle26 {
* Use case: Delay emission of events to simulate some indication.
*/
fun solve(source: Observable<Long>): Observable<Long> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.delay(300, TimeUnit.MILLISECONDS)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle27.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle27 {
* Use case: Add some logging.
*/
fun solve(source: Observable<Long>, function: (Long) -> Unit): Observable<Long> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.doOnNext(function)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle28.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle28 {
* Use case: Add some logging.
*/
fun solve(source: Completable, function: () -> Unit): Completable {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.doOnComplete(function)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle29.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle29 {
* Use case: Add some logging.
*/
fun solve(source: Maybe<Int>, function: (Throwable) -> Unit): Maybe<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.doOnError(function)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle3 {
* Use case: You want to filter certain items out.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.filter { it % 2 == 0 }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle30.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle30 {
* Use case: Add some logging.
*/
fun solve(source: Single<Int>, function: () -> Unit): Single<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.doOnSubscribe { function.invoke() }
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle31.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle31 {
* Use case: You want to re-run a certain Observable a number of times.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.repeat(3)
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle32.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Single
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

object Riddle32 {
Expand All @@ -11,7 +12,6 @@ object Riddle32 {
* Use case: You want to terminate the given reactive type and stop the operation.
*/
fun solve(source: Single<Long>): Single<Long> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.timeout(3, TimeUnit.SECONDS)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle33.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Riddle33 {
* Use case: You want to shift work to a particular [Scheduler].
*/
fun solve(source: Completable, scheduler: Scheduler): Completable {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.observeOn(scheduler)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle34.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Riddle34 {
* Use case: You want to shift work to a particular [Scheduler].
*/
fun solve(source: Completable, scheduler: Scheduler): Completable {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.subscribeOn(scheduler)
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle35.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Riddle35 {
* Use case: You have a local cache and only want to hit the network if the cache misses.
*/
fun solve(first: Maybe<String>, second: Single<String>): Single<String> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return first.switchIfEmpty(second)
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle36.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

object Riddle36 {
/**
Expand All @@ -10,7 +11,6 @@ object Riddle36 {
* Use case: You want the user-input to trigger a search request for the entered text but only when no changes have been made for a pre-determined time to avoid unnecessary requests.
*/
fun solve(source: Observable<String>, milliseconds: Long): Observable<String> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.debounce(milliseconds, TimeUnit.MILLISECONDS)
}
}
9 changes: 7 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle37.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import java.io.IOException

object Riddle37 {
/**
Expand All @@ -10,7 +11,11 @@ object Riddle37 {
* Use case: You want to recover from an expected error and map them to a particular result.
*/
fun solve(source: Observable<Boolean>): Observable<Boolean> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.onErrorResumeNext {
when (it) {
is IOException -> Observable.just(false)
else -> Observable.error(it)
}
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Riddle4 {
* Use case: Some button that can toggle two states. For instance a switch between White & Dark theme.
*/
fun solve(source: Observable<Unit>): Observable<Boolean> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.scan(false) { acc, _ -> !acc }
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle5.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.functions.BiFunction

object Riddle5 {
/**
Expand All @@ -10,7 +11,6 @@ object Riddle5 {
* Use case: Two input fields in a calculator that need to be summed up and the result should be updated every time one of the inputs change.
*/
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Observable.combineLatest(first, second, { a, b -> a + b })
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle6.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ object Riddle6 {
* Use case: Execute two network requests in parallel and wait for each other and process the combined data.
*/
fun solve(first: Single<Int>, second: Single<Int>): Single<Pair<Int, Int>> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return Single.zip(first, second, { a, b -> a to b })
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle7.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object Riddle7 {
* Use case: You never want to show the same value twice.
*/
fun solve(source: Observable<Int>): Observable<Int> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.distinct()
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/rxmobileteam/lecture11/Riddle8.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rxmobileteam.lecture11

import com.rxmobileteam.utils.ExerciseNotCompletedException
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

object Riddle8 {
/**
Expand All @@ -10,7 +11,6 @@ object Riddle8 {
* Use case: Make an Observable "lazy" for some time. For instance, when wanting to postpone some UI action.
*/
fun solve(source: Observable<Unit>): Observable<Unit> {
// TODO: implement this method
throw ExerciseNotCompletedException()
return source.delaySubscription(200, TimeUnit.MILLISECONDS)
}
}
Loading