An introduction to functional responsive programming
Functional responsive programming is a programming paradigm. It is programming by building functions that manipulate sequences of data and then respond to those sequences. It combines functional and responsive programming
Functional programming
Functional programming is a programming paradigm that requires us to pass functions as arguments or return them as return values. We can combine different functions to get the result we want.
Let’s take a look at these examples:
/ / students let allStudents: [Student] = getSchoolStudents () / / three years students in class two let gradeThreeClassTwoStudents: [Student] = allStudents .filter { student in student.grade == 3 && student.class == 2 }Copy the code
Since we want to get the class of 2003, we pass the decision function of class of 2003 as a parameter to the filter method so that we can filter out class of 2003 from the whole school.
Class two of the three years / / every male students to sing a song of the changjiang gradeThreeClassTwoStudents. Filter {student in student. Sex. = = male}. ForEach {boy in Boy.singasong (name: "pruning plum ")}Copy the code
Similarly, we pass the gender judgment function to the filter method, so that we can filter out male students from class 2, grade 3, and then pass singing as a function to the forEach method. So every male students to sing “plum”.
/ / class two grade three students score higher than 90 parents awarded prizes gradeThreeClassTwoStudents. Filter {student in student. The score > 90}. The map {student in student.parent } .forEach { parent in parent.receiveAPrize() }Copy the code
Students with more than 90 points were screened out by score determination, and then map was used to transform them into parents. Finally, forEach was used to let each parent accept the award.
/ / print class two of the three years from high to low grades gradeThreeClassTwoStudents. Sorted {student0, student1 in student0.score > student1.score } .forEach { student in print("score: \(student.score), name: \(student.name)") }Copy the code
Pass a function of the sorting logic to the sorted method so that students are sorted by grade, and finally print out the grade and student name with forEach.
- The overall structure
It is worth noting that we first screened male students from class 2, and later screened students from class 2 with scores higher than 90. The filter method is used, but different judgment function is passed, so as to obtain different screening results. If you want to achieve this demand now: grade one grade two less than 60 students sing a “I am guilty”.
I believe that you will soon be able to find the corresponding implementation method. This is functional programming, and it allows us to combine different methods and different functions to achieve the desired result. You can imagine how tedious it would be if we did the same logic with a traditional for loop. So the advantages of functional trial programming are obvious: flexible, high reuse, simple and easy maintenance to adapt to various needs
Functional responsive programming
- Now you’ve seen how we use functional programming to manipulate sequences. We can actually take this sequence of operations a little bit further. For example, you can think of a button click event as a sequence:
ForEach {showAlert()} forEach {showAlert()} forEach {showAlert()} forEach {showAlert()}Copy the code
- This is ideal for handling click events, but the problem is that the elements in the sequence (click events) are generated asynchronously, and traditional sequences can’t describe this asynchronously. To solve this problem, the listen sequence Observable is created. It is also a sequence, except that the elements in the sequence can be produced synchronously or asynchronously:
Click on the button / / sequence let taps: observables < Void > = button. The rx. Tap. AsObservable () / / after each click popup tooltip taps. The subscribe (onNext: { showAlert() })Copy the code
- In this case, taps is the sequence of button-clicking events. We then respond to each click by popping up a prompt. This type of programming is called responsive programming. We combine functional programming with responsive programming to get functional responsive programming:
passwordOutlet.rx.text.orEmpty
.map { $0.characters.count >= minimalPasswordLength }
.bind(to: passwordValidOutlet.rx.isHidden)
.disposed(by: disposeBag)
Copy the code
We use different constructor functions to create the required data sequence. Finally, respond to the sequence in the appropriate way. This is functional responsive programming.
Data binding (Subscription)
- One of the more important concepts in RxSwift is data binding (subscription). To bind a listening sequence to an observer:
- Let’s compare these two pieces of code:
let image: UIImage = UIImage(named: ...)
imageView.image = image
Copy the code
let image: Observable<UIImage> = ...
image.bind(to: imageView.rx.image)
Copy the code
- The first piece of code we’re familiar with is to set a single image to the imageView.
- The second piece of code “syncs” an image sequence to the imageView. Images in this sequence can be generated asynchronously. The image defined here is the blue part of the image above (the listening sequence) and imageView.rx.image is the orange part of the image above (the observer). This “synchronization mechanism” is data binding (subscription).
Since the | address