aboutsummaryrefslogtreecommitdiffstats
path: root/NSDictionary+LispExtensions.m
blob: ee0c01fdcd92c720f2179657ba7a969696ccb8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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