The Null Device

Posts matching tags 'C'


Geekier-than-thou technology blog Ars Technica have posted a detailed technical review of Snow Leopard, the latest revision of MacOS X, which delivers few new features but instead comprehensively overhauls the inner workings of the system. And there are a lot of interesting things there, from transparent compression of files to the shift to 64-bit and the replacement of the legacy QuickTime system with a new, Objective C-based one, not to mention a judicious sprinkling of user-interface improvements and technologies brought over from the iPhone programme. (Core Animation, it seems, is everywhere, and there's a CoreLocation service which can determine where a machine is.)

One of the most intriguing improvements (to me, as a programmer, anyway) is one at the lowest level: Apple have quietly extended the C language, adding anonymous/lambda functions and closures, which they call "blocks". So now you can create and pass back blocks of code (more or less) as if you were in Lisp, Python or JavaScript, like so:

typedef void (^work_t)(void);
void repeat(int n, work_t block) { 
  for (int i = 0; i < n; ++i) 
repeat(5, ^{ printf("Hello world\n") });
Which, of course, opens the door to functional-style algorithms like map/filter/reduce, passing predicates as function arguments, and other nifty tricks which people in the functional-programming world have been doing without a second thought for decades.

The code in bold is a block. It's not the prettiest syntax in the world, though it is consistent with C, and gets lexical scope. There are more technical details on blocks here (fun fact: a block is an Objective C runtime object, though can be used from vanilla C), and Apple's own documentation here. Apple have made the blocks extention open source, contributing it back to both GCC and the LLVM compiler they're moving to, and submitting it to the C standards working group (as in this paper), so there's a decent chance that they'll filter through to other platforms. (How quickly they're adopted elsewhere is, of course, another matter.)

Blocks in themselves are nifty for the functional-programming enthusiasts, though understandably may seem esoteric to everybody else. Apple, however, are making thorough practical use of them in a new subsystem named Grand Central Dispatch, which allows programmers to rewrite processor-intensive processes in terms of fine-grained units of work, pass them to queues, and have them automatically spread across however many processors the machine has free at the time; which, in theory at least, should greatly increase efficiency without requiring much more effort on the programmer's part.

(via MeFi) C apple functional programming osx programming tech 0

This will be the comment popup.
Post a reply
Display name:

Your comment:

Please enter the text in the image above here: