aboutsummaryrefslogtreecommitdiffstats
path: root/NSDictionary+LispExtensions.m
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2008-04-02 19:20:20 -0400
committerBrian Cully <bjc@kublai.com>2008-04-02 19:20:20 -0400
commitab10720260e2c184b319026da89f4dfd338500bb (patch)
treea692a27435da0296972e43b21b2f35762e720bfd /NSDictionary+LispExtensions.m
downloadmoxie-ab10720260e2c184b319026da89f4dfd338500bb.tar.gz
moxie-ab10720260e2c184b319026da89f4dfd338500bb.zip
Initial commit
Diffstat (limited to 'NSDictionary+LispExtensions.m')
-rw-r--r--NSDictionary+LispExtensions.m54
1 files changed, 54 insertions, 0 deletions
diff --git a/NSDictionary+LispExtensions.m b/NSDictionary+LispExtensions.m
new file mode 100644
index 0000000..ee0c01f
--- /dev/null
+++ b/NSDictionary+LispExtensions.m
@@ -0,0 +1,54 @@
+//
+// NSDictionary+LispExtensions.m
+// Moxie
+//
+// Created by Brian Cully on Mon Sep 13 2004.
+// Copyright (c) 2004 Brian Cully. All rights reserved.
+//
+
+#import "NSDictionary+LispExtensions.h"
+
+@implementation NSDictionary (LispExtensions)
+/*
+ * Assoc Lists are arrays of the form ((KEY0 VALUES0) (KEY1 VALUES1) ... (KEYN VALUESN)).
+ * Useful for lisp, not for Cocoa where we can use NSDictionary.
+ */
++ (NSMutableDictionary *)dictionaryWithAlist: (NSArray *)attrList
+{
+ NSEnumerator *objEnum;
+ NSMutableDictionary *result;
+ NSArray *row;
+
+ result = [NSMutableDictionary dictionary];
+ objEnum = [attrList objectEnumerator];
+ while ((row = [objEnum nextObject]) != nil) {
+ NSArray *subarray;
+ NSRange valueRange;
+ id key;
+
+ key = [row objectAtIndex: 0];
+ valueRange = NSMakeRange(1, [row count] - 1);
+ subarray = [row subarrayWithRange: valueRange];
+ [result setObject: subarray forKey: key];
+ }
+ return result;
+}
+
+- (NSString *)lispForm
+{
+ NSEnumerator *keyEnum;
+ NSMutableString *result;
+ id key;
+
+ result = [NSMutableString stringWithString: @"("];
+ keyEnum = [self keyEnumerator];
+ while ((key = [keyEnum nextObject]) != nil) {
+ id value;
+
+ value = [self objectForKey: key];
+ [result appendFormat: @"(%@ . %@)", [key lispForm], [value lispForm]];
+ }
+ [result appendString: @")"];
+ return result;
+}
+@end \ No newline at end of file