Code Monkey home page Code Monkey logo

rxriddles's Introduction

RxRiddles

This repository contains some riddles for RxJava to help you learn and master RxJava. There is an accompanying medium article that gives some more information.

Each riddle is in a single file with an accompanying unit test that will check your implementation. Next to the unit test, I have also put up my solution. Note that there are multiple ways to achieve and solve the riddles but usually there's a dedicated operator or function that I want to show you.

The riddles are not sorted in any real preference and I plan to keep it that way. Just start with whichever one you prefer. Riddles with numbers lower than 100 can be solved with a single operator while every other riddle requires multiple operators.

Contributing

I'm very open to having some more riddles. Especially some complex common use cases. In case you want to contribute create an issue and let's talk.

I want every riddle to be unique and have as little duplication as possible while also having some actual real-world use case for each riddle in mind. Something you can relate to and hopefully use in your everyday life.

RxJS

There is an initiative in RxRiddles-TypeScript project to re-write riddles from this project to RxJS and TypeScript. Take a look if you're interested.

License

Copyright (C) 2018 Vanniktech - Niklas Baudy

Licensed under the Apache License, Version 2.0

rxriddles's People

Contributors

chalup avatar dextorer avatar gulzar1996 avatar jakovljevic-mladen avatar joshuajamesong avatar jshvarts avatar luizgrp avatar lwasyl avatar masteramyx avatar renovate[bot] avatar vanniktech avatar vrendina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxriddles's Issues

Riddle number 6 does not check for parallelism

Hey,

There is a small problem with riddle number 6 and it is up to you guys how you want to solve it. In the task description there is mentioned that

Execute both [first] and [second] Single's in parallel...

however you solution works sequentially. Thats because there are no schedulers running on mentioned Singles which means everything executes on main thread. Test is passing because it is not really checking for parallelism. If you would want to do that you could

val first = Single.timer(5, SECONDS, rxRule)
        .map { 10 }
        .doOnSubscribe { subscribeCounter.incrementAndGet() }

val second = Single.timer(3, SECONDS, rxRule)
        .map { 5 }
        .doOnSubscribe { subscribeCounter.incrementAndGet() }

// rest of the test goes here...

rxRule.advanceTimeBy(4, SECONDS)

// check if second already finished and the first one did not
first.test().assertNotComplete()
second.test().assertComplete()

You get the idea. So if the intention was to check for parallelism you need to adjust solution. If solution is good then you need to adjust the description.

Empty test suite.

Im getting the following error in Android Studio:

Class not found: "com.vanniktech.rxriddles.Riddle1Test"Empty test suite.

Already tried, deleting the configuration, invalidating cache, but it still doesnt find the class.

RxSwift

Hey, I'd love to port this to Swift. I think it's a great idea. The Rx world on iOS is much less documented so I feel we need to port some of this RxJava knowledge to our side ;) Would you be open to me doing that?

I could write a medium post linking yours etc. Or however else you'd like to be credited in my port.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v3
  • gradle/wrapper-validation-action v1
  • actions/setup-java v3
gradle
build.gradle
  • org.jetbrains.kotlin:kotlin-gradle-plugin 1.7.20
  • io.reactivex.rxjava3:rxjava 3.1.5
  • junit:junit 4.13.2
  • org.assertj:assertj-core 3.23.1
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 7.5.1

  • Check this box to trigger a request for Renovate to run again on this repository

Riddle 24 should return Single<Long>

Currently Riddle24 returns Single<Int>. I think it should return Single<Long>, so that this solution works:

  fun solve(source: Observable<Any>): Single<Long> {
    return source.count()
  }

The current solution Riddle24Solution works because the function does not specify a return-type.

RxRiddle 1xx

Sometimes it comes to a case when we need to have cold observable which shares its emissions between multiple observers. For example, presentation layer calls for data and we want to store it in repository at the same call. I use publish().autoConnect(N) in that cases (N - number of observers). Here is an example of such usage:

  public Observable<User> getUsersAndStore() {
    Observable<User> listObservable = api.getUsers()
        .publish()
        .autoConnect(3);

// Here we store the result to ArrayMap<String, User>, 1st observer
    listObservable
        .collect(ArrayMap<String, User>::new,
            (destinationMap, user) -> destinationMap.put(user.type, user))
        .subscribe(typeWalletMap::putAll, Timber::e);

// 2nd
    listObservable
        .collect(ArrayMap<String, User>::new,
            (destinationMap, user) -> destinationMap.put(user.id, user))
        .subscribe(idWalletMap::putAll, Timber::e);

    return listObservable; // and finally return needed observable, the code which subscribes to it will be 
                                       // 3rd observer. That subscriber will initiate the emissions in the source 
                                       // observable.
  }

This approach seems to have side-effects(storing data to maps in this case), however we can't (or maybe just I can't) have code 100% free of side-effects, unfortunately. But the point is that all 3 observers use the same emissions without making source produce them 3 times.

Used literature: Thomas Nield, Learning RxJava '2017: Chapter 5 - Automatic connection

Question on Riddle #14

This is not an "issue", just wanting some clarification. The riddle says
Retry the given [source] up to three times unless an [IllegalArgumentException] has been emitted

However aren't we only "retrying" up to two times? We have an inital subscription, then comes the "retry" which happens twice. I was stuck on this because I kept setting retry(3, Predicate()).

Confusion with riddle #26

Firstly, thank you for this amazing Rx challenge you have put up, It has really helped me sharpen my Rx Skills.
Problem 26 confuses me because the test case passes for both delay() and delaySubscription() operator.
Looking at the marble diagram for delaySubscription() it delays the subscription for the source but there is no delay for subsequent emission. But the problem states that there should be a delay for subsequent emission too!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.