RxSwift method chain

Introduction

Nice to meet you! I'm Setaro during RxSwift warrior training with Tupple.

This is a memorandum and memo of the method chain that I have learned since I started writing RxSwift.

What is a method chain?

By writing a combination of methods, I am programming with a method to avoid the side effects of functional programming, and I am writing more readable and declarative code. In order to realize declarative coding, the description of the process by the method chain is basic and important.

Example by map and subscribe

//Double the entered Int type value
_ = Observable.just(10) 
.map { $0 * 2 } 
.subscribe(onNext: { print($0) // => 20 
})

As long as the type is the same, you can connect processing such as .map for the rest of your life, In this case, if you subscribe, the type will change, so dispose Bag and exit.

(Maybe people who write this in a connected way say "relatively declarative writing")

And if you change the return value of the closure of map to Int → String Events that can be subscribed are also Int → String. ** The type that can be subscribed to depends on the return value of the closure used in the map **

RxSwift operators implement method chains by defining interfaces that use generics.

Recommended Posts

RxSwift method chain
Method
Java method
to_i method
java (method)
getRequestDispatcher () method
merge method
Map method
include method
Abstract method
initialize method
List method
puts method
Java method
Class method
save! method
getParameter method
[Java] method
private method
rails method
[Java] method
[Java] Handling of JavaBeans in the method chain
[Android] HttpClient (wrapper of HttpURLConnection. Method chain version.
Method name of method chain in Java Builder + α