The Null Device

Posts matching tags 'functional programming'

2009/9/6

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) 
    block(); 
} 
 
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 [no comments] Share

2007/2/28

JavaScript 1.7, the version used in Firefox 2.0, has a raft of Python-inspired features, including generators and list comprehensions. So now, you can do things like:

function fib() {
  var i = 0, j = 1;
  while (true) {
    yield i;
    [i, j] = [j, i + j];
  }
}
and
var evens = [i for (i in range(0, 21)) if (i % 2 == 0)];
And, indeed, bulk assignments, like:
[a, b] = [b, a];
That is, as long as you're not concerned about your code working on non-Mozilla web browsers. (I wonder whether Microsoft, who still have well over 80% of the browser market, will adopt these new features.)

functional programming javascript python tech web [no comments] Share

2003/7/31

Python 2.3 is officially out, and brings with it lots of features. Generators are now a first-class part of the language (and not part of __future__), which allows a sort of lazy evaluation; Python can import modules from ZIP files; there is the enumerate() function, which allows you to iterate over a sequence's indices and values more efficiently, as well as Set and Boolean types; and there are a number of nifty new modules, such as a correct CSV handler, and more. Oh, and it's apparently 25% faster too.

functional programming programming python [2 comments] Share