public
Fork of timburks/nu
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/patrickt/nu.git
Search Repo:
Fixed a header declaration for reduce:  and enabled map: and each: to work 
with callable objects as well.
patrickt (author)
Thu May 29 13:30:05 -0700 2008
commit  3e05dfe87c056d5d6754baaa2262ca7c172153e0
tree    735e2f7609fd283e2fe7e3dd71a10e5ce3cf2e96
parent  8fc05c32464c0965e42a086f4fbf97f2992d0298
...
26
27
28
 
 
29
30
31
32
33
34
35
 
 
36
37
38
...
41
42
43
44
45
46
 
 
47
48
49
 
50
51
52
...
26
27
28
29
30
31
32
33
34
35
 
 
36
37
38
39
40
...
43
44
45
 
 
 
46
47
48
49
 
50
51
52
53
0
@@ -26,13 +26,15 @@ limitations under the License.
0
     @discussion This class implements methods that act on enumerated collections of objects.
0
     It is designed to be mixed into a class using the include method that Nu adds to NSObject.
0
     The receiving class must have an objectEnumerator method that returns an NSEnumerator.
0
+ Some methods in this class take a callable object as an argument; callable objects are those
0
+ that have evalWithArguments:context: defined.
0
  */
0
 @interface NuEnumerable : NSObject
0
 {
0
 }
0
 
0
-/*! Iterate over each member of a collection, evaluating the provided block for each member. */
0
-- (id) each:(NuBlock *) block;
0
+/*! Iterate over each member of a collection, evaluating the provided callable item for each member. */
0
+- (id) each:(id) callable;
0
 /*! Iterate over each member of a collection, evaluating the provided block for each member.
0
     The block is expected to take two arguments: the member and its index. */
0
 - (id) eachWithIndex:(NuBlock *) block;
0
@@ -41,12 +43,11 @@ limitations under the License.
0
 /*! Iterate over each member of a collection, returning the first element for which the provided block evaluates non-nil. */
0
 - (id) find:(NuBlock *) block;
0
 /*! Iterate over each member of a collection, applying the provided block to each member, and returning an array of the results. */
0
-- (NSArray *) map:(NuBlock *) block;
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
+- (NSArray *) map:(id) callable;
0
+/*! Iterate over each member of a collection, using the provided callable to combine members into a single return value.
0
 */
0
 
0
-- (id) reduce:(NSObject *) callable from:(id) initial;
0
+- (id) reduce:(id) 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;
...
27
28
29
30
 
31
32
33
 
34
35
36
37
38
 
39
40
41
...
109
110
111
112
 
113
114
115
116
 
117
118
119
120
121
 
122
123
124
...
137
138
139
140
 
141
142
143
...
27
28
29
 
30
31
32
 
33
34
35
36
37
 
38
39
40
41
...
109
110
111
 
112
113
114
115
 
116
117
118
119
120
 
121
122
123
124
...
137
138
139
 
140
141
142
143
0
@@ -27,15 +27,15 @@ limitations under the License.
0
 
0
 @implementation NuEnumerable
0
 
0
-- (id) each:(NuBlock *) block
0
+- (id) each:(id) callable
0
 {
0
     id args = [[NuCell alloc] init];
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:object];
0
- [block evalWithArguments:args context:Nu__null];
0
+ [callable evalWithArguments:args context:nil];
0
         }
0
     }
0
     [args release];
0
@@ -109,16 +109,16 @@ static bool nu_valueIsTrue(id value)
0
     return Nu__null;
0
 }
0
 
0
-- (NSArray *) map:(NuBlock *) block
0
+- (NSArray *) map:(id) callable
0
 {
0
     NSMutableArray *results = [[NSMutableArray alloc] init];
0
     id args = [[NuCell alloc] init];
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:object];
0
- [results addObject:[block evalWithArguments:args context:Nu__null]];
0
+ [results addObject:[callable evalWithArguments:args context:nil]];
0
         }
0
     }
0
     [args release];
0
@@ -137,7 +137,7 @@ static bool nu_valueIsTrue(id value)
0
     return results;
0
 }
0
 
0
-- (id) reduce:(NSObject *) callable from:(id) initial
0
+- (id) reduce:(id) callable from:(id) initial
0
 {
0
     id args = [[NuCell alloc] init];
0
     [args setCdr:[[[NuCell alloc] init] autorelease]];
...
9
10
11
12
 
 
13
14
15
...
42
43
44
45
 
 
 
 
 
 
46
47
48
...
9
10
11
 
12
13
14
15
16
...
43
44
45
 
46
47
48
49
50
51
52
53
54
0
@@ -9,7 +9,8 @@
0
         (set sum 0)
0
         ((array 100 200 300) each:
0
          (do (n) (set sum (+ sum n))))
0
- (assert_equal 600 sum))
0
+ (assert_equal 600 sum)
0
+ ((array 100 200 300) each:puts))
0
      
0
      (- testEachWithIndex is
0
         (set sum 0)
0
@@ -42,7 +43,12 @@
0
         (assert_equal 2 found))
0
      
0
      (- testMap is
0
- (assert_equal 3 ((((array 100 200 300) map:(do (n) (n stringValue))) 1) length)))
0
+ (assert_equal 3 ((((array 100 200 300) map:(do (n) (n stringValue))) 1) length))
0
+ ;; Testing mapping an operator onto an array
0
+ (set words (array "the girl" "from ipanema"))
0
+ (set regexen (words map: regex))
0
+ (set wanted (array /the girl/ /from ipanema/))
0
+ (assert_equal wanted regexen))
0
      
0
      (- testReduce is
0
         (set testArray (array 100 200 300))

Comments

    No one has commented yet.