Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 50 additions and 126 deletions.
  1. +50 −126 objc/Nu.m
View
176 objc/Nu.m
@@ -2887,28 +2887,16 @@ - (id) fifth{
}
- (id) nth:(int) n{
- if (n == 1)
- return _car;
- id cursor = _cdr;
- int i;
- for (i = 2; i < n; i++) {
- cursor = [cursor cdr];
- if (cursor == [NSNull NU_null]) return nil;
- }
- return [cursor car];
+ return [self objectAtIndex:n];
}
- (id) objectAtIndex:(int) n{
- if (n < 0)
- return nil;
- else if (n == 0)
- return _car;
- id cursor = _cdr;
- for (int i = 1; i < n; i++) {
- cursor = [cursor cdr];
- if (cursor == [NSNull NU_null]) return nil;
- }
- return [cursor car];
+ if (n < 0) return nil;
+
+ NSArray *cells = [[self enumerator] allObjects];
+ if ([cells count] <= n) return nil;
+
+ return [[cells objectAtIndex:n] car];
}
// When an unknown message is received by a cell, treat it as a call to objectAtIndex:
@@ -3006,37 +2994,35 @@ - (void) addToException:(NuException*)e value:(id)value{
- (id) allChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *) context{
NSParameterAssert(block);
- id cursor = self;
- NSParameterAssert([cursor cdr]);
- id current = [[cursor car] evalWithContext:context];
+ NSParameterAssert([self cdr]);
+ id current = [[self car] evalWithContext:context];
- while (cursor && (cursor != [NSNull NU_null]) &&
- [cursor cdr] && [cursor cdr] != [NSNull NU_null]) {
+ for (id cursor in [self enumerator]) {
+ if ([cursor cdr] == nil || [cursor cdr] == [NSNull NU_null]) break;
+
id next = [[[cursor cdr] car] evalWithContext:context];
if (!block(current, next)) {
return [context NU_false];
}
current = next;
- cursor = [cursor cdr];
}
return [context NU_true];
}
- (id) eitherChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *) context{
NSParameterAssert(block);
- id cursor = self;
- NSParameterAssert([cursor cdr]);
- id current = [[cursor car] evalWithContext:context];
+ NSParameterAssert([self cdr]);
+ id current = [[self car] evalWithContext:context];
- while (cursor && (cursor != [NSNull NU_null]) &&
- [cursor cdr] && [cursor cdr] != [NSNull NU_null]) {
+ for (id cursor in [self enumerator]) {
+ if ([cursor cdr] == nil || [cursor cdr] == [NSNull NU_null]) break;
+
id next = [[[cursor cdr] car] evalWithContext:context];
if (block(current, next)) {
return [context NU_true];
}
current = next;
- cursor = [cursor cdr];
}
return [context NU_false];
@@ -3050,9 +3036,9 @@ - (id) eachEvaluatedListInContext:(NSMutableDictionary *) context{
- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context{
NuCell *evaluatedArguments = nil;
- id cursor = self;
id outCursor = nil;
- while (cursor && (cursor != [NSNull NU_null])) {
+
+ for (id cursor in [self enumerator]) {
id nextValue = block(cursor, context);
id newCell = [[[NuCell alloc] init] autorelease];
[newCell setCar:nextValue];
@@ -3063,8 +3049,8 @@ - (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context{
[outCursor setCdr:newCell];
}
outCursor = newCell;
- cursor = [cursor cdr];
}
+
return evaluatedArguments;
}
@@ -3124,11 +3110,9 @@ - (id) evalWithContext:(NSMutableDictionary *)context{
- (id) each:(id) block{
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
- id cursor = self;
- while (cursor && (cursor != [NSNull NU_null])) {
+ for (id cursor in [self enumerator]) {
[args setCar:[cursor car]];
[block evalWithArguments:args context:[NSNull NU_null]];
- cursor = [cursor cdr];
}
[args release];
}
@@ -3139,12 +3123,10 @@ - (id) eachPair:(id) block{
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
[args setCdr:[[[NuCell alloc] init] autorelease]];
- id cursor = self;
- while (cursor && (cursor != [NSNull NU_null])) {
+ for (id cursor in [self enumerator]) {
[args setCar:[cursor car]];
[[args cdr] setCar:[[cursor cdr] car]];
[block evalWithArguments:args context:[NSNull NU_null]];
- cursor = [[cursor cdr] cdr];
}
[args release];
}
@@ -3155,13 +3137,12 @@ - (id) eachWithIndex:(id) block{
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
[args setCdr:[[[NuCell alloc] init] autorelease]];
- id cursor = self;
int i = 0;
- while (cursor && (cursor != [NSNull NU_null])) {
+
+ for (id cursor in [self enumerator]) {
[args setCar:[cursor car]];
[[args cdr] setCar:[NSNumber numberWithInt:i]];
[block evalWithArguments:args context:[NSNull NU_null]];
- cursor = [cursor cdr];
i++;
}
[args release];
@@ -3173,16 +3154,15 @@ - (id) select:(id) block{
NuCell *parent = [[[NuCell alloc] init] autorelease];
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
- id cursor = self;
id resultCursor = parent;
- while (cursor && (cursor != [NSNull NU_null])) {
+
+ for (id cursor in [self enumerator]) {
[args setCar:[cursor car]];
id result = [block evalWithArguments:args context:[NSNull NU_null]];
if (nu_valueIsTrue(result)) {
[resultCursor setCdr:[NuCell cellWithCar:[cursor car] cdr:[resultCursor cdr]]];
resultCursor = [resultCursor cdr];
}
- cursor = [cursor cdr];
}
[args release];
}
@@ -3195,15 +3175,14 @@ - (id) select:(id) block{
- (id) find:(id) block{
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
- id cursor = self;
- while (cursor && (cursor != [NSNull NU_null])) {
+
+ for (id cursor in [self enumerator]) {
[args setCar:[cursor car]];
id result = [block evalWithArguments:args context:[NSNull NU_null]];
if (nu_valueIsTrue(result)) {
[args release];
return [cursor car];
}
- cursor = [cursor cdr];
}
[args release];
}
@@ -3214,13 +3193,11 @@ - (id) map:(id) block{
NuCell *parent = [[[NuCell alloc] init] autorelease];
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
- id cursor = self;
id resultCursor = parent;
- while (cursor && (cursor != [NSNull NU_null])) {
+ for (id cursor in [self enumerator]) {
[args setCar:[cursor car]];
id result = [block evalWithArguments:args context:[NSNull NU_null]];
[resultCursor setCdr:[NuCell cellWithCar:result cdr:[resultCursor cdr]]];
- cursor = [cursor cdr];
resultCursor = [resultCursor cdr];
}
[args release];
@@ -3234,13 +3211,11 @@ - (id) map:(id) block{
- (id) mapSelector:(SEL) sel{
NuCell *parent = [[NuCell alloc] init];
id args = [[NuCell alloc] init];
- id cursor = self;
id resultCursor = parent;
- while (cursor && (cursor != [NSNull NU_null])) {
+ for (id cursor in [self enumerator]) {
id object = [cursor car];
id result = [object performSelector:sel];
[resultCursor setCdr:[NuCell cellWithCar:result cdr:[resultCursor cdr]]];
- cursor = [cursor cdr];
resultCursor = [resultCursor cdr];
}
[args release];
@@ -3254,36 +3229,22 @@ - (id) reduce:(id) block from:(id) initial{
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
id args = [[NuCell alloc] init];
[args setCdr:[[[NuCell alloc] init] autorelease]];
- id cursor = self;
- while (cursor && (cursor != [NSNull NU_null])) {
+ for (id cursor in [self enumerator]) {
[args setCar:result];
[[args cdr] setCar:[cursor car]];
result = [block evalWithArguments:args context:[NSNull NU_null]];
- cursor = [cursor cdr];
}
[args release];
}
return result;
}
- (NSUInteger) length{
- int count = 0;
- id cursor = self;
- while (cursor && (cursor != [NSNull NU_null])) {
- cursor = [cursor cdr];
- count++;
- }
- return count;
+ return [[[self enumerator] allObjects] count];
}
- (NSMutableArray *) array{
- NSMutableArray *a = [NSMutableArray array];
- id cursor = self;
- while (cursor && cursor != [NSNull NU_null]) {
- [a addObject:[cursor car]];
- cursor = [cursor cdr];
- }
- return a;
+ return [[[self enumerator] allObjects] valueForKey:@"car"];
}
- (NSUInteger) count{
@@ -3594,9 +3555,7 @@ @implementation NuEnumerable
- (id) each:(id) callable{
id args = [[NuCell alloc] init];
if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
@try
{
[args setCar:object];
@@ -3612,6 +3571,7 @@ - (id) each:(id) callable{
[args release];
@throw(exception);
}
+
}
}
[args release];
@@ -3622,10 +3582,8 @@ - (id) eachWithIndex:(NuBlock *) block{
id args = [[NuCell alloc] init];
[args setCdr:[[[NuCell alloc] init] autorelease]];
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
int i = 0;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
@try
{
[args setCar:object];
@@ -3651,9 +3609,7 @@ - (id) eachWithIndex:(NuBlock *) block{
- (NSArray *) select{
NSMutableArray *selected = [NSMutableArray array];
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
if (nu_valueIsTrue(object)) {
[selected addObject:object];
}
@@ -3665,9 +3621,7 @@ - (NSArray *) select:(NuBlock *) block{
NSMutableArray *selected = [NSMutableArray array];
id args = [[NuCell alloc] init];
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
[args setCar:object];
id result = [block evalWithArguments:args context:[NSNull NU_null]];
if (nu_valueIsTrue(result)) {
@@ -3682,9 +3636,7 @@ - (NSArray *) select:(NuBlock *) block{
- (id) find:(NuBlock *) block{
id args = [[NuCell alloc] init];
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
[args setCar:object];
id result = [block evalWithArguments:args context:[NSNull NU_null]];
if (nu_valueIsTrue(result)) {
@@ -3701,9 +3653,7 @@ - (NSArray *) map:(id) callable{
NSMutableArray *results = [NSMutableArray array];
id args = [[NuCell alloc] init];
if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
[args setCar:object];
[results addObject:[callable evalWithArguments:args context:nil]];
}
@@ -3717,10 +3667,8 @@ - (NSArray *) mapWithIndex:(id) callable{
id args = [[NuCell alloc] init];
[args setCdr:[[[NuCell alloc] init] autorelease]];
if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
int i = 0;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
[args setCar:object];
[[args cdr] setCar:[NSNumber numberWithInt:i]];
[results addObject:[callable evalWithArguments:args context:nil]];
@@ -3733,10 +3681,7 @@ - (NSArray *) mapWithIndex:(id) callable{
- (NSArray *) mapSelector:(SEL) sel{
NSMutableArray *results = [NSMutableArray array];
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
- // this will fail (crash!) if the selector returns any type other than an object.
+ for (id object in [self objectEnumerator]) {
[results addObject:[object performSelector:sel]];
}
return results;
@@ -3747,9 +3692,7 @@ - (id) reduce:(id) callable from:(id) initial{
[args setCdr:[[[NuCell alloc] init] autorelease]];
id result = initial;
if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
[args setCar:result];
[[args cdr] setCar: object];
result = [callable evalWithArguments:args context:nil];
@@ -3766,9 +3709,7 @@ - (id) maximum:(NuBlock *) block{
[args setCdr:[[[NuCell alloc] init] autorelease]];
if (nu_objectIsKindOfClass(block, [NuBlock class])) {
- NSEnumerator *enumerator = [self objectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self objectEnumerator]) {
if (!bestObject) {
bestObject = object;
}
@@ -4003,13 +3944,7 @@ - (id) eachEvaluatedListInContext:(NSMutableArray *) context{
@implementation NSArray(Nu)
+ (NSArray *) arrayWithList:(id) list{
- NSMutableArray *a = [NSMutableArray array];
- id cursor = list;
- while (cursor && cursor != [NSNull NU_null]) {
- [a addObject:[cursor car]];
- cursor = [cursor cdr];
- }
- return a;
+ return [[list array] mutableCopy] ?: [NSMutableArray array];
}
// When an unknown message is received by an array, treat it as a call to objectAtIndex:
@@ -4073,9 +4008,7 @@ - (id) reduceLeft:(id)callable from:(id) initial{
- (id) eachInReverse:(id) callable{
id args = [[NuCell alloc] init];
if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
- NSEnumerator *enumerator = [self reverseObjectEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self reverseObjectEnumerator]) {
@try
{
[args setCar:object];
@@ -4142,13 +4075,8 @@ - (void) sortUsingBlock:(NuBlock *) block{
@implementation NSSet(Nu)
+ (NSSet *) setWithList:(id) list{
- NSMutableSet *s = [NSMutableSet set];
- id cursor = list;
- while (cursor && cursor != [NSNull NU_null]) {
- [s addObject:[cursor car]];
- cursor = [cursor cdr];
- }
- return s;
+ NSArray *array = [list array] ?: [NSMutableArray array];
+ return [[NSSet setWithArray:array] mutableCopy];
}
// Convert a set into a list.
@@ -4245,9 +4173,7 @@ - (id) handleUnknownMessage:(NuCell *) method withContext:(NSMutableDictionary *
- (id) each:(id) block{
id args = [[NuCell alloc] init];
[args setCdr:[[[NuCell alloc] init] autorelease]];
- NSEnumerator *keyEnumerator = [[self allKeys] objectEnumerator];
- id key;
- while ((key = [keyEnumerator nextObject])) {
+ for (id key in [[self allKeys] objectEnumerator]) {
@try
{
[args setCar:key];
@@ -4272,9 +4198,7 @@ - (NSDictionary *) map: (id) callable{
NSMutableDictionary *results = [NSMutableDictionary dictionary];
id args = [[NuCell alloc] init];
if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
- NSEnumerator *enumerator = [self keyEnumerator];
- id object;
- while ((object = [enumerator nextObject])) {
+ for (id object in [self keyEnumerator]) {
[args setCar:object];
[args setCdr:[[[NuCell alloc] init] autorelease]];
[[args cdr] setCar:[self objectForKey:object]];

No commit comments for this range

Something went wrong with that request. Please try again.