Permalink
Browse files

Pairs are now tuples.

  • Loading branch information...
1 parent 9f3cf20 commit c6457c78299e887f73571dbf45faaa07c9dad241 Rob Rix committed Mar 21, 2013
Showing with 27 additions and 49 deletions.
  1. +1 −1 RXCollections/RXFold.m
  2. +9 −10 RXCollections/RXPair.h
  3. +13 −34 RXCollections/RXPair.m
  4. +4 −4 RXCollections/RXRecursiveEnumerator.m
View
@@ -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) {
View
@@ -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
View
@@ -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");
}

0 comments on commit c6457c7

Please sign in to comment.