OC in the functional programming &amp

In the normal development of the third party framework we have a good reflection of the functional programming and chain programming —-Masonry.
example:
Masonry classic representative

[v mas_makeConstraints:^ (MASConstraintMaker *make) {/ / 1 / / equalTo: functional programming (self.view) / offset (40) / / 2 / / equalTo chain programming (self.view).Offset (40) make.top.equalTo (self.view).Offset (40); make.size.mas_offset (CGSizeMake (100, 100)); make.centerX.equalTo (self.view).Offset (-40);}];

Now we try to do functional programming, create a new Person class, the Person header file declarations and the realization of the two methods:

@interface Person: NSObject - (void) run; - (void) eat; @end
#import "Person.h" @implementation Person (void) run {NSLog (@%s ", __FUNCTION__);} - (void) eat {NSLog (@%s");} @end

Two methods of calling Person in ViewController:

Person *person = [Person new]; / / 1> run and eat need a separate call! Can / / 2> sequence! [person run]; [person eat];

This is the traditional way of writing, run and eat can only be called alone, and can not go to the combination call, and now we set a goal 1: chain programming

Goal 1: / / / / chain programming this is what we want to achieve, can be freely combined [[person run] eat [[person eat] run];]; / / solution: return a person object in the [person run] method, using person to call the person object method, in which the effect of ID to achieve the following: / / obj = [person run1]; [obj eat1];

Obj is actually a person object, person object with the method of calling person is no problem, with this goal we are now beginning to transform the code:

@interface Person: NSObject - (void) - (void) run; eat; Run1; - (Person) - (Person) eat1; @end @implementation Person (Person * Run1) {NSLog (@ "%s", __FUNCTION__); return self;} - (Person * eat1) {NSLog (@ "%s", __FUNCTION__); return self;}

Add new Run1 method and eat1 method in person class

ID obj = run1] [person; ID obj2 = [person eat1]; [obj2 run1]; NSLog (@ ""); [[person run1] eat1]; [obj eat1];

The above code output:

OC in the functional programming &amp
Snip20161026_6.png

can see that we have achieved our goal 1: chain programming, but has not yet achieved functional programming, and now we define the goal of 2: functional programming

Goal 2: / / functional programming features / functional programming is used '()' to call the method, then the OC at what time we return to the '()' method call it / / I believe many people first thought should be the Block person.run2 (.Eat2) / * * (person.eat2) (.Run2 (); more than two lines of code); we need to achieve the goal now.

Then to the Person class transformation code:

@interface Person: NSObject - (void) - (void) run; eat; Run1; - (Person) - (Person * eat1); / / on chain programming and functional programming / / if you want to achieve functional programming, need to return to `block` - (Person) (* ^ (run2) - (Person); * (^)) (eat2); @end @implementation Person (Person) (* ^ (run2)) {Person (^runBlock) (*) = ^ {NSLog (@ run2); return self; return runBlock;};} - (Person (* ^ {return (eat2))) ^ {NSLog (@ eat2); return self;}}; @end

Implementing method calls in ViewController:

2: / / functional programming! Person.run2 (.Eat2) / (person.eat2) (.Run2); / / (); / / person.run2 (=>): person.eat2 (person.run2);; (.Run2) (.Eat2) (.Run2) ();

The output is as follows:

OC in the functional programming &amp
Snip20161026_7.png

we see that we have now implemented functional programming. However, there is a problem in the Masonry () method is a parameter, we continue to transform the code:

// 在block中是可以带参数的,所以现在对代码进行如下改动 @interface Person : NSObject - (void)run; - (void)eat;  - (Person *)run1; - (Person *)eat1;  // 关于链式编程和函数式编程 // 如果要实现函数式编程,需要返回 `block` - (Person * (^)())run2; - (Person * (^)())eat2;  // 返回的 block 可以接参数 - (Person * (^)(NSString *food))eat3; - (Person * (^)(double distance))run3; @end  @implementation Person - (Person *(^)(NSString *))eat3 {      return ^ (NSString *food) {         NSLog(@"吃 %@", food);          return self;     }; }  - (Person *(^)(double))run3 {      return  ^ (double distance) {          NSLog(@"跑 %f", distance);          return self;     }; }  @end

Continue back to the ViewController call method:

2: / / functional programming! Person.run2 (.Eat2) / (person.eat2) (.Run2); / / (); / / person.run2 (=>): person.eat2 (person.run2);; (.Run2) (.Eat2) (.Run2) (NSLog); (@ "---"); person.run3 (1000).Eat3 (@ water).Run3 (1000).Eat3 (@ water);

Now look at the output:

OC in the functional programming &amp
Snip20161026_9.png

so far we have implemented chain programming and functional programming.