• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Obj-C equivalent to Python generator functions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Obj-C equivalent to Python generator functions


  • Subject: Re: Obj-C equivalent to Python generator functions
  • From: Marcel Weiher <email@hidden>
  • Date: Sat, 4 Apr 2009 14:53:04 -0700


On Apr 1, 2009, at 19:04 , Sam Krishna wrote:

Does anyone here know of any Obj-C functional equivalent to Python generator functions? These are *categorically* different that the Spotlight API generator functions.
http://is.gd/qcYt

There is no direct equivalent at the language level, because Objective- C does not support coroutines (and even light-weight coroutines such as generators). However, generators can usually (always?) be transformed into methods of objects by storing the temporary variables in instance variables and thus saving temporary state across invocations.


If you want to get fancier, you could use one of the coroutine packages for C and apply it to Objective-C (google "coroutines in c" ).


Here's the code for a fibonacci generator created using the generator - > object covnersion technique described above:



#import <Foundation/Foundation.h>

@interface FibGen : NSEnumerator
{
	int current,previous;
}

@end

@implementation FibGen : NSEnumerator
-init
{
	if ( self=[super init]) {
		previous=0;
		current=1;
	}
	return self;
}

-nextObject
{
	id retval=[NSNumber numberWithInt:current];
	int next=previous+current;
	previous=current;
	current=next;
	return retval;
}

@end

int main( int argc, char *argv[] )
{
	id pool=[NSAutoreleasePool new];
	id fib=[FibGen new];
	for ( id  no in fib ) {
		NSLog(@"next: %@",no);
	}
	return 0;
}

marcel@spock[tmp]./generator
2009-04-04 14:49:30.118 generator[69984:10b] next: 1
2009-04-04 14:49:30.120 generator[69984:10b] next: 1
2009-04-04 14:49:30.120 generator[69984:10b] next: 2
2009-04-04 14:49:30.120 generator[69984:10b] next: 3
2009-04-04 14:49:30.121 generator[69984:10b] next: 5
2009-04-04 14:49:30.121 generator[69984:10b] next: 8
2009-04-04 14:49:30.121 generator[69984:10b] next: 13
2009-04-04 14:49:30.122 generator[69984:10b] next: 21
2009-04-04 14:49:30.122 generator[69984:10b] next: 34
2009-04-04 14:49:30.123 generator[69984:10b] next: 55
2009-04-04 14:49:30.123 generator[69984:10b] next: 89
2009-04-04 14:49:30.123 generator[69984:10b] next: 144
2009-04-04 14:49:30.123 generator[69984:10b] next: 233
2009-04-04 14:49:30.124 generator[69984:10b] next: 377
2009-04-04 14:49:30.124 generator[69984:10b] next: 610
2009-04-04 14:49:30.124 generator[69984:10b] next: 987
2009-04-04 14:49:30.125 generator[69984:10b] next: 1597
2009-04-04 14:49:30.125 generator[69984:10b] next: 2584
2009-04-04 14:49:30.125 generator[69984:10b] next: 4181
2009-04-04 14:49:30.125 generator[69984:10b] next: 6765
2009-04-04 14:49:30.126 generator[69984:10b] next: 10946
2009-04-04 14:49:30.126 generator[69984:10b] next: 17711
2009-04-04 14:49:30.126 generator[69984:10b] next: 28657
2009-04-04 14:49:30.127 generator[69984:10b] next: 46368
2009-04-04 14:49:30.127 generator[69984:10b] next: 75025
^C

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Obj-C equivalent to Python generator functions (From: Sam Krishna <email@hidden>)

  • Prev by Date: Re: memory deallocation
  • Next by Date: Re: Using bindings to set NSImageView from filename
  • Previous by thread: Re: Obj-C equivalent to Python generator functions
  • Next by thread: Seeking advice on best practice for managing visibility of many subviews
  • Index(es):
    • Date
    • Thread