Skip to content

Commit

Permalink
A method to convert an NSSet to a list
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewschleifer committed Nov 28, 2010
1 parent 1ebb73c commit fa7e4b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions objc/extensions.h
Expand Up @@ -77,6 +77,8 @@ limitations under the License.
@interface NSSet(Nu)
/*! Creates a set that contains the contents of a specified list. */
+ (NSSet *) setWithList:(id) list;
/*! Convert a set into a list. */
- (NuCell *) list;
@end

/*!
Expand Down
21 changes: 21 additions & 0 deletions objc/extensions.m
Expand Up @@ -164,6 +164,27 @@ + (NSSet *) setWithList:(id) list
return s;
}

// Convert a set into a list.
- (NuCell *) list
{
NSEnumerator *setEnumerator = [self objectEnumerator];
NSObject *anObject = [setEnumerator nextObject];

if(!anObject)
return nil;

NuCell *result = [[[NuCell alloc] init] autorelease];
NuCell *cursor = result;
[cursor setCar:anObject];

while ((anObject = [setEnumerator nextObject])) {
[cursor setCdr:[[[NuCell alloc] init] autorelease]];
cursor = [cursor cdr];
[cursor setCar:anObject];
}
return result;
}

@end

@implementation NSMutableSet(Nu)
Expand Down

0 comments on commit fa7e4b4

Please sign in to comment.