Re: -[NSMutableSet randomObject]

2010-05-25 Thread Jens Alfke
On May 25, 2010, at 9:49 AM, Michael A. Crawford wrote: >// If the array is empty, throw an exception. The caller should know > better. >// If there is only one, return object zero every time. >// If there are only two, alternate with every access. >// If there are three or more

Re: -[NSMutableSet randomObject]

2010-05-25 Thread Ricky Sharp
On May 25, 2010, at 11:49 AM, Michael A. Crawford wrote: >// If the array is empty, throw an exception. The caller should know > better. >// If there is only one, return object zero every time. >// If there are only two, alternate with every access. >// If there are three or mor

Re: -[NSMutableSet randomObject]

2010-05-25 Thread Thomas Davie
If this is indeed the desired effect, you're much better off writing a - (NSArray *)arrayByRandomlyOrderingObjects in an NSArray category, and then calling [[mySet allObjects] arrayByRandomlyOrderingObjects];. Bob On 25 May 2010, at 17:54, Dave DeLong wrote: > Ah, I see; you don't want to pro

Re: -[NSMutableSet randomObject]

2010-05-25 Thread Dave DeLong
Since an NSSet is, by definition, unordered, the allObjects method is not guaranteed to return the same ordering of objects every time. However, once you have the array, you could easily order it yourself using sortedArrayUsing(Selector/Comparator/Descriptors):. And I've used this code (or rea

Re: -[NSMutableSet randomObject]

2010-05-25 Thread John Joyce
Implementation should probably include various options for what kind of pseudo randomness is desired. On May 25, 2010, at 11:52 AM, Dave DeLong wrote: > What about something like: > > @implementation NSSet (Random) > > - (id) randomObject { > NSArray * allObjects = [self allObjects]; > if ([

Re: -[NSMutableSet randomObject]

2010-05-25 Thread Michael A. Crawford
Hey, not bad. Does the -allObjects method always return the same sequence? Did you just cobble this up or is this running in a system somewhere? It doesn't do everything I want but it is definitely the better part of the 80/20 rule. -Michael On May 25, 2010, at 12:52 PM, Dave DeLong wrote:

Re: -[NSMutableSet randomObject]

2010-05-25 Thread Dave DeLong
Ah, I see; you don't want to provide the same object twice in a row. If that's the case, is it really "random"? ;) Dave On May 25, 2010, at 10:52 AM, Dave DeLong wrote: > What about something like: > > @implementation NSSet (Random) > > - (id) randomObject { > NSArray * allObjects = [self

Re: -[NSMutableSet randomObject]

2010-05-25 Thread Dave DeLong
What about something like: @implementation NSSet (Random) - (id) randomObject { NSArray * allObjects = [self allObjects]; if ([allObjects count] == 0) { @throw ...; } return [allObjects objectAtIndex:(arc4random() % [allObjects count])]; } @end On May 25, 2010, at 10:49 AM, Michael A. Cra

-[NSMutableSet randomObject]

2010-05-25 Thread Michael A. Crawford
I'd like to extend Apple implementation of NSMutableSet to include a randomObject method. The purpose of this method is self-explanatory and its potential use is I'm sure obvious to most of you. For my first idea I thought about simply adding a category to NSMutableSet but any obvious implemen