This repository has been archived by the owner on May 19, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement destructuring pattern matching.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2014 Rob Rix. All rights reserved. | ||
|
||
#import <Reducers/REDFilter.h> | ||
|
||
@interface HMRCase : NSObject | ||
|
||
+(id)match:(id)object withCases:(NSArray *)cases; | ||
|
||
+(instancetype)case:(REDPredicateBlock)predicate then:(id(^)())block; | ||
|
||
-(id)evaluateWithObject:(id)object; | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2014 Rob Rix. All rights reserved. | ||
|
||
#import "HMRCase.h" | ||
#import "HMRCombinator.h" | ||
#import <Obstruct/apply.h> | ||
|
||
@implementation HMRCase { | ||
REDPredicateBlock _predicate; | ||
id (^_block)(); | ||
} | ||
|
||
+(id)match:(id)object withCases:(NSArray *)cases { | ||
id result; | ||
|
||
for (HMRCase *each in cases) { | ||
result = [each evaluateWithObject:object]; | ||
|
||
if (result != nil) break; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
l3_test(@selector(match:withCases:)) { | ||
id object = [NSObject new]; | ||
l3_expect([HMRCase match:object withCases:@[ [HMRCase case:REDTruePredicateBlock then:^{ return @YES; }] ]]).to.equal(@YES); | ||
l3_expect([HMRCase match:object withCases:@[ [HMRCase case:HMRBind then:REDIdentityMapBlock] ]]).to.equal(object); | ||
l3_expect([HMRCase match:HMRConcatenate(HMRLiteral(@"x"), HMRLiteral(@"y")) withCases:@[ [HMRCase case:HMRConcatenationPredicate(nil, HMRBind) then:REDIdentityMapBlock] ]]).to.equal(HMRLiteral(@"y")); | ||
} | ||
|
||
|
||
+(instancetype)case:(REDPredicateBlock)predicate then:(id(^)())block { | ||
return [[self alloc] initWithPredicate:predicate thenBlock:block]; | ||
} | ||
|
||
-(instancetype)initWithPredicate:(REDPredicateBlock)predicate thenBlock:(id(^)())block { | ||
NSParameterAssert(block != nil); | ||
|
||
if ((self = [super init])) { | ||
_predicate = [predicate ?: REDTruePredicateBlock copy]; | ||
_block = [block copy]; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
static _Thread_local CFMutableArrayRef variables; | ||
|
||
-(id)evaluateWithObject:(id)object { | ||
CFMutableArrayRef previous = variables; | ||
NSMutableArray *bindings = [NSMutableArray new]; | ||
variables = (CFMutableArrayRef)CFBridgingRetain(bindings); | ||
|
||
id result; | ||
|
||
if (_predicate(object)) { | ||
result = obstruct_apply_array(_block, bindings); | ||
} | ||
|
||
variables = previous; | ||
|
||
return result; | ||
} | ||
|
||
@end | ||
|
||
|
||
REDPredicateBlock const HMRBind = ^bool (id object) { | ||
[(NSMutableArray *)CFBridgingRelease(variables) addObject:object]; | ||
return YES; | ||
}; |