Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active June 15, 2021 02:49
Show Gist options
  • Save theoknock/f8763de97f207275153edeb4a62a8c63 to your computer and use it in GitHub Desktop.
Save theoknock/f8763de97f207275153edeb4a62a8c63 to your computer and use it in GitHub Desktop.
Unix-style pipe function composition block design pattern that accepts class properties as parameters without triggering a retain cycle warning from the compiler: "Block implicitly retains 'self'...").
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) int a;
@property (nonatomic) int b;
@property (nonatomic) int c;
@property (nonatomic) int d;
@property (nonatomic) int e;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_a = 1;
_b = 2;
_c = 3;
_d = 4;
_e = 5;
NSLog(@"_a (%d) + _b (%d) + _c (%d) + _d (%d) + _e (%d)\t\t== %d\n\n", _a, _b, _c, _d, _e, _a + _b + _c + _d + _e);
[self test];
}
- (int)test {
int(^cd_ab_e_)(void) = ^(int(^ab_)(int), int(^cd_)(int), int e_) {
return ^ {
NSLog(@"e_\t\t\t\t\t\t\t== %d", e_);
NSLog(@"ab_(e_)\t\t\t\t\t== %d", ab_(e_));
NSLog(@"cd_(ab_(e_))\t\t\t\t== %d", cd_(ab_(e_)));
return cd_(ab_(e_));
};
} (^ (int a_, int b_) {
return ^ int (int e__) {
NSLog(@"a_ (%d) + b_ (%d) + e__ (%d)\t== %d", a_, b_, e__, a_ + b_ + e__);
return a_ + b_ + e__;
};
} (_a, _b), ^ (int c_, int d_) {
return ^ int (int ab_e_) {
NSLog(@"c_ (%d) + d_ (%d) + ab_e_ (%d)\t== %d", c_, d_, ab_e_, c_ + d_ + ab_e_);
return c_ + d_ + ab_e_;
};
} (_c, _d), _e);
return cd_ab_e_();
}
// Raw-material refinery (ab) --> raw material (e)
// --> refined material (abe) --> goods manufacturer (cd)
// --> to make goods (cdabe)
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment