Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Pairs are now tuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Rix authored and Rob Rix committed Mar 21, 2013
1 parent 9f3cf20 commit c6457c7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
2 changes: 1 addition & 1 deletion RXCollections/RXFold.m
Expand Up @@ -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) {
Expand Down
19 changes: 9 additions & 10 deletions RXCollections/RXPair.h
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
47 changes: 13 additions & 34 deletions RXCollections/RXPair.m
Expand Up @@ -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];
}


Expand All @@ -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];
}


Expand Down
8 changes: 4 additions & 4 deletions RXCollections/RXRecursiveEnumerator.m
Expand Up @@ -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]));
}

Expand All @@ -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");
}
Expand Down

0 comments on commit c6457c7

Please sign in to comment.