读《最快让你上手ReactiveCocoa之基础篇》随笔

最近作者在读上的一篇名为《最快让你上手ReactiveCocoa之基础篇》的文章的时候,看到了作者在文章上用函数式编程的原理做了一个简单的计算器,但文中并未贴出Caculator这个类的实现,于是作者试着实现了一下,最终也能完成这个类的实现,其实实现的代码很简单,主要理解函数式编程到底是个什么样的概念。

以下是文章的地址:http://www.jianshu.com/p/87ef6720a096

以下是完成的代码:

首先是类的定义


Caculator.h

#import@interface Caculator : NSObject

@property (nonatomic ,assign) BOOL isEqual;

@property (nonatomic, assign) int result;

-(Caculator * )caculotor:(int(^)(int result))cacultor;

-(Caculator * )equal:(BOOL(^)(int result))operation;

@end

下面是类的实现

#import "Caculator.h"

@implementation Caculator

-(instancetype)init{

if (self = [super init]) {

_result = 0;

}return self;

}

-(Caculator * )caculotor:(int(^)(int result))cacultor{

_result = cacultor(_result);

return  self;

}

-(Caculator * )equal:(BOOL(^)(int result))operation{

self.isEqual = operation(_result);

return self;

}

@end

最后是调用方法

- (void)viewDidLoad {

[super viewDidLoad];

Caculator * caculaotr= [[Caculator alloc]init];

BOOL isEqual =  [[[caculaotr caculotor:^int(int result) {

result+=2;

result*=5;

return result;

}] equal:^BOOL(int result) {

return  result==10;

}] isEqual];

NSLog(@"isEqual%d",isEqual);

// Do any additional setup after loading the view, typically from a nib.

}

你可能感兴趣的:(读《最快让你上手ReactiveCocoa之基础篇》随笔)