Permalink
Browse files

refactor eval list

  • Loading branch information...
1 parent 164c514 commit 6b7496f58abc10e2835f8b82f8a4faba52ee8a4a @scutdavy committed Sep 2, 2013
Showing with 20 additions and 36 deletions.
  1. +20 −36 objc/Nu.m
View
@@ -2813,6 +2813,7 @@ - (NuCellEnumerator *) cellEnumerator;
- (id) reduce:(NUAccumulationBlock) block initial:(id) initial context:(NSMutableDictionary *) context;
- (id) map:(NUCellMapBlock) block context:(NSMutableDictionary *) context;
+- (id) evalAsPrognInContext:(NSMutableDictionary *) context;
@end
@implementation NuCell
@@ -3032,6 +3033,14 @@ - (id) eitherChainedPairs:(NUCellPairBlock) block context:(NSMutableDictionary *
return [context NU_false];
}
+- (id) evalAsPrognInContext:(NSMutableDictionary *) context{
+ id value = [NSNull NU_null];
+ for (id cursor in [self cellEnumerator]) {
+ value = [[cursor car] evalWithContext:context];
+ }
+ return value;
+}
+
- (id) eachEvaluatedListInContext:(NSMutableDictionary *) context{
return [self map:^id(id cell, NSMutableDictionary *context) {
return [[cell car] evalWithContext:context];
@@ -3930,6 +3939,10 @@ - (id) cellEnumerator{
return nil;
}
+- (id) evalAsPrognInContext:(NSMutableDictionary *) context{
+ return nil;
+}
+
@end
@implementation NSArray(Nu)
@@ -6758,11 +6771,7 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context{
id condition = [[pairs car] car];
id test = [condition evalWithContext:context];
if (nu_valueIsTrue(test)) {
- value = test;
- for (id cursor in [[[pairs car] cdr] cellEnumerator]) {
- value = [[cursor car] evalWithContext:context];
- }
- return value;
+ return [[[pairs car] cdr] evalAsPrognInContext:context] ?: test;
}
}
return value;
@@ -6781,24 +6790,12 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context{
id condition = [[cases car] car];
id result = [condition evalWithContext:context];
if ([result isEqual:target]) {
- id value = [NSNull NU_null];
- id cursor = [[cases car] cdr];
- while (cursor && (cursor != [NSNull NU_null])) {
- value = [[cursor car] evalWithContext:context];
- cursor = [cursor cdr];
- }
- return value;
+ return [[[cases car] cdr] evalAsPrognInContext:context];
}
cases = [cases cdr];
}
// or return the last one
- id value = [NSNull NU_null];
- id cursor = [[cases car] cdr];
- while (cursor && (cursor != [NSNull NU_null])) {
- value = [[cursor car] evalWithContext:context];
- cursor = [cursor cdr];
- }
- return value;
+ return [[[cases car] cdr] evalAsPrognInContext:context];
}
@end
@@ -6891,10 +6888,7 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context{
@try
{
id expressions = [cdr cdr];
- while (expressions && (expressions != [NSNull NU_null])) {
- result = [[expressions car] evalWithContext:context];
- expressions = [expressions cdr];
- }
+ result = [expressions evalAsPrognInContext:context];
}
@catch (NuBreakException *exception) {
break;
@@ -6923,10 +6917,7 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context{
@try
{
id expressions = [cdr cdr];
- while (expressions && (expressions != [NSNull NU_null])) {
- result = [[expressions car] evalWithContext:context];
- expressions = [expressions cdr];
- }
+ result = [expressions evalAsPrognInContext:context];
}
@catch (NuBreakException *exception) {
break;
@@ -6962,10 +6953,7 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context{
@try
{
id expressions = [cdr cdr];
- while (expressions && (expressions != [NSNull NU_null])) {
- result = [[expressions car] evalWithContext:context];
- expressions = [expressions cdr];
- }
+ result = [expressions evalAsPrognInContext:context];
}
@catch (NuBreakException *exception) {
break;
@@ -7870,11 +7858,7 @@ @interface Nu_progn_operator : NuOperator
@implementation Nu_progn_operator
- (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context{
- id value = [NSNull NU_null];
- for (id cursor in [cdr cellEnumerator]) {
- value = [[cursor car] evalWithContext:context];
- }
- return value;
+ return [cdr evalAsPrognInContext:context];
}
@end

0 comments on commit 6b7496f

Please sign in to comment.