2

Is there a better / more idiomatic way to use a Maybe type from JavaRx 2 than flatMap and try/catch? The following example takes a Maybe<User> and tries to book them a random ticket for a flight. If the user doesn't exist, return an empty Observable.

fun bookRandomTicketFor(userId: UUID): Observable<Ticket> {
    val agencies = travelAgents() // Observable<TravelAgency>
    val user = findById(userId) // Maybe<User>
    val location = locate() // Observable<GeoLocation>

    return Observable
            .just(user.toObservable())
            .flatMap { usr ->
                try {
                    usr.zipWith(location, { aUser, location ->
                        agencies
                                .flatMap { agency ->
                                    agency
                                            .search(aUser, location) // Observable<Flight>.
                                            .toList() // Convert to List<Flight>.
                                            .toObservable() // And to Observable<List<Flight>>.
                                            .flatMap { flights -> // So it can be shuffled,
                                                Observable.just( // giving a random order.
                                                        shuffle(flights as MutableList<Flight>)[0]
                                                )
                                            }
                                }.firstElement() // Now take the first randomly shuffled Flight.
                    }).flatMap { flight ->
                        book(user.toObservable(), flight.toObservable())
                    }
                } catch (ex: Exception) {
                    Observable.empty<Ticket>()
                }
            }
            .doOnSubscribe { Logger.log("Random ticket: start for $userId") }
            .doOnComplete { Logger.log("Random ticket: exit for $userId") }
}

It seems a bit of a fudge to have to convert the Maybe<User> to an Observable and start with an Observable<Observable<User>> that I can then flatMap and try/catch. Just wondering if there is a neater approach to doing this?

tynn
  • 38,113
  • 8
  • 108
  • 143
junglie85
  • 1,243
  • 10
  • 30

0 Answers0