Permalink
Browse files

Merge branch 'feature/pairs-are-tuples' into develop

  • Loading branch information...
2 parents a3d7fe2 + c6457c7 commit 44d28df71dc1a3323d3b5efd6f9874fa158583b8 Rob Rix committed Mar 21, 2013
@@ -135,6 +135,13 @@
remoteGlobalIDString = D4B62B4B16A1581700E42C84;
remoteInfo = "Lagrangian (Mac OS X framework)";
};
+ D496681D16FB151A002C0FA8 /* PBXContainerItemProxy */ = {
+ isa = PBXContainerItemProxy;
+ containerPortal = D4B62B2B16A1177A00E42C84 /* Lagrangian.xcodeproj */;
+ proxyType = 1;
+ remoteGlobalIDString = D4A5B2C0164B4FAF00338EB4;
+ remoteInfo = lagrangian;
+ };
D4B62B3516A1177B00E42C84 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = D4B62B2B16A1177A00E42C84 /* Lagrangian.xcodeproj */;
@@ -511,6 +518,7 @@
buildRules = (
);
dependencies = (
+ D496681E16FB151A002C0FA8 /* PBXTargetDependency */,
D473F66816DA87E8000FCD8D /* PBXTargetDependency */,
);
name = "RXCollections (Mac OS X framework)";
@@ -703,6 +711,11 @@
name = "Lagrangian (Mac OS X framework)";
targetProxy = D473F66B16DA8832000FCD8D /* PBXContainerItemProxy */;
};
+ D496681E16FB151A002C0FA8 /* PBXTargetDependency */ = {
+ isa = PBXTargetDependency;
+ name = lagrangian;
+ targetProxy = D496681D16FB151A002C0FA8 /* PBXContainerItemProxy */;
+ };
/* End PBXTargetDependency section */
/* Begin PBXVariantGroup section */
@@ -49,7 +49,7 @@ id RXFold(id<NSFastEnumeration> enumeration, id initial, RXFoldBlock block) {
}
@l3_test("construct dictionaries from enumerations of pairs") {
- l3_assert(RXConstructDictionary(@[[RXPair pairWithKey:@1 value:@1], [RXPair pairWithKey:@2 value:@4], [RXPair pairWithKey:@3 value:@9]]), l3_is(@{@1: @1, @2: @4, @3: @9}));
+ l3_assert(RXConstructDictionary(@[[RXTuple tupleWithKey:@1 value:@1], [RXTuple tupleWithKey:@2 value:@4], [RXTuple tupleWithKey:@3 value:@9]]), l3_is(@{@1: @1, @2: @4, @3: @9}));
}
NSDictionary *RXConstructDictionary(id<NSFastEnumeration> enumeration) {
@@ -2,19 +2,18 @@
// Created by Rob Rix on 2013-01-12.
// Copyright (c) 2013 Rob Rix. All rights reserved.
-#import <Foundation/Foundation.h>
+#import <RXCollections/RXTuple.h>
-@interface RXPair : NSObject <NSCopying>
+@protocol RXPair <NSObject>
-+(instancetype)pairWithLeft:(id)left right:(id)right;
--(instancetype)initWithLeft:(id)left right:(id)right;
++(instancetype)tupleWithLeft:(id)left right:(id)right;
@property (nonatomic, strong, readonly) id left;
@property (nonatomic, strong, readonly) id right;
-@property (nonatomic, copy, readonly) NSArray *elements;
+@end
--(bool)isEqualToPair:(RXPair *)pair;
+@interface RXTuple (RXPair) <RXPair>
@end
@@ -26,9 +25,9 @@
@end
-@interface RXPair (RXKeyValuePair) <RXKeyValuePair>
+@interface RXTuple (RXKeyValuePair) <RXKeyValuePair>
-+(instancetype)pairWithKey:(id<NSCopying>)key value:(id)value;
++(instancetype)tupleWithKey:(id<NSCopying>)key value:(id)value;
@end
@@ -40,8 +39,8 @@
@end
-@interface RXPair (RXLinkedListNode) <RXLinkedListNode>
+@interface RXTuple (RXLinkedListNode) <RXLinkedListNode>
-+(instancetype)pairWithFirst:(id)first rest:(id)rest;
++(instancetype)tupleWithFirst:(id)first rest:(id)rest;
@end
@@ -4,49 +4,28 @@
#import "RXPair.h"
-@implementation RXPair
+@implementation RXTuple (RXPair)
-+(instancetype)pairWithLeft:(id)left right:(id)right {
- return [[self alloc] initWithLeft:left right:right];
++(instancetype)tupleWithLeft:(id)left right:(id)right {
+ return [self tupleWithObjects:(const id []){ left, right } count:2];
}
--(instancetype)initWithLeft:(id)left right:(id)right {
- if ((self = [super init])) {
- _left = left;
- _right = right;
- }
- return self;
-}
-
-
--(NSArray *)elements {
- return @[self.left, self.right];
-}
-
--(bool)isEqualToPair:(RXPair *)pair {
- return
- [pair isKindOfClass:self.class]
- && [self.left isEqual:pair.left]
- && [self.right isEqual:pair.right];
-}
-
--(BOOL)isEqual:(id)object {
- return [self isEqualToPair:object];
+-(id)left {
+ return self[0];
}
-
--(instancetype)copyWithZone:(NSZone *)zone {
- return self;
+-(id)right {
+ return self[1];
}
@end
-@implementation RXPair (RXKeyValuePair)
+@implementation RXTuple (RXKeyValuePair)
-+(instancetype)pairWithKey:(id<NSCopying>)key value:(id)value {
- return [[self alloc] initWithLeft:key right:value];
++(instancetype)tupleWithKey:(id<NSCopying>)key value:(id)value {
+ return [self tupleWithLeft:key right:value];
}
@@ -61,10 +40,10 @@ -(id)value {
@end
-@implementation RXPair (RXLinkedListNode)
+@implementation RXTuple (RXLinkedListNode)
-+(instancetype)pairWithFirst:(id)first rest:(id)rest {
- return [self pairWithLeft:first right:rest];
++(instancetype)tupleWithFirst:(id)first rest:(id)rest {
+ return [self tupleWithLeft:first right:rest];
}
@@ -29,9 +29,9 @@ -(instancetype)initWithTarget:(id)target keyPath:(NSString *)keyPath {
@l3_test("accumulates the contents of homogeneous trees in depth-first order") {
- RXPair *tree = [RXPair pairWithLeft:[RXPair pairWithLeft:@"x" right:[RXPair pairWithLeft:@"y" right:@"z"]] right:@"w"];
+ RXTuple *tree = [RXTuple tupleWithLeft:[RXTuple tupleWithLeft:@"x" right:[RXTuple tupleWithLeft:@"y" right:@"z"]] right:@"w"];
NSMutableArray *flattened = [NSMutableArray new];
- RXAccumulateRecursiveContentsOfTarget(flattened, tree, @"elements");
+ RXAccumulateRecursiveContentsOfTarget(flattened, tree, @"allObjects");
l3_assert(flattened, l3_equals(@[tree, tree.left, [tree.left left], [tree.left right], [[tree.left right] left], [[tree.left right] right], tree.right]));
}
@@ -46,8 +46,8 @@ static void RXAccumulateRecursiveContentsOfTarget(NSMutableArray *accumulator, i
@l3_test("recursively enumerates trees in depth-first order") {
- RXPair *tree = [RXPair pairWithLeft:[RXPair pairWithLeft:@"x" right:[RXPair pairWithLeft:@"y" right:@"z"]] right:@"w"];
- l3_assert(RXFold([RXRecursiveEnumerator enumeratorWithTarget:tree keyPath:@"elements"], @"", ^(NSString *memo, id each) {
+ RXTuple *tree = [RXTuple tupleWithLeft:[RXTuple tupleWithLeft:@"x" right:[RXTuple tupleWithLeft:@"y" right:@"z"]] right:@"w"];
+ l3_assert(RXFold([RXRecursiveEnumerator enumeratorWithTarget:tree keyPath:@"allObjects"], @"", ^(NSString *memo, id each) {
return [memo stringByAppendingString:[each isKindOfClass:[NSString class]]? each : @""];
}), @"xyzw");
}
@@ -11,6 +11,8 @@
-(id)objectAtIndexedSubscript:(NSUInteger)subscript;
+-(NSArray *)allObjects;
+
@property (nonatomic, readonly) NSUInteger count;
-(bool)isEqualToTuple:(RXTuple *)tuple;
@@ -110,6 +110,15 @@ -(instancetype)initWithArray:(NSArray *)array {
#pragma mark Access
+@l3_test("can return its contents as an array") {
+ l3_assert(([[RXTuple tupleWithArray:@[@1, @2]] allObjects]), (@[@1, @2]));
+}
+
+-(NSArray *)allObjects {
+ return [NSArray arrayWithObjects:self.elements count:self.count];
+}
+
+
@l3_test("has a specific count") {
RXTuple *tuple = [RXTuple tupleWithArray:@[@M_PI, @M_PI]];
l3_assert(tuple.count, 2);

0 comments on commit 44d28df

Please sign in to comment.