Chain programming and functional programming, chain programming functions

Source: Internet
Author: User

Chain programming and functional programming, chain programming functions

Speaking of chain programming and functional programming, Do not be nervous.

This is actually the case when I listen to it. I believe that those who have experience in C # development or other programming, as long as they are not OC, they will know at a glance.

Let's look at the two lines of code:

1 Person * person = [[Person alloc] init]; 2 person. run (1, 9.2 ). eat (@ "banana "). run (1, 1.2 ). eat (@ "noodle ");

The above is chain programming + functional programming.

Let's explain in a vernacular: see the parameters in the brackets. Is it similar to the function call in C? in other languages, parameters are transmitted using parentheses. Only OC uses colons to transmit parameters.

The method call uses ".", while the OC uses [] + space.

If you want to call these methods based on OC, it is estimated that four rows are required. The object calls the methods one by one, but the chain is done in such a row.

 

Let's analyze what parameters can be passed through parentheses () in OC. That is, Block. When Block is called, blockName (para) is like this.

Therefore, we infer that the run (para) and eat (para) Methods certainly return a Block and a Block with a parameter.

So how can I explain the call method through "point? The point is actually to call the getter method, so the run and eat methods have no parameters, and there is only one returned Block value.

In addition, if you can continue, what does it mean? Block also has a return value, and the type of the return value is the type of the current object.

Haha, now I understand.

 

Let's go to the Code:

Person header file:

1 # import <Foundation/Foundation. h> 2 3 @ interface Person: NSObject 4 5 // the return value of the function is a Block, the return value of the Block is the current object, and the Block has a parameter 6-(Person * (^) (double distance) run; 7 8 // the return value of the function is a Block, the return value of the Block is the current object, and the Block has a parameter 9-(Person * (^) (NSString * food) eat; 10 11 @ end

The. m file of the Person class:

1 # import "Person. h "2 3 @ implementation Person 4 5-(Person * (^) (double )) run {6 // The method returns a Block with no return value as a parameter. 7 return ^ (double distance) {8 NSLog (@ "run: % f", distance ); 9 // The block return value is the current object 10 return self; 11}; 12} 13 14-(Person * (^) (NSString *)) eat {15 // The returned method is a "Block with no return value" 16 return ^ (NSString * food) {17 NSLog (@ "eat: % @", food ); 18 // The block return value is the current Object 19 return self; 20}; 21} 22 23 @ end

As you can see, you must check the two comments, which is the core of the whole process.

Next, call.

1 # import "ViewController. h "2 # import" Person. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad {11 [super viewDidLoad]; 12 13 Person * person = [[Person alloc] init]; 14 15 // This is chained programming + functional programming 16 person. run (1, 9.2 ). eat (@ "banana "). run (1, 1.2 ). eat (@ "noodle"); 17} 18 19 @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.