mikeash.com: just this guy, you know?

Posted at 2009-10-23 15:45 | RSS feed (Full text feed) | Blog Index
Next article: Friday Q&A 2009-10-30: Generators in Objective-C
Previous article: Friday Q&A 2009-10-16: Creating a Blocks-Based Object System
Tags: fridayqna generators
Friday Q&A 2009-10-23: A Preview of Coming Attractions
by Mike Ash  

I'm afraid that I ran out of time this week and wasn't able to put together a real Friday Q&A. However, as a preview of what I'll be talking about next week, check out the MAGenerator project in my public subversion repository.

MAGenerator is a library for building generators in Objective-C. These are much like the generators found in Python, although with a slightly different interface. They are essentially functions which remember state between invocations, such that when you call them a second time, all local variables have the same value they did at the end of the first call, and execution resumes where it was when it returned from the first call.

There's complete, if terse, documentation found in the MAGenerator.h header, and some examples in the GeneratorTests.m file. I plan to release the code under an MIT license if you'd actually like to use it somewhere. Unlike my blocks-based object system from last week, I think this code could actually be used in a practical sense, even though the implementation is still pretty much evil.

Next week I'll go through how it all works, why it's built the way it is, and how to use it. Until then, enjoy the code. And as always, keep your suggestions for topics coming. I'm already booked for next week but am always happy to hang on to ideas for later.

Did you enjoy this article? I'm selling whole books full of them! Volumes II and III are now out! They're available as ePub, PDF, print, and on iBooks and Kindle. Click here for more information.

Comments:

This pattern doesn't seem to be used much, if at all, in the AppKit/Foundation libraries. Could you or anyone briefly explain what kinds of benefits this would give us high-level Mac developers?
It should come as no surprise that AppKit/Foundation doesn't use a pattern that wasn't even really possible until recently, and which MAGenerator is the first library to actually make it reasonable to build them.

Next week will be the full discussion of how it works and what they're good for. In the mean time, I suggest the following light reading:

http://en.wikipedia.org/wiki/Generator_(computer_science)
http://en.wikipedia.org/wiki/Coroutine
http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

And whatever discussion of generators in Python (a language that has had built-in support for them for a long time) you can turn up.

Comments RSS feed for this page

Add your thoughts, post a comment:

Spam and off-topic posts will be deleted without notice. Culprits may be publicly humiliated at my sole discretion.

Name:
The Answer to the Ultimate Question of Life, the Universe, and Everything?
Comment:
Formatting: <i> <b> <blockquote> <code>.
NOTE: Due to an increase in spam, URLs are forbidden! Please provide search terms or fragment your URLs so they don't look like URLs.
Hosted at DigitalOcean.