函数式编程思想:把操作尽可能的写成一系列的函数嵌套或者方法调用;
- 函数式编程本质:就是往方法中传入block,方法中嵌套block调用,把代码集合起来管理
- 函数式编程特点:每个方法必须有返回值(返回对象自身),把函数或者block当做参数;block的参数==需要操作的值(传参),block返回值==操作结果(返回值)
#import <Foundation/Foundation.h>
@interface CalculateManager : NSObject
@property (nonatomic, assign) int result;
- (instancetype)calculate:(int(^)(int))calculateBlock;
@end
#import "CalculateManager.h"
@implementation CalculateManager
- (instancetype)calculate:(int (^)(int))calculateBlock {
_result = calculateBlock(_result);
return self;
}
@end
---------------------------------------------
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CalculateManager *mgr = [[CalculateManager alloc] init];
int res = [[mgr calculate:^int(int result) {
result += 5;
result *= 5;
return result;
}] result];
NSLog(@"%i",res);
}