public
Fork of timburks/nu
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/patrickt/nu.git
Edited the reduce:from: method in NuEnumerable to accept any object that 
can be called. Though this is primarily useful for reducing with 
operators, it can be applied to any type of object with 
evalWithArguments:context: defined.
patrickt (author)
Wed May 28 08:24:42 -0700 2008
commit  c9fbf1c2e62355459ec1b9ce17fb67c4fde22784
tree    22108aac5a73447ef67b7cdcfb4f48a8fed56bfd
parent  5db99f224eadeecba62195423ec2d2973e8b4674
...
18
19
20
 
21
22
23
...
44
45
46
47
 
 
 
48
49
50
...
18
19
20
21
22
23
24
...
45
46
47
 
48
49
50
51
52
53
0
@@ -18,6 +18,7 @@ limitations under the License.
0
 #import <Foundation/Foundation.h>
0
 #import "cell.h"
0
 #import "block.h"
0
+#import "operator.h"
0
 
0
 /*!
0
     @class NuEnumerable
0
@@ -44,7 +45,9 @@ limitations under the License.
0
 /*! Iterate over each member of a collection, using the provided block to combine members into a single return value.
0
     The block is expected to take two arguments: the accumulated return value followed by the collection member.
0
 */
0
-- (id) reduce:(NuBlock *) block from:(id) initial;
0
+
0
+- (id) reduce:(NSObject *) callable from:(id) initial;
0
+
0
 /*! Iterate over each member of a collection, applying the provided selector to each member, and returning an array of the results. */
0
 - (NSArray *) mapSelector:(SEL) selector;
0
 @end
...
137
138
139
140
 
141
142
143
144
145
 
146
147
148
149
150
151
 
152
153
154
...
137
138
139
 
140
141
142
143
144
 
145
146
147
148
149
150
 
151
152
153
154
0
@@ -137,18 +137,18 @@ static bool nu_valueIsTrue(id value)
0
     return results;
0
 }
0
 
0
-- (id) reduce:(NuBlock *) block from:(id) initial
0
+- (id) reduce:(NSObject *) callable from:(id) initial
0
 {
0
     id args = [[NuCell alloc] init];
0
     [args setCdr:[[[NuCell alloc] init] autorelease]];
0
     id result = initial;
0
- if (nu_objectIsKindOfClass(block, [NuBlock class])) {
0
+ if ([callable respondsToSelector:@selector(evalWithArguments:context:)]) {
0
         NSEnumerator *enumerator = [self objectEnumerator];
0
         id object;
0
         while ((object = [enumerator nextObject])) {
0
             [args setCar:result];
0
             [[args cdr] setCar: object];
0
- result = [block evalWithArguments:args context:Nu__null];
0
+ result = [callable evalWithArguments:args context:nil];
0
         }
0
     }
0
     [args release];
...
45
46
47
 
48
49
50
 
 
 
 
 
 
 
51
52
53
...
45
46
47
48
49
 
 
50
51
52
53
54
55
56
57
58
59
0
@@ -45,9 +45,15 @@
0
         (assert_equal 3 ((((array 100 200 300) map:(do (n) (n stringValue))) 1) length)))
0
      
0
      (- testReduce is
0
+ (set testArray (array 100 200 300))
0
         (set reduction
0
- ((array 100 200 300) reduce:(do (sum n) (+ sum n)) from:0))
0
- (assert_equal 600 reduction))
0
+ (testArray reduce:(do (sum n) (+ sum n)) from:0))
0
+ (assert_equal 600 reduction)
0
+ (set reduction ((array 100 200 300) reduce:+ from:0))
0
+ (assert_equal 600 reduction)
0
+ (assert_equal (testArray reduce:(do (sum n) (+ sum n)) from:0) (testArray reduce:+ from:0)))
0
+
0
+
0
      
0
      (- testMapSelector is
0
         (assert_equal 3 ((((array 100 200 300) mapSelector:"stringValue") 1) length))))

Comments

    No one has commented yet.