1

I've just started learning Java stream API with kotlin and I'm using Project Reactor along with r2dbc API. I'm writing a simple code as given below and would appreciate in understanding how this works.

I want to know the line where t.let{} returns a MovieCeleb object using return@map. I want to know what this line does i.e., whether it creates a list/map and how it does this.

fun listAllMoviesBy(actor: String, client: DatabaseClient) {
    client.execute()
            .sql { "SELECT fin.id, full_name, movie_title, make_year, celeb_id, birth_date " +
                    "FROM featured_in fin JOIN film_celeb fc ON " +
                    " fin.celeb_id = fc.id WHERE fc.full_name = $1"
            }.bind(0, actor).map { t, u ->
                t.let {
                   val featuredIn = FeaturedIn(it.get("id") as Int, it.get("movie_title") as String, it.get("make_year") as Int)
                   val celeb = MovieCeleb(it.get("celeb_id") as Int, it.get("full_name") as String, it.get("birth_date") as LocalDate, featuredIn)
                    return@map celeb
                }
            }.all()
            .subscribe{
                println(it)
            }
}
Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
Mohammad
  • 187
  • 1
  • 12
  • 2
    Possible duplicate of [Kotlin: Whats does "return@" mean?](https://stackoverflow.com/questions/40160489/kotlin-whats-does-return-mean) – awesoon Apr 16 '19 at 08:50

1 Answers1

5

Given

.map { t, u ->
            t.let {
               val featuredIn = FeaturedIn(it.get("id") as Int, it.get("movie_title") as String, it.get("make_year") as Int)
               val celeb = MovieCeleb(it.get("celeb_id") as Int, it.get("full_name") as String, it.get("birth_date") as LocalDate, featuredIn)
                return@map celeb
            }
        }

return@map celeb will return celeb reference value from map function (not from let!). It is just some kind of so-called non-local return, whereas you can manually specify the return scope of your statement.

nyarian
  • 4,085
  • 1
  • 19
  • 51
  • I would also like to know when this mapping will be performed i.e, when the sql has returned the values? And how are the individual objects added to Flux API? – Mohammad Apr 16 '19 at 08:36
  • what does this question have to do with Flux API? it is completely different question. – nyarian Apr 16 '19 at 08:38
  • 1
    mapping will be performed after `bind` method will be executed and return a value that `map` method will operate on – nyarian Apr 16 '19 at 08:39
  • The ```.all()``` returns a ```Flux``` of objects. So I asked about the individual objects. But never mind I'll dig further – Mohammad Apr 16 '19 at 08:44