使用RxScala进行树和尾递归

时间:2018-07-19 23:24:57

标签: scala rx-java rx-scala

我正在尝试用rxscala https://medium.com/bystevenp/comment-trees-and-recursion-with-rxjava-d147a904610a重写此示例,但是卡住了使其变为尾递归

case class Item(hasKids: Boolean, kids: List[Int])
def getItem(i: Int): Observable[Item] = ???

@tailrec
def getComments(parentItem: Item): Observable[Item] = {
   if (parentItem.hasKids) {

    Observable.from(parentItem.kids)
      .flatMap {
        id =>
           getItem(id)
       }.flatMap {
          item =>
            getComments(item)

    } merge Observable.just(parentItem)

   } else {
    Observable.just(parentItem)
  }
}

0 个答案:

没有答案