public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
A pair of useful extensions to NSDictionary.

Use objectForKey:withDefault: to lookup an object and return a specified 
default if no object is found.

Use each: to iterate over the key-object pairs in a dictionary. Pass it a 
block with two arguments: (key object).
timburks (author)
Fri Jan 18 14:42:25 -0800 2008
commit  7989745269899a7e7186e62071a6b1018a376470
tree    9d052722b5780592404188dc8d42d231d7148106
parent  d00ae79f27a8018ea984cf71426ee3cfc37ee2cd
...
137
138
139
140
 
 
 
 
 
 
 
 
 
 
 
141
142
143
...
137
138
139
 
140
141
142
143
144
145
146
147
148
149
150
151
152
153
0
@@ -137,7 +137,17 @@
0
      (imethod (id) handleUnknownMessage:(id) method withContext:(id) context is
0
           (if (eq (method length) 1)
0
               (then (self objectForKey:((method car) evalWithContext: context)))
0
- (else (super handleUnknownMessage:method withContext:context)))))
0
+ (else (super handleUnknownMessage:method withContext:context))))
0
+
0
+ ;; Look up an object by key, return the specified default if no object is found.
0
+ (imethod (id) objectForKey:(id)key withDefault:(id)default is
0
+ (cond ((self objectForKey:key))
0
+ (else default)))
0
+
0
+ ;; Iterate over the key-object pairs in a dictionary. Pass it a block with two arguments: (key object).
0
+ (imethod (id) each:(id) block is
0
+ ((self allKeys) each:
0
+ (do (key) (block key (self objectForKey:key))))))
0
 
0
 
0
 (class NSString
...
5
6
7
8
 
9
10
11
...
15
16
17
18
19
 
 
20
21
22
...
25
26
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
...
5
6
7
 
8
9
10
11
...
15
16
17
 
 
18
19
20
21
22
...
25
26
27
 
 
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
0
@@ -5,7 +5,7 @@
0
 
0
 (class TestDictionary is NuTestCase
0
      
0
- (imethod (id) testSet is
0
+ (imethod (id) testSet is
0
           (set d (NSMutableDictionary dictionary))
0
           (d set:(one:1
0
                   "two" 2
0
@@ -15,8 +15,8 @@
0
           (assert_equal "three" (d valueForKey:"three")))
0
      
0
      (imethod (id) testCreate is
0
- (set d (NSMutableDictionary dictionaryWithList:(one:1
0
- "two" 2
0
+ (set d (NSMutableDictionary dictionaryWithList:(one:1
0
+ "two" 2
0
                                                           three:"three")))
0
           (assert_equal 3 (d count))
0
           (assert_equal 2 (d valueForKey:"two"))
0
@@ -25,6 +25,19 @@
0
      (imethod (id) testAutomaticAccessor is
0
           (set d (dict "one" 1 two:2))
0
           (assert_equal 1 (d "one"))
0
- (assert_equal 2 (d "two"))))
0
-
0
+ (assert_equal 2 (d "two")))
0
+
0
+ (imethod (id) testEach is
0
+ (set d (dict "one" 1 two:2))
0
+ (set count 0)
0
+ (d each:
0
+ (do (k v)
0
+ (assert_equal (d objectForKey:k) v)
0
+ (set count (+ count 1))))
0
+ (assert_equal (d count) count))
0
+
0
+ (imethod (id) testLookupWithDefault is
0
+ (set d (dict "one" 1 two:2))
0
+ (assert_equal 1 (d objectForKey:"one" withDefault:3))
0
+ (assert_equal 3 (d objectForKey:"three" withDefault:3))))
0
 
...
22
23
24
25
 
26
27
28
...
22
23
24
 
25
26
27
28
0
@@ -22,7 +22,7 @@
0
           (assert_equal "hello, world" (+ "hello" "," " " "world")))
0
      
0
      ;; Our "ternary operator" is really a method and not an operator, but I think it belongs here anyway.
0
- (imethod (id) testTernaryOperator is
0
+ (imethod (id) dontTestTheDeprecatedTernaryOperator is
0
           (assert_equal "no" (nil ? "yes" : "no"))
0
           (assert_equal "no" (() ? "yes" : "no"))
0
           (assert_equal "no" (0 ? "yes" : "no"))

Comments

    No one has commented yet.