iOS__链式编程思想|函数式编程思想

1.链式编程思想:方法的返回值是block必须有返回值(本身对象),block参数(需要操作的值)

#import "ViewController.h"
具体计算放在Calculation类中的block进行,返回对象本身
int result = [NSObject mas_makeCalculate:^(Calculation *cal) {
//       [[[cal add:5]add:3]add:9];正常编程思想
       cal.add(3).add(5).add(5);
   }];
    NSLog(@"%d",result);
#import "NSObject+CalculateMenager.h"
+(int)mas_makeCalculate:(void (^)(Calculation *))block
{
    Calculation *cal = [[Calculation alloc]init];
    block(cal);
    return cal.result;
}
#import "Calculation.h"
-(Calculation* (^)(int))add
{
    return ^(int value){
        self.result += value;
        return self;
    };
}

2.函数式编程思想

#import "CalculateMenage.h"
-(instancetype)calculate:(int (^)(int))calculateBlock
{
_result = calculateBlock(_result);
    return self;
}
#import "ViewController.m"
 #import "CalculateMenage.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)viewDidLoad {
    [super viewDidLoad];
    CalculateMenage *menager = [[CalculateMenage alloc]init];
  int result = [[menager calculate:^int(int result) {
        result += 5;
        result += 5;
        return result;
    }]result];
    NSLog(@"%d",result);
}

你可能感兴趣的:(iOS__链式编程思想|函数式编程思想)