Skip to content

Instantly share code, notes, and snippets.

@rizumita
Last active October 19, 2021 09:54
Show Gist options
  • Save rizumita/a7f0364d4fc119b35ffd to your computer and use it in GitHub Desktop.
Save rizumita/a7f0364d4fc119b35ffd to your computer and use it in GitHub Desktop.
RxSwift recursive flatMap
func recursive(observable: Observable<Int>) -> Observable<Int> {
return observable.flatMap { value -> Observable<Int> in
guard value < 10 else {
return Variable(-1).asObservable()
}
print(value)
return recursive(Variable(value + 1).asObservable())
}
}
recursive(Variable(0).asObservable()).subscribeNext { _ in }
@iiiyu
Copy link

iiiyu commented Oct 22, 2015

it's so nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment