Permalink
Browse files

enumerator for Nucell

  • Loading branch information...
1 parent e322e3c commit 3b124d14104d970ff2fcf24a5ee83d04eef99689 @scutdavy committed Aug 30, 2013
Showing with 31 additions and 6 deletions.
  1. +31 −6 objc/Nu.m
View
@@ -2777,16 +2777,46 @@ + (NSString *) stringValue{
#pragma mark - NuCell.m
+@interface NuCellEnumerator : NSEnumerator
++ (instancetype) enumeratorWithCell:(NuCell *) cell;
+@end
+
+@interface NuCellEnumerator ()
+@property (nonatomic, strong) NuCell *cursor;
+@end
+
+@implementation NuCellEnumerator
++ (instancetype) enumeratorWithCell:(NuCell *)cell{
+ NuCellEnumerator *enumerator = [NuCellEnumerator new];
+ enumerator.cursor = cell;
+ return enumerator;
+}
+
+- (id) nextObject{
+ if (self.cursor == nil || self.cursor == [NSNull NU_null]) return nil;
+ id ret = self.cursor;
+ self.cursor = self.cursor.cdr;
+ return ret;
+}
+@end
+
+
+
@interface NuCell ()
@property (nonatomic) int file;
@property (nonatomic) int line;
- (id) allChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *) context;
- (id) eitherChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *) context;
- (id) eachEvaluatedListInContext:(NSMutableDictionary *) context;
+- (NuCellEnumerator *) enumerator;
@end
@implementation NuCell
+- (NuCellEnumerator *) enumerator{
+ return [NuCellEnumerator enumeratorWithCell:self];
+}
+
+ (id) cellWithCar:(id)car cdr:(id)cdr{
NuCell *cell = [[self alloc] init];
[cell setCar:car];
@@ -2914,11 +2944,7 @@ - (id) handleUnknownMessage:(NuCell *) method withContext:(NSMutableDictionary *
}
- (id) lastObject{
- id cursor = self;
- while ([cursor cdr] != [NSNull NU_null]) {
- cursor = [cursor cdr];
- }
- return [cursor car];
+ return [[[[self enumerator] allObjects] lastObject] car];
}
- (NSMutableString *) stringValue{
@@ -3040,7 +3066,6 @@ - (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context{
cursor = [cursor cdr];
}
return evaluatedArguments;
-
}

0 comments on commit 3b124d1

Please sign in to comment.