title: Composing network request with RX and Kotlin
body: <p>I am working on an Android application that uses kotlin with RXJava and Retrofit for network calls. On startup, the application will do several network calls to a REST-like backend. The procedure look very much like the one used by Netflix in their examples. However when trying to rewrite it to work with Kotlin I am struggling. I have trouble understanding how returns work in Kotlin closures. Would very much appreciate some help here!</p>
  
  <p>Netflix example:</p>
  
  <pre><code>    return getUser(userId).flatMap(user -&gt; {
          Observable&lt;Map&lt;String, Object&gt;&gt; catalog =    getPersonalizedCatalog(user)
              .flatMap(catalogList -&gt; catalogList.videos().&lt;Map&lt;String, Object&gt;&gt; flatMap(
                      video -&gt; {
                          Observable&lt;Bookmark&gt; bookmark = getBookmark(video);
                          Observable&lt;Rating&gt; rating = getRatings(video);
                          Observable&lt;VideoMetadata&gt; metadata = getVideoMetadata(video);
                          return Observable.zip(bookmark, rating, metadata, (b, r, m) -&gt; combineVideoData(video, b, r, m));
                      }));
  
      Observable&lt;Map&lt;String, Object&gt;&gt; social = getSocial(user).map(s -&gt; {
          return s.getDataAsMap();
      });
  
      return Observable.merge(catalog, social);
  }).flatMap(data -&gt; {
      String json = SimpleJson.mapToJson(data);
      return response.writeStringAndFlush("data: " + json + "\n");
  });
  </code></pre>
  
  <p>My solution:</p>
  
  <pre><code>restClient.getVideoList("VAR1", "VAR2")
                  .subscribeOn(Schedulers.io())
                  .flatMap { videos -&gt; Observable.from(videos)}
                  .map { video -&gt;
                      var actors: Observable&lt;List&lt;Actor&gt;&gt; = restClient.getActorList(video.id(), "VAR2")
                      var price = restClient.getPrice(video.id(), "VAR2")
                      Observable.zip(actors, price, { (a, p) -&gt;  combineVideoData(video, a, p)})
                  }.map{ video -&gt; doSomethingWithVideo(video) }
  </code></pre>
  
  <p>For some reason my implementation returns an observable and not a video object which it i supposed to. There is no doubt something wrong with my understanding of closures in Kotlin, and I have tried to figure it out for days, but still struggling. Would be very grateful for any help!</p>