<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>I have been a Mac and iPhone/iPad software developer for the past 3 years. I’m currently on leave.</description><title>tumblilarius lukhnos</title><generator>Tumblr (3.0; @lukhnos)</generator><link>http://blog.lukhnos.org/</link><item><title>"Currying" Objective-C Methods</title><description>&lt;p&gt;In this blog post, I&amp;#8217;ll show you a way to &amp;#8220;curry&amp;#8221; Objective-C methods. I&amp;#8217;ll first talk about the problem I&amp;#8217;m trying to solve, what currying is, and how currying can help solve the problem. Then I&amp;#8217;ll discuss how you can use blocks to achieve currying and the issue with that approach. Next, I&amp;#8217;ll present a library that can easily &amp;#8220;curry&amp;#8221; Objective-C methods of your choice. You have probably noticed I put the term in quotes, and I&amp;#8217;ll briefly talk about why the approach I present here is not the real currying in a strict sense. I&amp;#8217;ll also discuss some other issues in introducing the notion of currying in an imperative, object-oriented programming language like Objective-C.&lt;/p&gt;

&lt;p&gt;Please note that the library is written for study purposes, and I didn&amp;#8217;t consider its practicality or performance. Nor is it intended for production use.&lt;/p&gt;

&lt;p&gt;If you want to dive right into the code, here&amp;#8217;s &lt;a href="http://github.com/lukhnos/ObjCurry/"&gt;the source&lt;/a&gt;, along with some sample showing you how to use it.&lt;/p&gt;

&lt;h3&gt;Introduction&lt;/h3&gt;

&lt;p&gt;Let&amp;#8217;s say we have a method in SomeClass:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    - (void)doThis:(id)a withThat:(id)b andThat:(id)c;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we refer only to the method and want to emphasize its class membership, we can also write it as &lt;code&gt;-[SomeClass doThis:withThat:andThat:]&lt;/code&gt;, although that does not reveal the type information for the parameters and return value.&lt;/p&gt;

&lt;p&gt;Then one day you find that, in your project, you have quite a few places that look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    SomeClass *foo = [[SomeClass alloc] init];
    [foo doThis:a withThat:b andThat:c];
    [foo doThis:a withThat:b andThat:d];
    [foo doThis:a withThat:b andThat:e];
    [foo doThis:a withThat:b andThat:f];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The method takes three arguments, but we are always passing the same &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; here. Would it be nice if there&amp;#8217;s some way to remember those, so that we only need to pass the argument that actually changes, namely, &lt;code&gt;c&lt;/code&gt;, &lt;code&gt;d&lt;/code&gt;, &lt;code&gt;e&lt;/code&gt;, and &lt;code&gt;f&lt;/code&gt;, like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    [someMagicFoo blahAndThat:c];
    [someMagicFoo blahAndThat:d];
    [someMagicFoo blahAndThat:e];
    [someMagicFoo blahAndThat:f];        
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It turns out that there are quite a few possible solutions to this problem. Before I discuss them, let me first introduce a concept called currying.&lt;/p&gt;

&lt;h3&gt;Currying&lt;/h3&gt;

&lt;p&gt;Let&amp;#8217;s take the C function for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    int f(int x, int y)
    {
        return x + y;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then say you want to do these computations:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    int z;
    z = f(5, 10);
    z = f(5, 20);
    z = f(5, 30);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, we see that we&amp;#8217;re always passing &lt;code&gt;5&lt;/code&gt; for &lt;code&gt;x&lt;/code&gt;. One way to save that effort is to write another function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    int g(int y)
    {
        return f(5, y);
    }

    int z;
    z = g(10);
    z = g(20);
    z = g(30);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In other words, we want to &amp;#8220;fix&amp;#8221; the first argument of &lt;code&gt;f&lt;/code&gt; and we only want to pass the second argument to get the function value. This is called &amp;#8220;&lt;a href="http://en.wikipedia.org/wiki/Partial_application"&gt;partial application&lt;/a&gt;&amp;#8221;, as we only want to apply the function to a smaller set of arguments.&lt;/p&gt;

&lt;p&gt;Wouldn&amp;#8217;t it be nice if, instead of writing &lt;code&gt;g&lt;/code&gt; ourselves, the compiler can help us write that, using the hypothetical construction below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    int (*g)(int) = fix(f, 5);  // hypothetical construction
    int z;
    z = g(10);
    // ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The construction above is actually not hypothetical, although it&amp;#8217;s not available in the C language. The transformation that allows such construction is called &lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;currying&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In C, we declare the prototype of function &lt;code&gt;f&lt;/code&gt; as int &lt;code&gt;f(int, int)&lt;/code&gt;, meaning it&amp;#8217;s a function that has two integer parameters and returns an integer value. It can also be written formally as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    f: int × int -&amp;gt; int
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can also write it as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    f: (int, int) -&amp;gt; int
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which reads &amp;#8220;f is a function that maps a pair of integers to an integer&amp;#8221;. The two forms are equivalent, as &lt;em&gt;n&lt;/em&gt;-tuples (a pair is a 2-tuple) can be used to represent set cross products.&lt;/p&gt;

&lt;p&gt;Now let&amp;#8217;s talk about currying with a concrete example. We can &amp;#8220;curry a function&amp;#8221;, in the sense that we pass a function to a machinery called &lt;code&gt;curry&lt;/code&gt;, and it will give us a new function (think of it as someone who writes a new function for us).&lt;/p&gt;

&lt;p&gt;So, if we &lt;em&gt;curry&lt;/em&gt; &lt;code&gt;f&lt;/code&gt; (i.e. passing &lt;code&gt;f&lt;/code&gt; to &lt;code&gt;curry&lt;/code&gt;), then we get a new function with the following type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    curry f: int -&amp;gt; (int -&amp;gt; int)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That looks bizarre, so let me explain. The above line reads &amp;#8220;&lt;code&gt;curry f&lt;/code&gt; (the returned function of calling &lt;code&gt;curry&lt;/code&gt; with &lt;code&gt;f&lt;/code&gt;) is a function that, when passed an integer, returns a function that takes an integer and returns an integer&amp;#8221;.&lt;/p&gt;

&lt;p&gt;Sounds familiar?&lt;/p&gt;

&lt;p&gt;It should! Remember the C example we just mentioned. Let&amp;#8217;s suppose if the C language supports currying, and we can actually obtain a function from that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    // we need this bizarre type to declare h
    typedef ...SomeBizarreType...;

    SomeBizarreType h = curry(f);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, if we call &lt;code&gt;h(5)&lt;/code&gt;, it will give us the function &lt;code&gt;g&lt;/code&gt; that we want!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    int (*g)(int) = h(5);
    int z;
    z = g(10);
    z = g(20);
    z = g(30);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;curry&lt;/code&gt; is actually a function that takes a form like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    curry: ((a, b) -&amp;gt; c) -&amp;gt; (a -&amp;gt; (b -&amp;gt; c))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It can be a bit cumbersome to read out the definition above. Here&amp;#8217;s a way to see this: if you call &lt;code&gt;curry(f)&lt;/code&gt;, it returns function &lt;code&gt;h&lt;/code&gt; to you. Then if you call &lt;code&gt;h(5)&lt;/code&gt;, it returns our good friend &lt;code&gt;g&lt;/code&gt;. Here, &lt;code&gt;f&lt;/code&gt; is the function of type &lt;code&gt;(a, b) -&amp;gt; c&lt;/code&gt;, and &lt;code&gt;h&lt;/code&gt; is &lt;code&gt;(a -&amp;gt; (b -&amp;gt; c))&lt;/code&gt;, and &lt;code&gt;g&lt;/code&gt; is &lt;code&gt;(b -&amp;gt; c)&lt;/code&gt;. Those parentheses are added to disambiguate, but rest assured that there are many people who can do without them.&lt;/p&gt;

&lt;p&gt;This transformation technique is named after &lt;a href="http://en.wikipedia.org/wiki/Haskell_Curry"&gt;Haskell Curry&lt;/a&gt; (1900-1982), a mathematician (yes, the &lt;a href="http://www.haskell.org/haskellwiki/Haskell"&gt;language&lt;/a&gt; is named after him), who, among other things, discovered a fixed-point combinator called Y combinator (yes, &lt;a href="http://ycombinator.com/"&gt;that firm&lt;/a&gt; is named after it), &lt;a href="http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/"&gt;a construct which can express recursive functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also note that &lt;code&gt;curry&lt;/code&gt; is not confined to functions that have two parameters (i.e. have a pair of parameters):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    curry: ((t_0_, t_1, ..., t_{n-1}) -&amp;gt; t_n) -&amp;gt;
            (t_0 -&amp;gt; (t_1 -&amp;gt; ( ... -&amp;gt; (t_{n-1} -&amp;gt; t_n))))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And there&amp;#8217;s &lt;code&gt;uncurry&lt;/code&gt; that is the inverse of &lt;code&gt;curry&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    uncurry: (t_0 -&amp;gt; (t_1 -&amp;gt; ( ... -&amp;gt; (t_{n-1} -&amp;gt; t_n)))) -&amp;gt;
            ((t_0_, t_1, ..., t_{n-1}) -&amp;gt; t_n)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now that we know what currying is, let&amp;#8217;s see how it can help us find a solution to the problem we have in the beginning.&lt;/p&gt;

&lt;h3&gt;Using Objective-C Blocks for Currying&lt;/h3&gt;

&lt;p&gt;It turns out that there isn&amp;#8217;t really a way to write a curry function in C. The problem is that in C, functions are not first-class objects, meaning you can&amp;#8217;t manipulate functions like you do data structures. Unlike, say, in Haskell, where &lt;a href="http://www.haskell.org/haskellwiki/Currying"&gt;&lt;code&gt;curry&lt;/code&gt;&lt;/a&gt; is a function you can actually use.&lt;/p&gt;

&lt;p&gt;Then, there&amp;#8217;s a practical issue. Recall the example of &lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; above. If we always want to fix the &lt;code&gt;x&lt;/code&gt; parameter with a constant, then for each constant we need to write a new &lt;code&gt;g&lt;/code&gt;. What if we don&amp;#8217;t want to be confined to a constant?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    int g(int y)
    {
        // a is an argument we want to fix
        return f(a, y);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now the problem is that &lt;code&gt;g&lt;/code&gt; has to &amp;#8220;remember&amp;#8221; what &lt;code&gt;a&lt;/code&gt; is. You don&amp;#8217;t want to use global variable for that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    // suppose a is a global variable and a = 5 initially
    z = g(10);  // 15
    z = g(20);  // 20
    foo();      // a gets changed to 0
    z = g(10);  // ugh! it's 10 now
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the example above, we want to have a g that &amp;#8220;remembers&amp;#8221; to pass &lt;code&gt;5&lt;/code&gt; for &lt;code&gt;x&lt;/code&gt; to &lt;code&gt;f&lt;/code&gt;. Using a global variable ruins that.&lt;/p&gt;

&lt;p&gt;A construct to &amp;#8220;remember&amp;#8221; something happening in your program is called &lt;a href="http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/"&gt;closure&lt;/a&gt;. A closure &amp;#8220;remembers&amp;#8221; the local states when it&amp;#8217;s created.&lt;/p&gt;

&lt;p&gt;In Objective-C, blocks are closures. We can actually use blocks to write the &lt;code&gt;h&lt;/code&gt; function (&lt;a href="https://gist.github.com/1771842"&gt;compilable code here&lt;/a&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    typedef int (^int_to_int_t)(int);
    typedef int_to_int_t (^int_to_int_to_int_t)(int);

    int_to_int_to_int_t h = ^(int x) {
        int_to_int_t fx = ^(int y) {
            return f(x, y);  
        };

        return fx;
    };

    int_to_int_t g = h(5);

    int z;
    z = g(10);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, when the block &lt;code&gt;fx&lt;/code&gt; is created, it will &amp;#8220;remember&amp;#8221; the &lt;code&gt;x&lt;/code&gt; argument that&amp;#8217;s passed to &lt;code&gt;h&lt;/code&gt;. You also noticed that we need to use some &lt;code&gt;typedef&lt;/code&gt;s here.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m declaring &lt;code&gt;h&lt;/code&gt; as a local block because I don&amp;#8217;t want to dive into the &lt;a href="http://thirdcog.eu/pwcblocks/#objcblocks"&gt;block copying issue&lt;/a&gt; here, which is another complex matter that requires an entire blog post. Just remember that &lt;em&gt;you should return an autoreleased copy of the block if you want to pass it around&lt;/em&gt; (yes, I&amp;#8217;m essentially telling you that I&amp;#8217;m doing it &lt;em&gt;wrong&lt;/em&gt; here for the sake of brevity).&lt;/p&gt;

&lt;p&gt;In fact, we can go one step further, then we&amp;#8217;ll have something that&amp;#8217;s very close to the curry function, by passing the function we want to curry using a function pointer (&lt;a href="https://gist.github.com/1771845"&gt;compilable code here&lt;/a&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    // typedefs for int-&amp;gt;int, int-&amp;gt;(int-&amp;gt;int), etc.
    typedef int (^int_to_int_t)(int);
    typedef int_to_int_t (^int_to_int_to_int_t)(int);
    typedef int (*int_x_int_to_int_t)(int, int);
    typedef int_to_int_to_int_t (^curry_t)(int_x_int_to_int_t);

    curry_t curry = ^(int_x_int_to_int_t f) {
        int_to_int_to_int_t h = ^(int x) {
            int_to_int_t g = ^(int y) {
                return f(x, y);  
            };
            return g;        
        };
        return h;
    };

    int_to_int_to_int_t h = curry(f);
    int_to_int_t g = h(5);

    int z;
    z = g(10);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s a lot of declarations, not pretty, but it&amp;#8217;s something very close to the real thing.&lt;/p&gt;

&lt;h3&gt;What about Objective-C Methods?&lt;/h3&gt;

&lt;p&gt;Now that we know what currying is, and how Objective-C blocks can give us some kind of &amp;#8220;curried&amp;#8221; functions, let&amp;#8217;s see how they can help when it comes to Objective-C methods. Recall our first example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    SomeClass *foo = [[SomeClass alloc] init];
    [foo doThis:a withThat:b andThat:c];
    [foo doThis:a withThat:b andThat:d];
    [foo doThis:a withThat:b andThat:e];
    [foo doThis:a withThat:b andThat:f];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we want to fix &lt;code&gt;foo&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, and &lt;code&gt;b&lt;/code&gt;, we can actually write a simple block like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    void (^fooABAndThat)(id) = ^(id x) {
        [foo doThis:a withThat:b andThat:x];
    };

    fooABAndThat(c);
    fooABAndThat(d);
    // ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you want to have to flexibility to just fix &lt;code&gt;a&lt;/code&gt; then fix &lt;code&gt;b&lt;/code&gt; or some other arguments later, however, then you&amp;#8217;ll end up with something as messy as the previous &amp;#8220;curry&amp;#8221; function we have. Namely, if you want to do something like this (provided we always fix &lt;code&gt;foo&lt;/code&gt;, to make things simpler):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    ((fooDoThis(a))(b))(c);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you have to write something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    typedef void (^id_to_void)(id);
    typedef id_to_void (^id_to_id_to_void)(id);
    typedef id_to_id_to_void (^id_to_id_to_id_to_void)(id);
    id_to_id_to_id_to_void fooDoThis = ^(id x) {
        id_to_id_to_void withThat = ^(id y) {
            id_to_void andThat = ^(id z) {
                [foo doThis:x withThat:y andThat:z];
            };
            return andThat;
        };
        return withThat;
    };

    ((fooDoThis(a))(b))(c);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Apparently, that&amp;#8217;s not very fun to write.&lt;/p&gt;

&lt;p&gt;As I mentioned before, in C, functions are not first class objects. That you have to declare a type for everything doesn&amp;#8217;t help, either (hence the messy &lt;code&gt;typedef&lt;/code&gt;s above). Unless we have a fancy compiler or language extension that writes those declarations implicitly and automatically for us, this seems to be as good as it can get with blocks.&lt;/p&gt;

&lt;p&gt;But wait. Objective-C is a dynamic language. Its &lt;a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html"&gt;runtime API&lt;/a&gt; allows us to create classes and methods on the fly. Before the introduction of blocks, a lot of language have actually been built by making use of that property. Is there an easier way to &amp;#8220;curry&amp;#8221; an Objective-C method, so that we can do something like this?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    [someMagicFoo blahAndThat:c];
    [someMagicFoo blahAndThat:d];
    [someMagicFoo blahAndThat:e];
    [someMagicFoo blahAndThat:f];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With ObjCurry, we can do that.&lt;/p&gt;

&lt;h3&gt;Using Objective-C Runtime API to &amp;#8220;Curry&amp;#8221; Methods&lt;/h3&gt;

&lt;p&gt;ObjCurry is an Objective-C library that allows you to &amp;#8220;curry&amp;#8221; a wide range of Objective-C methods. Once included in a project, you can do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    [SomeClass curry:@selector(doThis:withThat:andThat:)];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, you can do things like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    id fooDoWithA = [foo doThis:a];
    id thenB1 = [foo withThat:b1];
    id thenB2 = [foo withThat:b2];

    // equivalent to [foo doThis:a withThat:b1 andThat:c]
    [thenB1 andThat:c];

    // equivalent to [foo doThis:a withThat:b1 andThat:c]
    [thenB2 andThat:c];

    id someMagicFoo = [[foo doThis:a] withThat:b];
    [someMagicFoo blahAndThat:c];
    [someMagicFoo blahAndThat:d];
    [someMagicFoo blahAndThat:e];
    [someMagicFoo blahAndThat:f];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And, of course, we can curry methods in a Foundation class. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    [[NSMutableDictionary class] curry:@selector(setObject:forKey:)];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, say, we have a dictionary, and we want to be able to set the same value to different keys:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    NSMutableDictionary *dict = ...;    
    id dictSetBarWithKey = [dict setObject:@"bar"];
    [dictSetBarWithKey forKey:@"a"];
    [dictSetBarWithKey forKey:@"b"];
    [dictSetBarWithKey forKey:@"c"];

    // dict is now {"a":"bar", "b":"bar", "c":"bar"}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Under the Hood&lt;/h3&gt;

&lt;p&gt;ObjCurry uses Objective-C runtime API to dynamically create new classes and methods. In the previous example, after you call:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    [SomeClass curry:@selector(doThis:withThat:andThat:)];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A new instance method &lt;code&gt;doThis:&lt;/code&gt; will be added to &lt;code&gt;SomeClass&lt;/code&gt;. Then, when you call:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    id fooDoWithA = [foo doThis:a];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The returned object, &lt;code&gt;fooDoWithA&lt;/code&gt;, is actually a proxy object whose class implements &lt;code&gt;withThat:&lt;/code&gt;. That method, in turn, will return another proxy object, whose class (currently the same as the previous proxy object&amp;#8217;s) implements &lt;code&gt;andThat:&lt;/code&gt;. The last method in the chain, &lt;code&gt;andThat:&lt;/code&gt;, will actually send the message &lt;code&gt;doThis:withThat:andThat:&lt;/code&gt; to foo, along with the captured three arguments.&lt;/p&gt;

&lt;p&gt;The implementation uses &lt;code&gt;NSInvocation&lt;/code&gt;, which is the working horse for many Objective-C techniques often collectively called &amp;#8220;higher-order messaging&amp;#8221;, to capture the arguments along the way. Proxy objects are also often used in tandem with &lt;code&gt;NSInvocation&lt;/code&gt;. One of the best example is &lt;code&gt;NSUndoManager&lt;/code&gt;, which can give you a proxy object for undoing. Any message you send (i.e. and method invocation) to that proxy object is recorded but not executed. The real invocation is deferred until the user chooses to undo, then the recorded invocation is &amp;#8220;played back&amp;#8221; (i.e. the real method on the target object is called).&lt;/p&gt;

&lt;p&gt;Because methods have to be typed, ObjCurry only supports a limited number of common types, such as &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, (un)signed &lt;code&gt;short&lt;/code&gt;/&lt;code&gt;int&lt;/code&gt;/&lt;code&gt;long&lt;/code&gt;/&lt;code&gt;long long&lt;/code&gt;, and generic pointer (&lt;code&gt;void *&lt;/code&gt;). For return types, there is one more supported: &lt;code&gt;void&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s possible to add more types — see &lt;code&gt;NSObject+Curry.m&lt;/code&gt; to see how to expand the type support. For example, you can add &lt;code&gt;CGRect&lt;/code&gt;/&lt;code&gt;NSRect&lt;/code&gt; support. The problem is that, unless deeper runtime API hack is found (perhaps some assembly required), for n argument types and m return types, we have to write &lt;em&gt;n&lt;/em&gt;m* definitions. Even with the current macro usage it still won&amp;#8217;t scale well. If
you have better solution, I&amp;#8217;d be very happy to hear from you.&lt;/p&gt;

&lt;h3&gt;Issues and Extensions&lt;/h3&gt;

&lt;p&gt;You probably noticed that when I say we want a way to &amp;#8220;curry&amp;#8221; Objective-C methods, I always put the term in quotes.&lt;/p&gt;

&lt;p&gt;The reason is that ObjCurry is not the real thing. To understand why, we have to first know how is message sending done in Objective-C.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s say we have an Objective-C instance method of class &lt;code&gt;Foo&lt;/code&gt; that adds two &lt;code&gt;NSNumber&lt;/code&gt; objects:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    - (NSNumber *)add:(NSNumber *)x to:(NSNumber *)y;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When we call:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    NSNumber *z = [foo add:x to:y];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The compiler actually turns that into a function call:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    z = objc_msgSend(foo, @selector(add:to:), x, y);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(The real code is of course optimized, but this is what the compiler would do from a high-level point of view.)&lt;/p&gt;

&lt;p&gt;And the method &lt;code&gt;-[Foo add:to:]&lt;/code&gt; is compiled as something like the following (again this is just a sketch). Remember that in Objective-C there is actually only one object type, hence the use of type &lt;code&gt;id&lt;/code&gt; all over:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    id Foo_add_to(id obj, SEL selector, id x, id y)
    {
        // ...
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;#8217;s ignore the selector parameter to simplify the matter. Obviously, a method is like a function, but it needs to take an object parameter so that it knows which object to act on. In other words, our &lt;code&gt;Foo_add_to&lt;/code&gt; function actually has the type:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    Foo_add_to: id × id × id -&amp;gt; id
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ideally, a curry function should give us something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    curry Foo_add_to: id -&amp;gt; (id -&amp;gt; (id -&amp;gt; id))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But because ObjCurry introduces proxy objects in the picture, what we actually have is more like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    "curried" Foo_add_to: (id × id) -&amp;gt; (id × id -&amp;gt; id)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s why ours is not a real thing in the strict sense.&lt;/p&gt;

&lt;p&gt;Another issue that comes with using ObjCurry is that Objective-C compilers give you warnings on previously undeclared methods. This is a nuisance, although there is a workaround. In the &lt;code&gt;main.m&lt;/code&gt; that demonstrates how the use ObjCurry, I put a few Objective-C categories that declares the methods as a result of currying. This silences the compiler warnings, but it can be a debugging hazard when you invoke a method that&amp;#8217;s not actually created, since the compiler now trusts you that you have an implementation somewhere, even if not statically available at compile or link time.&lt;/p&gt;

&lt;p&gt;In addition, there&amp;#8217;s the issue of mutability. Although proxies behave like immutable objects (as each time a curried method is called, a new proxy object is created), the target object (i.e. the original object on which the original method is invoked) is not. So while the proxies create an illusion of closures, you are still dealing with an imperative language. So if you intend to try ObjCurry in a multithreaded system, you have been warned.&lt;/p&gt;

&lt;p&gt;With the basics in place, it&amp;#8217;s possible to extend the fun. For example, each proxy object can actually remember the parameters that are ever passed to them and the returned objects. So if the parameter doesn&amp;#8217;t change, the proxy can always returned the &amp;#8220;memorized&amp;#8221; return value. This is known as &amp;#8220;&lt;a href="http://en.wikipedia.org/wiki/Memoization"&gt;memoization&lt;/a&gt;&amp;#8221; (some people say it&amp;#8217;s just a fancy term for &amp;#8220;caching&amp;#8221;). Again, in an imperative language like Objective-C, there are all kinds of issues that come with this.&lt;/p&gt;

&lt;p&gt;The use of proxy object also brings some overhead and will have some impact on performance in places such as a tight loop. &lt;code&gt;NSInvocation&lt;/code&gt; is slow compared to the straightforward message sending, considering the overhead in creating invocation objects and all the house keeping efforts.&lt;/p&gt;

&lt;p&gt;Finally, because this is not a feature supported by the compiler, debugging can be tricky if something happened inside the proxy object (especially in the last step that actually invokes the original method).&lt;/p&gt;

&lt;h3&gt;Related Works&lt;/h3&gt;

&lt;ul&gt;&lt;li&gt;A working paper written by Laurent Dami in 1991, &amp;#8220;&lt;a href="http://asg.unige.ch/site/papers/Dami91a.pdf"&gt;More Functional Reusability in C/C++/Objective-c [sic] with Curried Functions&lt;/a&gt;&amp;#8221; is an oft-cited work regarding currying in Objective-C.&lt;/li&gt;
&lt;li&gt;If you know Haskell, you probably have read the documentation on &lt;a href="http://www.haskell.org/haskellwiki/Currying"&gt;currying&lt;/a&gt; and partial application.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;Wikipedia&amp;#8217;s entry&lt;/a&gt; on currying also explains it in detail (and it also links to Dami&amp;#8217;s paper).&lt;/li&gt;
&lt;li&gt;Josh Weinberg has &lt;a href="https://github.com/jweinberg/Objective-Curry"&gt;an experimental implementation&lt;/a&gt; of currying and uncurrying that is written in Objective-C++ and uses templates.&lt;/li&gt;
&lt;li&gt;There are also some discussions on Stack Overflow (on &lt;a href="http://stackoverflow.com/questions/152005/how-can-currying-be-done-in-c"&gt;currying in C++&lt;/a&gt; and &lt;a href="http://stackoverflow.com/questions/1023261/is-there-a-way-to-do-currying-in-c"&gt;in C&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.cocoadev.com/index.pl?HigherOrderMessaging"&gt;CocoaDev Wiki&lt;/a&gt; has collected some of the higher-order messaging techniques in Objective-C.&lt;/li&gt;
&lt;li&gt;David Chisnall&amp;#8217;s &lt;a href="http://www.informit.com/articles/article.aspx?p=1353398"&gt;Advanced Flow Control for Objective-C&lt;/a&gt; is a nice summary of the &amp;#8220;higher-order messaging&amp;#8221; techniques and their applications. Among other things, it talks about the possibility of &lt;a href="http://www.informit.com/articles/article.aspx?p=1353398&amp;amp;seqNum=5"&gt;using proxy objects&lt;/a&gt; to achieve something like curried functions. It&amp;#8217;s more than the entries on CocoaDev Wiki recent since it also discusses blocks.&lt;/li&gt;
&lt;li&gt;Finally, Mike Ash discusses &lt;a href="http://www.mikeash.com/pyblog/friday-qa-2011-10-28-generic-block-proxying.html"&gt;block proxying&lt;/a&gt; and &lt;a href="http://www.mikeash.com/pyblog/friday-qa-2011-11-11-building-a-memoizing-block-proxy.html"&gt;building a memoizing block proxy&lt;/a&gt; using NSInvocation and blocks in great detail.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;The idea of currying is actually very straightforward, but implementing it with a non-functional programming language incurs many issues, some of which I have discussed above. We have seen how Objective-C blocks can be used to curry functions, and I&amp;#8217;ve also introduced the library &lt;a href="http://github.com/lukhnos/ObjCurry/"&gt;ObjCurry&lt;/a&gt; to &amp;#8220;curry&amp;#8221; Objective-C methods using the Objective-C runtime API. As I said in the beginning, the project is more of a study and does not consider performance issues. Still, we have in effect created a small language extension of our own. There are still other issues with the implementation, which I&amp;#8217;ve also briefly addressed. As Curry is an open source project, you can try it out yourself. And you know a way to generate automatically &lt;code&gt;IMP&lt;/code&gt;s for non-&lt;code&gt;id&lt;/code&gt;, non-primitive data types such as &lt;code&gt;NSRect&lt;/code&gt;/&lt;code&gt;CGRect&lt;/code&gt;, I&amp;#8217;d be more happy to hear from you.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/17270947434</link><guid>http://blog.lukhnos.org/post/17270947434</guid><pubDate>Wed, 08 Feb 2012 10:15:00 -0800</pubDate></item><item><title>Krzysztof Kieślowski on Crafting an Experience</title><description>&lt;p&gt;I recently re-watched the &lt;em&gt;Three Colors&lt;/em&gt; trilogy by the late Polish director Krzysztof Kieślowski (1941-1996). I first watched the three films when I was in college. I think I understood more and saw more in the films than I did when I was younger. Growing up, having traveled, and some knowledge of the history and one of the languages spoken in the film (French) all helped. But even back then I was deeply touched by the films the first time I watched them.&lt;/p&gt;

&lt;p&gt;I also appreciated more Kieślowski&amp;#8217;s mastery of the film language.&lt;/p&gt;

&lt;p&gt;The DVD edition of the trilogy comes with an extra feature called “Krzysztof Kieślowski&amp;#8217;s Cinema Lesson”, in which the director talked about the making of one significant scenes in each of the trilogy. In particular, he talked about the filming process and the editing choices involved. This is the &lt;em&gt;auteur&lt;/em&gt; dissecting his own work in details.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;Blue&lt;/em&gt;, for example, there&amp;#8217;s a famous “sugar cube scene”, starting at 52′20″ and lasting about five seconds. Julie, the heroine, is found sitting in a café by Olivier. Julie has recently lost her husband and daughter in a car accident, and she tries hard to leave everything in her life behind. Olivier, who has worked with her husband on a song commissioned by the European Union, becomes a lover and puts an effort in finding her. Julie turns him down in the café. After he leaves, she plays with the sugar cube that comes with her coffee, while a flute player on the street is playing a number that sounds very similar to one of the motives in the commissioned (but yet finished) song.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s Kieślowski talking about how he required the cube to be soaked in coffee in exactly five seconds, and the rationale behind it. Emphasis mine:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is a sugar cube about to fall in the cup of coffee. What does this obsession with close-ups mean? Simply that we&amp;#8217;re trying to show the heroine&amp;#8217;s world from her point of view, to show that she sees these little things, things that are near her, by focusing on them, in order to demonstrate that the rest doesn&amp;#8217;t matter to her.&lt;/p&gt;
  
  &lt;p&gt;It seems easy to film a sugar cube soaking up coffee, sucking it up and turning brown. […] We can start a stopwatch. It should take five and a half seconds or five seconds to be completely soaked. How to make sure that it only takes five seconds? Not so easy. Let&amp;#8217;s take a regular sugar cube, like this one, and soak it in my coffee. I&amp;#8217;ll start the stopwatch&amp;#8230; Eight seconds. That&amp;#8217;s three seconds too long. We had to prepare one that would be soaked in five seconds. &lt;strong&gt;We decided such a detail shouldn&amp;#8217;t last more than five seconds&lt;/strong&gt;. For half a day, my assistant tested all kinds of sugar cubes, […]&lt;/p&gt;
  
  &lt;p&gt;&lt;strong&gt;What do we care about a stupid sugar cube sucking up some stupid coffee?&lt;/strong&gt; Nothing… unless we are, for a moment, in our heroine&amp;#8217;s world. She dips a sugar cube in her coffee and focuses on it to reject the offer that the man who loves her just made her. […]&lt;/p&gt;
  
  &lt;p&gt;And when you ask me if I think about the viewer, about the viewer&amp;#8217;s point of view — I go back to this stupid sugar cube — I try always to keep this in mind. &lt;strong&gt;We don&amp;#8217;t do previews, test screenings. I believe an audience can take four and a half seconds of sugar soaking, but that eight and a half would be too much&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;</description><link>http://blog.lukhnos.org/post/15088979092</link><guid>http://blog.lukhnos.org/post/15088979092</guid><pubDate>Sat, 31 Dec 2011 09:59:00 -0800</pubDate></item><item><title>Cirque du Soleil and Radio Canada International</title><description>&lt;p&gt;In the mid-1990s I owned a shortwave radio. It was one of the top-tier models made by &lt;a href="http://en.wikipedia.org/wiki/Sangean"&gt;Sangean&lt;/a&gt;, although I forgot the model number. That was before Internet radio came along, and you could still write to shortwave radio stations, with an attached &lt;a href="http://en.wikipedia.org/wiki/International_reply_coupon"&gt;international reply coupon&lt;/a&gt;, to request for their schedule and publications. So I wrote to Radio Canada International (RCI) to ask for their schedule and a booklet on how to install shortwave antennae. Sangean&amp;#8217;s manual listed the addresses of the radio stations you could write to.&lt;/p&gt;

&lt;p&gt;Because the city is in a basin, shortwave reception in Taipei wasn&amp;#8217;t great, so an external antenna made sense. I bought one coupon from a stamp collecting store (that&amp;#8217;s the one and only time I did that in my life), sent the letter, and a few weeks later, I received the schedule (which got updated every 6 months — because the atmospheric condition varies by season) and a guide to install various kinds of antennae. My father helped me install a long-wire antenna, the simplest type, on the roof of our apartment building (he also told me how shortwave radio was a restricted item in the 1950s in Taiwan, as the government didn&amp;#8217;t want people to have too much access to the information of the outside world). We calculated the needed length and got the insulation right, dropped the connecting cable to my window, and connected to the radio&amp;#8217;s external antenna socket. In addition to RCI, we were also able to listen to BBC World Service, Voice of America, Deutsche Welle, Radio Japan (NHK) and a few others with different reception qualities, a great supplement (my father would have said alternative) to ICRT, Taipei&amp;#8217;s local English station.&lt;/p&gt;

&lt;p&gt;RCI&amp;#8217;s broadcast time in East Asia was short, 30 minutes in French and then 30 minutes in English, but the timing was perfect. The French broadcast started at 6 AM Taipei time, then followed by the English broadcast at 6:30. My Sangean radio had both a timer and tape recorder, so I let RCI wake me up every morning, then later on the bus I would play the English news and programs in my Aiwa cassette player. That was a great way to wake up and learn English.&lt;/p&gt;

&lt;p&gt;Before a shortwave broadcast begins, the station plays a short sequence to identify itself, called &amp;#8220;ident.&amp;#8221; The ident of BBC World Service, &amp;#8220;This is London calling,&amp;#8221; is among the best known (check out &lt;a href="http://www.youtube.com/watch?v=WuJ5_j4U6HQ"&gt;this YouTube video&lt;/a&gt;, the version I heard is between 1:01 and 1:30). RCI, unsurprisingly, used the first bar of &amp;#8220;O Canada,&amp;#8221; the national anthem, followed by the bilingual announcement &amp;#8220;Radio Canada International / Radio Canada International&amp;#8221; (yes, one of them was in French&amp;#8230;), and the sequence repeated itself two more times.&lt;/p&gt;

&lt;p&gt;For me, one mystery about RCI&amp;#8217;s broadcast, at least in East Asia, was that before the ident, they actually played three minutes of a song, in a language that was definitely neither French or English. I hummed the tune to many people, but no one I asked knew what it was. It was also strange, too, because not many shortwave stations did that, a full three minutes of a song before the ident.&lt;/p&gt;

&lt;p&gt;I usually erased the tape on which I recorded each morning&amp;#8217;s program, but one day I decided to save a tape for the opening song. A few years later I converted the tape into MP3, played the clip to a few more people, but I still hadn&amp;#8217;t got any answer. And then I forgot the whole thing.&lt;/p&gt;

&lt;p&gt;Just today, out of the blue, I realized I still kept the MP3 file, and now I could use services like Shazam to identify what the song was. And so I did. Turns out that the opening song was &amp;#8220;&lt;a href="http://itunes.apple.com/us/album/saltimbanco/id309895009"&gt;Kumbalawe&lt;/a&gt;,&amp;#8221; the opening song of a show by Cirque du Soleil. It totally made sense now, as they are Canadian (from Montréal). The song uses an invented language, though. Isn&amp;#8217;t it amazing, that with a technology like music identification, we can find out the name of a song whose melody you never forget, after so many years?&lt;/p&gt;

&lt;p&gt;On the other hand, I guess I could have written to RCI long ago to ask about the song. Still, it was a happy rediscovery of a song that made so many wonderful early mornings.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/12732034510</link><guid>http://blog.lukhnos.org/post/12732034510</guid><pubDate>Sun, 13 Nov 2011 01:52:00 -0800</pubDate></item><item><title>Permanently Convert WordPress Blogs to Static Pages</title><description>&lt;p&gt;Since I started using Tumblr to host my blogs, I no longer used WordPress. It&amp;#8217;s a great blog software, but even if you stopped writing blogs with it, you still need to run its software update from time to time for security. Because of that, plus other various personal reasons, I decided to convert all the WordPress blogs that I hosted on my own to static pages.&lt;/p&gt;

&lt;p&gt;After some research online, the best solution I found for this task is Ammon Shepherd&amp;#8217;s &lt;a href="https://github.com/mossiso/WP-Static"&gt;WPStatic&lt;/a&gt; utility. Shepherd wrote extensively about his project (&lt;a href="http://mossiso.com/code/make-wordpress-static"&gt;usage&lt;/a&gt;, &lt;a href="http://mossiso.com/2007/09/10/converting-wordpress-to-static-html.html"&gt;origin&lt;/a&gt;, &lt;a href="http://mossiso.com/2009/07/20/convert-wp-to-static-html-part-2.html"&gt;more details&lt;/a&gt;). The project has been around for a while, and the version hosted on github works with the latest WordPress. Here&amp;#8217;s a quick summary of what I do to &amp;#8220;staticize&amp;#8221; my blogs.&lt;/p&gt;

&lt;p&gt;First, you need to know what WPStatic does:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;It backs up your WordPress database (as a MySQL dump). It actually makes two backups: One before it makes the changes to your database, and one after.&lt;/li&gt;
&lt;li&gt;It changes your blog&amp;#8217;s permalink structure &lt;em&gt;and&lt;/em&gt; close all the comments (for both posts and pages)&lt;/li&gt;
&lt;li&gt;It runs &lt;code&gt;wget&lt;/code&gt; to fetch every static pages linkable from the top page&lt;/li&gt;
&lt;li&gt;It archives the MySQL dump, the static pages, and the files in &lt;code&gt;wp-content&lt;/code&gt; into a &lt;code&gt;.zip&lt;/code&gt; file&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The script is only one file, called &lt;code&gt;wpstatic&lt;/code&gt;. Just place the file in your blog root, then run &lt;code&gt;chmod +x wpstatic&lt;/code&gt; to make it executable, and then run &lt;code&gt;./wpstatic -h&lt;/code&gt; for help. Some of the above actions, such as permalink structure modification, can be toggled off.&lt;/p&gt;

&lt;p&gt;Then, here&amp;#8217;s what I did:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;&lt;p&gt;I ran some of the backups on Mac OS X and some on Linux. If you run the script on Linux, you need to change nothing. If you run the script on Mac OS X, you need to do a global replace to change every occurrence of &lt;code&gt;sed -r&lt;/code&gt; to &lt;code&gt;sed -E&lt;/code&gt; (that&amp;#8217;s because the &lt;code&gt;sed&lt;/code&gt; that comes with Mac OS X comes from BSD).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;./wpstatic&lt;/code&gt; but don&amp;#8217;t proceed with the &lt;code&gt;wget&lt;/code&gt; backup (the actual page fetching). I ran this step to let the script close all the comments for me.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now this step is important. By default, the script changes your blog&amp;#8217;s permalink strcuture to something like &lt;code&gt;&lt;a href="http://example.com/year/month/day/post-name"&gt;http://example.com/year/month/day/post-name&lt;/a&gt;&lt;/code&gt;. This didn&amp;#8217;t work for me since some of my blog posts contain CJK titles, and CJK filenames work badly (a tragedy compounded by both URL encoding and file systems). Plus I have been using the structure like &lt;code&gt;&lt;a href="http://example.com/archives/123"&gt;http://example.com/archives/123&lt;/a&gt;&lt;/code&gt; for many years, and Google searchs on my blogs point to URLs like that. So, after the script changed the permalink structure, I had to go back to the admin page again, first change the permalink back to &lt;code&gt;/archives/%post_id%&lt;/code&gt;, then I added a &lt;code&gt;/&lt;/code&gt; at the end. The final permalink structure became &lt;code&gt;/archives/%post_id%/&lt;/code&gt;, and this is great, because this actually caused the pages to be saved into something like &lt;code&gt;/archives/123/index.html&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modifiy &lt;code&gt;wp-includes/default-filters.php&lt;/code&gt; and comment out all actions that generate feed links. I turned off (commented out) these lines. You might want to remove more:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;add_action('wp_head','feed_links',2);
add_action('wp_head','feed_links_extra',3);
add_action('wp_head','rsd_link');
add_action('wp_head','wlwmanifest_link');
add_action('wp_head','rel_canonical');
add_action('wp_head','wp_shortlink_wp_head',10,0);
add_action('template_redirect','wp_shortlink_header',11,0);    
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Modify your theme. In particular, remove the Meta links in the sidebar, the feed links in the header and the footer, and anything (such as the comment feed link in the single post page) that may generate RSS feeds, trackbacks or pingbacks — if you&amp;#8217;re converting your blog into static pages, those data is probably going to be meaningless since you are not updating the pages anymore. Search engines will still love your static pages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the script again, this time with &lt;code&gt;wpstatic -p&lt;/code&gt;. You probably need to run a few times to make sure that it fetches no more than it should (i.e. no feeds, no trackback links, etc.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If your blog has database prefix &lt;code&gt;wp_&lt;/code&gt;, the generated static page directory will be called &lt;code&gt;wp-static&lt;/code&gt;. Inside the script has packed the MySQL dump and other static contents to &lt;code&gt;wp-content.zip&lt;/code&gt;. You want to back that file up immediately.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Backup your original blog directory for its scripts and settings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove the &lt;code&gt;wp-content.zip&lt;/code&gt; in the generated static folder. Move static folder out. Remove the WordPress folder and rename the static folder to your blog&amp;#8217;s directory name.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Your mileage may vary. And most of important of all, &lt;strong&gt;backup everything&lt;/strong&gt; before you perform any of the above conversion steps.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/12463734700</link><guid>http://blog.lukhnos.org/post/12463734700</guid><pubDate>Mon, 07 Nov 2011 01:49:00 -0800</pubDate></item><item><title>Tim Cook, CEO of Apple, at the company memorial of Steve Jobs:


  But there is one more thing he...</title><description>&lt;p&gt;Tim Cook, CEO of Apple, at the company memorial of Steve Jobs:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;But there is one more thing he leaves us. He leaves us with each other. Because without him, Apple would have died in the late-90s, and the vast majority of us would have never met.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The works of Apple have touched and will continue to touch many people&amp;#8217;s lives. They have certainly touched, and changed, mine. I gave up on programming when I decided to major in the humanities. Many years later, I realized that I still liked software and cared about it. In 2004 I wanted to pick up the skills again, but found the landscape wide and unwieldy. I didn&amp;#8217;t know where to start. In the summer of that year, I bought the first Mac that I truly owned — a PowerBook G4 — and found a starting point: an open source project on Traditional Chinese input methods that a couple of good friends and I started. Mac OS X always had a great UNIX foundation, and Mac was already the choice of many open source developers. Friends taught me important things I needed to catch up, like how C++ had evolved during the years I wasn&amp;#8217;t paying attention. At the same time I realized that the native app development on Mac OS X had its roots in NeXTSTEP. Years ago, I was once shown the amazing NeXT cubes at an acquaintance&amp;#8217;s lab when he was doing his grad study in Hsinchu, Taiwan, and I never dreamt that one day I&amp;#8217;d be able to program with the operating system&amp;#8217;s software frameworks. It was a joy, almost illuminating, to discover and learn that so many good things had never lost their way. With the PowerBook, I learned how to program once more, and I&amp;#8217;ve got to know many great people and made many good friends.&lt;/p&gt;

&lt;p&gt;I wrote to Steve Jobs once. That was when Snow Leopard first came out in the late summer of 2009, and there were some issues with the new Traditional Chinese fonts. Those issues made them unusable in formal settings. With friends&amp;#8217; help and encouragement, we compiled the screenshots, filed Radar, and I finally got to draft the email. I didn&amp;#8217;t expect him to read it, but the next day, someone at Apple replied, asking if I could send back the attachments that I &amp;#8220;emailed Mr. Jobs.&amp;#8221; And the issues with the fonts were eventually resolved. It&amp;#8217;s not the famous one-liner that people loved to post online, and it&amp;#8217;s easy for us to attribute everything that a company does to one person. Still, that is important: there are people at Apple that care.&lt;/p&gt;

&lt;p&gt;I appreciate that Apple shared the recording of its company event with the rest of the world. It was solemn but at times lighthearted, comforting and at the same time gracefully cheering. It gave me a way to say goodbye, even though I never met him.&lt;/p&gt;

&lt;p&gt;Nobody is an island, and what a great loss it was. Norah Jones&amp;#8217;s songs soothed, and Coldplay lifted us up. It is truly, in Apple&amp;#8217;s own way, a celebration of Steve&amp;#8217;s life.&lt;/p&gt;

&lt;p&gt;Goodbye, Steve.&lt;/p&gt;

&lt;p&gt;Thank you, Steve.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/11858909783</link><guid>http://blog.lukhnos.org/post/11858909783</guid><pubDate>Mon, 24 Oct 2011 02:14:00 -0700</pubDate></item><item><title>Flickr OAuth Support in ObjectiveFlickr</title><description>&lt;p&gt;I&amp;#8217;ve added Flickr OAuth support to ObjectiveFlickr over the weekend.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.flickr.com/blog/2011/06/21/flickr-now-supports-oauth-1-0a/"&gt;Flickr announced in June&lt;/a&gt; that they started requiring new apps to use the &lt;a href="http://www.flickr.com/services/api/auth.oauth.html"&gt;OAuth-based authentication process&lt;/a&gt;, and the previous authentication process was deprecated and will be eventually phased out in early 2012. I recommend developers who use ObjectiveFlickr check out the latest version to start the transition.&lt;/p&gt;

&lt;p&gt;ObjectiveFlickr is hosted on &lt;a href="https://github.com/lukhnos/objectiveflickr"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;What&amp;#8217;s Added to ObjectiveFlickr&lt;/h3&gt;

&lt;p&gt;The main class, &lt;code&gt;OFFlickrAPIContext&lt;/code&gt;, has two new methods:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;code&gt;-fetchOAuthRequestTokenWithCallbackURL:&lt;/code&gt; makes a request to Flickr for a request token&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-fetchOAuthAccessTokenWithRequestToken:verifier:&lt;/code&gt; makes a request to Flickr to exchange the user-authorized request token (see below) for an access token&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Two new delegate methods are added to the OFFlickrAPIRequestDelegate protocol, which you need to implement:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;code&gt;-flickrAPIRequest:didObtainOAuthRequestToken:secret:&lt;/code&gt; handles the result of request token fetch&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-flickrAPIRequest:didObtainOAuthAccessToken:secret: userFullName:userName:userName:userName:&lt;/code&gt; handles the result of access token fetch&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The class &lt;code&gt;OFFlickrAPIContext&lt;/code&gt; now also support properties &lt;code&gt;OAuthToken&lt;/code&gt; and &lt;code&gt;OAuthTokenSecret&lt;/code&gt;. It also has a new method &lt;code&gt;-userAuthorizationURLWithRequestToken:requestedPermission:&lt;/code&gt; that returns the URL for user authorization, which you need to open in a new browser window.&lt;/p&gt;

&lt;p&gt;A new sample app, OAuthTransitionMac, is also added to show how to use the new authentication API along with the old. It also shows you how to exchange the existing auth token (from the old API) for an OAuth-based token. OAuthTransitionMac uses ARC, so it also shows how to set up the project (since ObjectiveFlickr still uses manual retain/release).&lt;/p&gt;

&lt;p&gt;The iPhone app, SnapAndRun, has also been updated to show how to use OAuth in your iOS apps. Many people have had difficulties in linking the ObjectiveFlickr library externally, particularly on the iOS Simulator. So I simply put everything in the project. I also realized that SnapAndRun is unnecessarily complicated for its purpose &amp;#8212; this is one thing you may be able to help improve (see the section &amp;#8220;ObjectiveFlickr Needs Your Contribution&amp;#8221; below).&lt;/p&gt;

&lt;h3&gt;What You Need to Do&lt;/h3&gt;

&lt;ol&gt;&lt;li&gt;&lt;p&gt;Read &lt;a href="http://www.flickr.com/services/api/auth.oauth.html"&gt;Flickr&amp;#8217;s own document&lt;/a&gt; to get an overview of the new authentication process. A glimpse of the flow chart should suffice for knowing what needs to be done.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If your app doesn&amp;#8217;t have callback URL support, this is the time to add it. Refer to SnapAndRun (which has &lt;code&gt;snapnrun://&lt;/code&gt;) and OAuthTransitionMac (&lt;code&gt;oatransdemo://&lt;/code&gt;) for how to add URL support. In essence, you need to handle the incoming URL in your app delegate. This applies to both Mac and iOS apps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When your app requests the user to authenticate, and when the user agrees to proceed, make a call using &lt;code&gt;-fetchOAuthRequestTokenWithCallbackURL:&lt;/code&gt; to obtain a request token. Pass your app&amp;#8217;s callback URL (e.g. &lt;code&gt;oatransdemo://callback&lt;/code&gt;) to the method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement &lt;code&gt;-flickrAPIRequest:didObtainOAuthRequestToken:secret:&lt;/code&gt;. This delegate method is called after the request token is successfully obtained, and you need to set your Flickr context&amp;#8217;s &lt;code&gt;OAuth&lt;/code&gt; and &lt;code&gt;OAuthSecret&lt;/code&gt; properties to the returned token and secret. Setting those two properties is required so that the subsequent access token fetch request will be signed correctly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If any error occurs, the delegate method &lt;code&gt;-flickrAPIRequest:didFailWithError:&lt;/code&gt; will still be called. The &lt;code&gt;NSError&lt;/code&gt; object passed will have the error code &lt;code&gt;OFFlickrAPIRequestOAuthError&lt;/code&gt;, and the &lt;code&gt;userInfo&lt;/code&gt; dictionary will have a key-value pair under &lt;code&gt;OFFlickrAPIRequestOAuthErrorUserInfoKey&lt;/code&gt;, which is simply the error message that Flickr returns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the same delegate method, call &lt;code&gt;-userAuthorizationURLWithRequestToken:requestedPermission:&lt;/code&gt; to obtain the user authorization URL. The permission will be something like &lt;code&gt;OFFlickrWritePermission&lt;/code&gt; (see &lt;code&gt;ObjectiveFlickr.h&lt;/code&gt; for possible values). Then, open the URL in a separate browser. Use &lt;code&gt;-[UIApplication openURL:]&lt;/code&gt; on iOS, &lt;code&gt;-[NSWorkspace openURL:]&lt;/code&gt; on Mac.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the user authorized your app to use their data, your app will be activated again. Parse the URL&amp;#8217;s query with the &lt;code&gt;OFExtractOAuthCallback&lt;/code&gt; function supplied in &lt;code&gt;OFUtilities.h&lt;/code&gt;. You should obtain the authorized request token and verifier now.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Obtain the actual access token with &lt;code&gt;-fetchOAuthAccessTokenWithRequestToken:verifier&lt;/code&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement the delegate method &lt;code&gt;-flickrAPIRequest:didObtainOAuthAccessToken:secret: userFullName:userName:userName:userName:&lt;/code&gt;. This handles the access token fetch, and set your Flickr context&amp;#8217;s &lt;code&gt;OAuth&lt;/code&gt; and &lt;code&gt;OAuthSecret&lt;/code&gt; to the final access token/secret pair that is passed back to the delegate method. Again, any error occurred will be passed to the error handling delegate method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Done! You can start making API calls and upload photos with your existing code base.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;h3&gt;Implementation Details&lt;/h3&gt;

&lt;p&gt;Flickr&amp;#8217;s OAuth uses HMAC-SHA1 signing, which I decided to implement inside ObjectiveFlickr without depending on any other library. At this point our app projects probably have too many dependencies, and I want the changes to OF and to the way you use it to be minimal.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve moved the inline functions out of &lt;code&gt;OFUtilities.h&lt;/code&gt; to a new &lt;code&gt;OFUtilities.m&lt;/code&gt;. This makes projects that use ARC happier, as inline functions cannot make retain/release calls under ARC. A Base64 encoding routine written by Matt Gallagher is also embedded in &lt;code&gt;OFUtilities.m&lt;/code&gt; and is not exposed to the outside (i.e. the functions are declared &lt;code&gt;static&lt;/code&gt;). Again this is to minimize dependencies.&lt;/p&gt;

&lt;p&gt;To minimize implicit state changes, I decided to require you to set your Flickr context&amp;#8217;s &lt;code&gt;OAuthToken&lt;/code&gt; and &lt;code&gt;OAuthTokenSecret&lt;/code&gt; in your delegate methods. Having &lt;code&gt;OFFlickrRequest&lt;/code&gt; do it for you implicit seems to be a bad idea architecturally.&lt;/p&gt;

&lt;p&gt;The current version &amp;#8220;burns in&amp;#8221; Flickr&amp;#8217;s auth service endpoints in the code. It may be a good idea to make them properties in &lt;code&gt;OFFlickrContext&lt;/code&gt;. Flickr now also has HTTPS endpoints. I&amp;#8217;ll be glad to merge your pull requests if you have tried them out and made changes to the framework.&lt;/p&gt;

&lt;p&gt;The API calling routines in &lt;code&gt;OFFlickrRequest&lt;/code&gt; now has two code paths. If both the &lt;code&gt;OAuthToken&lt;/code&gt; and &lt;code&gt;OAuthTokenSecret&lt;/code&gt; properties are present (i.e. not &lt;code&gt;nil&lt;/code&gt;) in your &lt;code&gt;OFFlickrContext&lt;/code&gt; object, then the request object uses the OAuth-based signing to make the call. Otherwise it falls back to use the traditional authToken. If you&amp;#8217;re only interested in public data, there&amp;#8217;s no need for the authentication.&lt;/p&gt;

&lt;p&gt;The OAuth spec allows three different ways of passing the signature. To simplify, I didn&amp;#8217;t use the Authorization HTTP header, but chose to put the signature as part of the HTTP POST body or HTTP GET query.&lt;/p&gt;

&lt;h3&gt;ObjectiveFlickr Needs Your Contribution&lt;/h3&gt;

&lt;p&gt;Many projects use ObjectiveFlickr, and I need your help.&lt;/p&gt;

&lt;p&gt;Please let me know if ObjectiveFlickr works or breaks your project. I no longer work on projects that need to support OSes older than Mac OS X 10.6 or iOS 4.3. I tried to be careful, for example I don&amp;#8217;t use fast enumeration (lest it break OS X 10.4 support), but there may be places I use properties instead of old-school getters and setters in the library (not samples &amp;#8212; they now only target the latest Mac OS X/iOS). You can help expand the test coverage in this area.&lt;/p&gt;

&lt;p&gt;Eventually support for older OS or SDKs will and should be dropped. So much happened during the past two years, and more will come! It is important the shed some skin in order to catch up with those new developments in the Mac/iOS development landscape.&lt;/p&gt;

&lt;p&gt;OAuth requires the consumer of an API (your app) to sign their calls and it involves a lot of URI string escaping. Handling improperly and you might see strange errors when you set a fancy title to a photo. The chars that need to be escaped, in addition to the default charset, are defined in the &lt;code&gt;kEscapeChars&lt;/code&gt; constant in &lt;code&gt;ObjectiveFlickr.m&lt;/code&gt;. I tried a number of common cases, but again the test coverage is very small. This is another area that you may be able to help.&lt;/p&gt;

&lt;p&gt;ObjectiveFlickr was first developed in 2006 and was rewritten in early 2009 during the iOS 2.2 days. And it shows: the SnapAndRun iPhone sample app is unnecessarily complicated, and many conventions are out of date. A sample app written from ground up should be able to demo how to use the library more succinctly. The library&amp;#8217;s own Xcode project has also needed to be remade so that app projects can again use cross-project reference to ObjectiveFlickr.&lt;/p&gt;

&lt;p&gt;Then there&amp;#8217;s documentation and code comments. Or synchronous API calls (so that you can make calls in other threads easily) or even &lt;code&gt;NSOperation&lt;/code&gt;/block-based request methods. A lot of things can be and need to be done in a framework project like ObjectiveFlickr. Your contribution will benefit the Flickr and Mac/iOS development community a lot.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;It&amp;#8217;s highly recommended that you start migrating to use Flickr&amp;#8217;s OAuth-based authentication, and ObjectiveFlickr now has enough support for it. The work for the transition shouldn&amp;#8217;t be too much, as you only need to change your authentication process. Hopefully ObjectiveFlickr continues to serve your apps well. Best of luck with the transition!&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/11275346353</link><guid>http://blog.lukhnos.org/post/11275346353</guid><pubDate>Mon, 10 Oct 2011 08:52:00 -0700</pubDate></item><item><title>The Low-Hanging Fruits and Bumpy Ride into the Future</title><description>&lt;p&gt;I bought &lt;a href="http://marginalrevolution.com/"&gt;Tyler Cowen&lt;/a&gt;&amp;#8217;s book &amp;#8220;&lt;a href="http://www.amazon.com/Great-Stagnation-Low-Hanging-Eventually-ebook/dp/B004H0M8QS"&gt;The Great Stagnation&lt;/a&gt;&amp;#8221; and finished it the same evening. I enjoyed it much like I did from reading books like &amp;#8220;&lt;a href="http://www.amazon.com/Day-Empire-Hyperpowers-Global-Dominance/dp/0385512848"&gt;Day of Empire&lt;/a&gt;&amp;#8221;, in that they shed light on understanding the world we live in and where we are going from that. Cowen&amp;#8217;s book is entirely about the situation in the US, but much of his analysis should apply to many other places in the world. He also talked about the political implication of such situation, or the &amp;#8220;great stagnation&amp;#8221;, and his view on China&amp;#8217;s catching up, in &lt;a href="http://www.economist.com/blogs/freeexchange/2011/08/tyler-cowen-stagnation"&gt;an interview with The Economist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cowen&amp;#8217;s main point in the book is that we have exhausted what the technological advances of the past few decades have been able to bring us — hence the first chapter&amp;#8217;s name, &amp;#8220;The Low-Hanging Fruit We Ate.&amp;#8221; New advances are harder to come by, as evidenced by the diminished return in ever-increasing R&amp;amp;D spending. The situation is worsened by two additional factors: The US education system is inadequate, especially the K-12, and the hubris in the financial system, based on the assumption that previous growth trend would continue, finally led to all kinds of troubles that we are still seeing today.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m not so sure about Cowen&amp;#8217;s view on the Internet. He devotes a whole chapter on it arguing that, while the Internet has brought us many great stuff, it creates few jobs, and people may even spend less because of all the (mostly free) fun they get from the Internet. Certainly eBay (one of his low job creation examples) does not hire many people — not at a scale of the Big Cars — but what about the benefits of disintermediation, which reduces the friction in transaction and makes many sellers reachable to more potential buyers (indie music comes to mind). But Cowen only spends a page to say that Google and Facebook hire less than 30k people combined and doesn&amp;#8217;t give us numbers to argue that it&amp;#8217;s indeed &amp;#8220;not saving the revenue-generating sector of the economy.&amp;#8221; What&amp;#8217;s worrying about such argument is that, with politicians fixated on job creation (and that&amp;#8217;s certainly not confined to the US), when they start pointing fingers at the tech industry that they aren&amp;#8217;t creating many jobs, we may start to wonder where this may lead us.&lt;/p&gt;

&lt;p&gt;One interesting, if also somber, implication of &amp;#8220;the great stagnation&amp;#8221; is political polarization, which Cowen doesn&amp;#8217;t mention in his book but talked about in the aforementioned interview. This seems a good perspective to understand the political dramas (sometimes ludicrously baffling to foreigners) in the US.&lt;/p&gt;

&lt;p&gt;Cowen prescribes bitter remedies: &amp;#8220;Raise the social status of scientists&amp;#8221; — and this is not easy as it sounds. He warns us that the recession will be long, although Japan shows the US how to cope with the decline as a society to a large extent. There&amp;#8217;s a twist to the theme here, because Cowen also warns us to be ready when more low-hanging fruit comes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[…] because sometimes low-hanging fruit is dangerous. The last time the world had a major dose of low-hanging fruit, a few countries didn&amp;#8217;t handle it very well, including the Axis powers, the Soviet Union, and Communist China, among others.&lt;/p&gt;
  
  &lt;p&gt;Without the new technologies of the time, the totalitarian mistakes of the twentieth century would not have been possible. […] The record-keeping techniques of mass bureaucracy were used to control and often kill other human beings en masse. Only after bitter experience did fascist ideas become less popular, and social and political norms subsequently evolved to protect electorates against the fascist temptation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So expect a bumpy ride to the future. I think that&amp;#8217;s a good advice to everyone who&amp;#8217;s lived the optimistic 1990s. The ending words are worth quoting in full:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In the meantime, we need to be prepared for a recession that could last longer than we are used to. We need to be prepared for the possibility that the growth slowdown could continue once the immediate recession passes. Part of science is coming to terms with its limits. The rate of scientific progress will continue to be uneven, sometimes grossly so. Yet reason and science have never been more important: If nothing else, a more reasonable and more scientific understanding of our predicament can help us cope, both intellectually and emotionally.&lt;/p&gt;
  
  &lt;p&gt;Back to the hard problems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Indeed.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/10880164700</link><guid>http://blog.lukhnos.org/post/10880164700</guid><pubDate>Fri, 30 Sep 2011 23:24:00 -0700</pubDate></item><item><title>Using OS X's Built-in ICU Library in Your Own Project</title><description>&lt;p&gt;Unicode string is usually not difficult to handle&lt;sup id="fnref:p6441462604-1"&gt;&lt;a href="#fn:p6441462604-1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt;. The tricky part comes when you need to have knowledge of Unicode on things like:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Turning strings like &amp;#8220;Café&amp;#8221; to upper case or lower case (the &lt;code&gt;toupper()&lt;/code&gt; or &lt;code&gt;tolower()&lt;/code&gt; in Standard C Library won&amp;#8217;t work)&lt;/li&gt;
&lt;li&gt;Stripping all punctuation symbols. This is not as easy as it seems. How do you also strip various European quotation marks as well as full-width ones used in East Asian text?&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Some languages, like Java, already have such Unicode knowledge &lt;a href="http://download.oracle.com/javase/6/docs/api/java/lang/Character.html"&gt;baked in&lt;/a&gt;. For C and C++, the standard library to use is IBM&amp;#8217;s &lt;a href="http://site.icu-project.org/"&gt;ICU&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can either build ICU from its source code, or use tools like &lt;a href="http://www.macports.org/"&gt;MacPorts&lt;/a&gt; to install it for you. But you may also wonder: Since OS X has great internationalization support, perhaps OS X also uses ICU?&lt;/p&gt;

&lt;p&gt;It turns out that OS X does include a version of pre-built ICU library&lt;sup id="fnref:p6441462604-2"&gt;&lt;a href="#fn:p6441462604-2" rel="footnote"&gt;2&lt;/a&gt;&lt;/sup&gt; placed in &lt;code&gt;/usr/library/libicucore.dylib&lt;/code&gt;, but for &lt;a href="http://lists.apple.com/archives/xcode-users/2005/jun/msg00633.html"&gt;various reasons&lt;/a&gt; — and I also assume it has much to do with the fact that ICU is written in C++, and library versioning with C++ is a pain — it does not include the headers for you to use.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s how you can link against the built-in library:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Install a copy of the latest ICU&lt;/li&gt;
&lt;li&gt;In your project file, set header path to include the installed ICU header (e.g. &lt;code&gt;/opt/local/include&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Define &lt;code&gt;U_DISABLE_RENAMING&lt;/code&gt; in your project (i.e. setting the compiler flag &lt;code&gt;-DU_DISABLE_RENAMING=1&lt;/code&gt;)— this turns off ICU&amp;#8217;s &lt;a href="http://userguide.icu-project.org/design"&gt;version renaming scheme&lt;/a&gt; so that you&amp;#8217;ll be able to link against an older library (the one that comes with OS X in this case) with the latest header&lt;/li&gt;
&lt;li&gt;Link against &lt;code&gt;libicucore&lt;/code&gt; (with the linker flag &lt;code&gt;-licucore&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;As the name implies, it only contains the &lt;em&gt;core&lt;/em&gt; ICU library, so it lacks quite a few things. For example, C++ methods such as &lt;code&gt;UnicodeString::toUTF8&lt;/code&gt;, classes as &lt;code&gt;StringPiece&lt;/code&gt;, &lt;code&gt;StringByteSink&lt;/code&gt; are missing. A good way to check availability is to use the UNIX tool &lt;code&gt;nm&lt;/code&gt; to dump &lt;code&gt;libicucore.dylib&lt;/code&gt;. Your best bet is to stick to the C API. On the other hand, this saves the trouble of including your own copy of ICU.&lt;/p&gt;

&lt;div class="footnotes"&gt;
&lt;hr&gt;&lt;ol&gt;&lt;li id="fn:p6441462604-1"&gt;
&lt;p&gt;Read Joel Spolsky&amp;#8217;s &lt;a href="http://www.joelonsoftware.com/articles/Unicode.html"&gt;excellent primer on Unicode&lt;/a&gt; if it still isn&amp;#8217;t to you. &lt;a href="#fnref:p6441462604-1" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn:p6441462604-2"&gt;
&lt;p&gt;Many of NS/CFString and NSCharacterSet&amp;#8217;s internationalization features use ICU under the hood. I haven&amp;#8217;t tested it, but I believe the tips provided here can also be used on iOS. &lt;a href="#fnref:p6441462604-2" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;&lt;/div&gt;</description><link>http://blog.lukhnos.org/post/6441462604</link><guid>http://blog.lukhnos.org/post/6441462604</guid><pubDate>Sat, 11 Jun 2011 20:37:00 -0700</pubDate></item><item><title>Brad Cox's "Object-Oriented Programming" (1986)</title><description>&lt;p&gt;A few weeks ago I had a chance to re-read this particular book by Brad Cox&lt;sup id="fnref:p4400884121-1"&gt;&lt;a href="#fn:p4400884121-1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt;. It is also the first book that introduced the Objective-C language to the world.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s not a heavy book — at 274 pages including index, Cox has outlined his solution for the software reusability problem in a succinct language (yes, pun intended). The book spends the first two chapters on why reusability is essential to mitigate the complexity in building a large system, what Cox calls &amp;#8220;software crisis&amp;#8221;, and on how the object-oriented programming paradigm, which Cox describes using the analogy of integrated circuits (&amp;#8220;Software ICs&amp;#8221;), is in turn the way to achieve reusability.&lt;/p&gt;

&lt;p&gt;In the following chapters, Cox describes the language that he designed to fulfill his vision of Software IC: Objective-C. A short introduction and evaluation of Smalltalk-80 is given, and comparisons with Ada and then C++ are also provided. Cox likes many aspects of Smalltalk-80: The language is designed for personal computing, with interaction and graphics in mind (remember this is the language that gives the birth to modern graphical user interface), and it has many advanced features in its time: virtual machines with persistent stores (so say &amp;#8220;saving files&amp;#8221; is an unnecessary feature), a language built on blocks (think lambda) and message passing mechanisms, an IDE, and so on. As for why Smalltalk-80 didn&amp;#8217;t become mainstream, Cox&amp;#8217;s explanation is that Smalltalk-80 is a language designed for research purposes. To be of practical value, a hybrid language that can work with existing infrastructure will have a better chance:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;My own interest in hybrid object-oriented language developed from the dream that organizational computing and personal computing could meet each other halfway, via a bridge over which arbitrary data could flow with minimum programming intervention. The bridge is created by providing the organizational computer with its own object-oriented programming language so that data can be put into object form. Object passivation [persistence] machinery [&amp;#8230;] can automatically convert this data into a form that Smalltalk can read and vice versa, so that much of the information transfer could be done automatically. This, and C&amp;#8217;s ability to access databases maintained on older languages like FORTRAN [Cox also mentions COBOL in previous paragraph], creates a bridge between data locked in the mainframe and the user by relying on the ability of Smalltalk-80 to build user interfaces rapidly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For the rest of the book, Cox describes the features of Objective-C in detail with an example of using the language to build a visual graph manipulation tool. Some syntactical features are no longer found in today&amp;#8217;s Objective-C (e.g. the &lt;a href="https://gist.github.com/870330"&gt;&lt;code&gt;= ClassName&lt;/code&gt;&lt;/a&gt; declaration, and the feature that class declaration and definition are one unified compilation unit, much like Java, instead of separate parts). In the first of the last two chapters, Cox outlines the architecture of an &amp;#8220;iconic user interface&amp;#8221; — the model-view-controller paradigm (MVC), which was also introduced by Smalltalk. The last chapter briefly discusses the runtime environment (such as garbage collection) and possibilities for other higher-level language features, including &amp;#8220;virtual object memories&amp;#8221;&lt;sup id="fnref:p4400884121-2"&gt;&lt;a href="#fn:p4400884121-2" rel="footnote"&gt;2&lt;/a&gt;&lt;/sup&gt;, concurrency (using coroutines), distributed systems and coordination systems&lt;sup id="fnref:p4400884121-3"&gt;&lt;a href="#fn:p4400884121-3" rel="footnote"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Overall, I find the book full of insights that still resonate, 25 years after its publication. It&amp;#8217;s interesting to take a historical perspective on how much progress we have made since then. Objective-C had never been as popular as C++, a &amp;#8220;competitor&amp;#8221; in some senses, or as Java, some features of which are actually influenced by Objective-C (such as interfaces, or what Objective-C calls protocols). NeXTSTEP, the first platform that heavily used Objective-C as the main language, was never mainstream. It takes Mac OS X, and then iOS, to propel it to &lt;a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html"&gt;one of the most popular programming languages&lt;/a&gt; worldwide. And many ideas are still waiting for more refined realizations. Some, like garbage collection, finally become an option on desktop platform. Some, like concurrency, are still things that frustrate programmers, expert or novice alike. Perhaps, like what &lt;a href="http://existentialtype.wordpress.com/2011/03/15/teaching-fp-to-freshmen/"&gt;some who teach functional programming would like to show&lt;/a&gt;, we are past the apex of the object-oriented model, and a new paradigm shift awaits.&lt;/p&gt;

&lt;div class="footnotes"&gt;
&lt;hr&gt;&lt;ol&gt;&lt;li id="fn:p4400884121-1"&gt;
&lt;p&gt;Cox, Brad J. &lt;em&gt;Object-Oriented Programming: An Evolutionary Approach&lt;/em&gt;. Addison-Wesley, 1986. I&amp;#8217;m referring to the first edition here. If you know the differences between its &lt;a href="http://www.amazon.com/Object-oriented-Programming-Brad-Cox/dp/0201103931"&gt;first&lt;/a&gt; and &lt;a href="http://www.amazon.com/Object-Oriented-Programming-Evolutionary-Brad-Cox/dp/0201548348"&gt;second&lt;/a&gt; editions, I&amp;#8217;d appreciate it if you can write something about it. &lt;a href="#fnref:p4400884121-1" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn:p4400884121-2"&gt;
&lt;p&gt;We can identify some of Cox&amp;#8217;s description of such a &amp;#8220;capability-based&amp;#8221; addressing system in today&amp;#8217;s uniform resource identifiers (URI). &lt;a href="#fnref:p4400884121-2" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn:p4400884121-3"&gt;
&lt;p&gt;Although gcc has been supporting gc-capable Objective-C for years, it was not possible to use it in Mac applications due to lack of framework support. Apple eventually came up with its own &amp;#8220;mix of curry&amp;#8221; of concurrent, generational &lt;a href="http://www.friday.com/bbum/2008/11/11/autozone-the-objective-c-garbage-collector/"&gt;garbage collector&lt;/a&gt;, with runtime and full framework support, in 2006. Objective-C also has a feature called Distributed Objects, which is often used among Mac applications for inter-process communications. Library support for operation-based concurrency became part of the Mac OS X 10.5 framework, again since 2006. &lt;a href="#fnref:p4400884121-3" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;&lt;/div&gt;</description><link>http://blog.lukhnos.org/post/4400884121</link><guid>http://blog.lukhnos.org/post/4400884121</guid><pubDate>Wed, 06 Apr 2011 16:10:00 -0700</pubDate></item><item><title>BugzKit, a FogBugz API Library for Objective-C</title><description>&lt;p&gt;BugzKit, a FogBugz API Library for Objective-C&lt;/p&gt;

&lt;p&gt;BugzKit is a &lt;a href="http://www.fogcreek.com/fogbugz/"&gt;FogBugz&lt;/a&gt; API library for Objective-C. I started developing BugzKit in mid-2009 at &lt;a href="http://lithoglyph.com/"&gt;Lithoglyph&lt;/a&gt; as the foundation for &lt;a href="http://lithoglyph.com/ladybugz/"&gt;LadyBugz&lt;/a&gt;, a FogBugz client for the Mac. Since the company has &lt;a href="http://blog.lithoglyph.com/post/3580343508/ladybugz-becomes-a-free-app"&gt;discontinued the development of the app&lt;/a&gt;, I think it&amp;#8217;s time to release the library to the public as an open source project. I hope that by opening the library, it can help others develop new FogBugz clients, such as ones for the iOS platform.&lt;/p&gt;

&lt;p&gt;The library is now available on &lt;a href="https://github.com/lukhnos/BugzKit"&gt;GitHub&lt;/a&gt; and is released under the MIT License.&lt;/p&gt;

&lt;p&gt;One important issue in designing an API library for FogBugz is data dependency. The case (ticket) data you fetch from the server is meaningless without the lists of projects, people, milestones, and so on. Therefore you want to fetch those lists before you can fetch the cases of a particular project or filter. Also, you want to fetch lists as well as cases in parallel, as there are many such lists and fetching them serially takes too long.&lt;/p&gt;

&lt;p&gt;For example, in the &lt;a href="https://github.com/lukhnos/BugzKit"&gt;BasicRequests sample app&lt;/a&gt; that comes with the library, we want to perform the following tasks:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Check the API version and availability&lt;/li&gt;
&lt;li&gt;Login (FogBugz uses the term &amp;#8220;log on&amp;#8221;)&lt;/li&gt;
&lt;li&gt;Fetch three lists (projects, people, milestones) in parallel&lt;/li&gt;
&lt;li&gt;If all succeed, logout (&amp;#8220;log off&amp;#8221;)&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;You may notice that logout should be performed after Step 3. even if any task in that step fails: Since we have an auth token, we should logout. For the sake for simplicity, let&amp;#8217;s stick to what the sample code implements.&lt;/p&gt;

&lt;p&gt;Apparently, each step depends on its previous step. If there&amp;#8217;s no API available (such as the service endpoint is down, or the URL is incorrect, etc.), you can&amp;#8217;t login. If you don&amp;#8217;t have the auth token obtained from the login, you can&amp;#8217;t fetch the lists. If any of the steps fails, the program should cancel all other operations standing in line and exit. If we draw a dependency graph, it will look like this:&lt;/p&gt;

&lt;div style="text-align:center"&gt;
&lt;a href="http://www.flickr.com/photos/lukhnos/5491178323/"&gt;&lt;img src="http://farm6.static.flickr.com/5213/5491178323_1b9f1d74e7_z.jpg"/&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;BugzKit solves the problem by separating &lt;em&gt;request objects&lt;/em&gt; and &lt;em&gt;request operations&lt;/em&gt;. Request objects encapsulates information required to make a request (URL, parameters, HTTP method name, etc.) whereas request operations are the actual &lt;em&gt;drivers&lt;/em&gt; that perform the HTTP request. Once we have the operation layer, we can then make use of the &lt;a href="http://developer.apple.com/cocoa/managingconcurrency.html"&gt;operation queue framework&lt;/a&gt; in Foundation (&lt;code&gt;NSOperation&lt;/code&gt; and &lt;code&gt;NSOperationQueue&lt;/code&gt; are the two main classes). NSOperation has the better granularity (compared with threads) for handling operation dependency, cancellation, and convergence (e.g. two or more finished operations meeting each other). Extending the sample code to anything that works in the real world will take a lot more work, but I believe the library makes it at least manageable.&lt;/p&gt;

&lt;p&gt;The API library covers most of the functions required to fetch lists and manipulate cases. It also handles attachments for you by automatically creating the temp files on demand and preparing an input stream so that you can send the byte stream as the POST request body (with the multipart MIME type). Many areas are not covered, though, such as wiki or time tracking. It should be easy to extend the library to support those FogBugz features.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/3599898899</link><guid>http://blog.lukhnos.org/post/3599898899</guid><pubDate>Wed, 02 Mar 2011 05:03:49 -0800</pubDate></item><item><title>The Hybrid Drive Upgrade</title><description>&lt;p&gt;I replaced the old hard disk drive in my laptop as it showed signs of dying two weeks ago. There is a lot of talk about upgrading to SSD among the people I follow on Twitter. SSD is great, no doubt, but it&amp;#8217;s still an expensive option.&lt;/p&gt;

&lt;p&gt;Then I came upon this: a &lt;a href="http://www.seagate.com/www/en-us/products/laptops/laptop-hdd"&gt;hybrid drive&lt;/a&gt;. It&amp;#8217;s actually a traditional, spinning hard disk drive with 4&amp;#160;GB flash memory used as read cache.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m never a benchmarking person when it comes to hardware. I do have read &lt;a href="http://www.anandtech.com/show/3734/seagates-momentus-xt-review-finally-a-good-hybrid-hdd"&gt;the AnandTech review&lt;/a&gt; after having installed and used the new drive for a few days, and my experience agrees with the review&amp;#8217;s findings. In some areas like boot-up speed, this drive does provide SSD-like performance. In other areas it&amp;#8217;s just a fast spinning drive.&lt;/p&gt;

&lt;p&gt;The unpleasant, unresponsive period after login is a thing of the past, and app launching is faster too.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s true that many Mac users seldom restart their machines, so a big selling point of this kind of hybrid drive, &amp;#8220;it speeds up booting up&amp;#8221;, seems moot. It&amp;#8217;s true that the hybrid drive is more expensive than its traditional spinning cousins, at about 2x the price, but an SSD of the same capacity will cost about 6x-10x. So for a bit more money you get big speed improvement in one area and small improvements in other areas with this hybrid drive.&lt;/p&gt;

&lt;p&gt;This drive does give me a small pleasant surprise: Improved virtual machine performance. For work reasons I usually keep 4-5 virtual machine images around, and I often have to fire up and then suspend some of them (I use VMware Fusion). There must be some complex interactions between the VM&amp;#8217;s and the host&amp;#8217;s file systems, and this hybrid drive seems to be of great help. Not only do my VMs start faster, but the long latency after resuming is also gone.&lt;/p&gt;

&lt;p&gt;Improved virtual machine gives me a great incentive to virtualize more. Now I can compartmentalize many more things, like trying new apps, in separate VMs. In theory I could have already been doing this all along, but the slow start-up and resume time has put me off, and I seldom used VM other than for work. But this enables a whole new usage for me.&lt;/p&gt;

&lt;p&gt;Overall I&amp;#8217;m satisfied with the upgrade. I didn&amp;#8217;t spend too much, and the marginal return is high. Those SSD upgrade tweets made me crave them but I was also wary of their pocket-burning tendencies. Now I won&amp;#8217;t have to worry about them for at least quite a while.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/3262057314</link><guid>http://blog.lukhnos.org/post/3262057314</guid><pubDate>Sat, 12 Feb 2011 17:16:00 -0800</pubDate></item><item><title>"The storytelling that thrives for a long time in the milieu of work — the
rural, the maritime, and..."</title><description>“&lt;p&gt;The storytelling that thrives for a long time in the milieu of work — the
rural, the maritime, and the urban — is itself an artisan form of
communication, as it were. It does not aim to convey the pure essence of the
thing, like information or a report. It sinks the thing into the life of the
storyteller, in order to bring it out of him again. Thus traces of the
storyteller cling to the story the way the handprints of the potter cling to
the clay vessel. Storytellers tend to begin their story with a presentation
of the circumstances in which they themselves have learned what is to follow,
unless they simply pass it off as their own experience.&lt;/p&gt;

&lt;p&gt;…&lt;/p&gt;

&lt;p&gt;Seen in this way, the storyteller joins the ranks of the teachers and sages.
He has counsel — not for a few situations, as the proverb does, but for many,
like the sage. For it is granted to him to reach back to a whole lifetime
(a life, incidentally, that comprises not only his own experience but no
little of the experience of others; what the storyteller knows from hearsay
is added to his own). His gift is the ability to relate his life; his
distinction, to be able to tell his entire life. The storyteller: he is the
man who could let the wick of his life be consumed completely by the gentle
flame of his story. This is the basis of the imcomparable aura about the
storyteller […]. The storyteller is the figure in which the righteous man
encounters himself.&lt;/p&gt;”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Walter Benjamin, “The Storyteller” (1936). English translation from the book “Illuminations”, New York, NY: Schocken, 1969.&lt;/em&gt;</description><link>http://blog.lukhnos.org/post/1464274570</link><guid>http://blog.lukhnos.org/post/1464274570</guid><pubDate>Tue, 02 Nov 2010 14:01:00 -0700</pubDate></item><item><title>People Who Don't Boast Their Working on Making the Change</title><description>&lt;p&gt;I don&amp;#8217;t have the DVD with me now, but in one of the bonus scenes of the film &lt;a href="http://www.helveticafilm.com/"&gt;Helvetica&lt;/a&gt;, &lt;a href="http://spiekermann.com/"&gt;Erik Spiekermann&lt;/a&gt;, a German design titan, talks about how his satisfaction comes from the fact that he designed the signage of train stations that people rely on and pass by everyday without realizing the design was his work.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s an interesting way of seeing things that I find, again and again, in people who design. It doesn&amp;#8217;t have to be visual design or typography: Think about designing a system, a software framework, a house, a mechanism, things that other parts rely on and people use.&lt;/p&gt;

&lt;p&gt;To stretch a bit, hence: There are at least two kinds of people. One boasts how they are going to make the change. The other makes the change, and moves on.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/1411167002</link><guid>http://blog.lukhnos.org/post/1411167002</guid><pubDate>Tue, 26 Oct 2010 18:31:00 -0700</pubDate></item><item><title>LEMON and That parse.y File in SQLite Source</title><description>&lt;p&gt;I had really only took a cursory glimpse of SQLite&amp;#8217;s source, and I had always thought the parse.y file was a yacc/Bison parser source. I was wrong: It&amp;#8217;s the parser source for &lt;a href="http://www.hwaci.com/sw/lemon/"&gt;LEMON&lt;/a&gt;, the parser generator &lt;em&gt;written by SQLite&amp;#8217;s author&lt;/em&gt;, D. Richard Hipp.&lt;/p&gt;

&lt;p&gt;According to its web page:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;The parser generated by Lemon is both re-entrant and thread-safe.&lt;/li&gt;
&lt;li&gt;Lemon includes the concept of a non-terminal destructor, which makes it much easier to write a parser that does not leak memory.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;SQLite is such an amazing example of source and tool self-reliance.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/1378808403</link><guid>http://blog.lukhnos.org/post/1378808403</guid><pubDate>Fri, 22 Oct 2010 22:24:00 -0700</pubDate></item><item><title>[A-Za-z0-9\s\n\.,]-Only Programming Language?</title><description>&lt;p&gt;Someone must have already said this somewhere.&lt;/p&gt;

&lt;p&gt;I did more note-taking, email and document drafting these days on my iPad. Most types of text I can do it fine on iPad&amp;#8217;s on-screen keyboard, except one thing: code snippets. C and C-like languages use tons of symbol characters that often hide two level down on iPad&amp;#8217;s keyboard, and it&amp;#8217;s horrible if you need to write just a simply code snippet to get your idea across. The alternative could have been an external keyboard, but then the new &lt;a href="http://www.apple.com/macbookair/"&gt;MacBook Air&lt;/a&gt; now totally makes sense: Programming stills belongs to the veritable laptops.&lt;/p&gt;

&lt;p&gt;Or does it? I stared at the on-screen keyboard and thought, maybe there is some programming language that can be written mostly using the Latin alphabet. Caveat: You don&amp;#8217;t want to make the language too similar to human language. Many scripting languages tried to do that (let x be Window, call open, blah blah&amp;#8230;). There seems to be some tricky boundary between real human language and programming language. Maybe the keywords should be in some dead language, like Latin, then&lt;sup id="fnref:p1364723468-1"&gt;&lt;a href="#fn:p1364723468-1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt;?&lt;/p&gt;

&lt;div class="footnotes"&gt;
&lt;hr&gt;&lt;ol&gt;&lt;li id="fn:p1364723468-1"&gt;
&lt;p&gt;If you want to be really picky, there should be a * in the title, after the square bracket (Tumblr&amp;#8217;s Markdown escapes one of the characters if I type the expression in the body text, so I&amp;#8217;m not going to do it). And, think about it, AZaz09 doesn&amp;#8217;t look like a bad name for a language. &lt;a href="#fnref:p1364723468-1" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;&lt;/div&gt;</description><link>http://blog.lukhnos.org/post/1364723468</link><guid>http://blog.lukhnos.org/post/1364723468</guid><pubDate>Thu, 21 Oct 2010 00:20:00 -0700</pubDate></item><item><title>And I still read essays sometimes</title><description>&lt;p&gt;&amp;#8220;&lt;a href="http://www.nytimes.com/2010/10/05/opinion/05iht-edcohen.html?_r=1&amp;amp;ref=opinion"&gt;Change or Perish&lt;/a&gt;&amp;#8221; by Roger Cohen, via &lt;a href="http://www.economist.com/blogs/lexington/2010/10/failing_recognise_ones_world"&gt;Lexington&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Before leggings, when there were letters, before texts and tweets, when there was time, before speed cameras, when you could speed, before graffiti management companies, when cities had souls, we managed just the same.&lt;/p&gt;
  
  &lt;p&gt;Before homogenization, when there was mystery, before aggregation, when the original had value, before digital, when there was vinyl, before Made in China, when there was Mao, before stress management, when there was romance, we had the impression we were doing all right.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The rhythm feels e. e. cummings-ish (&amp;#8220;long enough and just so long / tomorrow will not be too late&amp;#8221;). The tone sad, but beautiful.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/1254495339</link><guid>http://blog.lukhnos.org/post/1254495339</guid><pubDate>Wed, 06 Oct 2010 00:05:10 -0700</pubDate></item><item><title>Follow-Up on Objective-C Cyclic Retention in Block: Referencing Self</title><description>&lt;p&gt;I&amp;#8217;ve written on &lt;a href="http://blog.lukhnos.org/post/897176864/cyclic-retention-pitfall-in-objective-c-blocks"&gt;a pitfall of Objective-C blocks&lt;/a&gt;. Blocks implicitly retain the objects referenced inside them, and that can cause all kinds of problems.&lt;/p&gt;

&lt;p&gt;Mark Dalrymple has written on &lt;a href="http://borkwarellc.wordpress.com/2010/09/06/block-retain-cycles/"&gt;the same topic&lt;/a&gt;, and his emphasis is on using &lt;code&gt;self&lt;/code&gt;. He came up with a &amp;#8220;lighter&amp;#8221; solution without resorting to an NSValue object: Use the &lt;code&gt;__block&lt;/code&gt; storage class.&lt;/p&gt;

&lt;p&gt;Read his blog entry in full to understand the issue better. I highly suspect this issue will be with us for a long time. I don&amp;#8217;t think GC will rescue us all: Apple&amp;#8217;s GC is so buggy to the extent that it&amp;#8217;s &lt;a href="http://blog.lukhnos.org/post/828780709/why-my-next-mac-app-will-not-use-garbage-collection"&gt;totally unusable in some applications&lt;/a&gt;, and it&amp;#8217;s not available to iOS at least at the current version. The only hope to mitigate the problem is for the static analyzer to be able to warn the dangers ahead.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/1226846177</link><guid>http://blog.lukhnos.org/post/1226846177</guid><pubDate>Sat, 02 Oct 2010 00:47:00 -0700</pubDate></item><item><title>Lithoglyphics: LadyBugz 1.6.9: More Bug Fixes</title><description>&lt;a href="http://blog.lithoglyph.com/post/976901518/ladybugz-1-6-9-more-bug-fixes"&gt;Lithoglyphics: LadyBugz 1.6.9: More Bug Fixes&lt;/a&gt;: &lt;blockquote&gt;&lt;p&gt;We’ve just released &lt;a href="http://lithoglyph.com/ladybugz/"&gt;LadyBugz&lt;/a&gt; 1.6.9. It turns out that 1.6.7 didn’t completely fix the crashing bugs, and from the crash reports we’ve received (thanks everyone for letting us know!), it concentrated in one specific area. We’ve revamped that part of the code, and version 1.6.9 should clear that issue now.&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://blog.lukhnos.org/post/976967772</link><guid>http://blog.lukhnos.org/post/976967772</guid><pubDate>Thu, 19 Aug 2010 05:22:33 -0700</pubDate></item><item><title>Another Mile to Go in the GC-back-to-NonGC Transition</title><description>&lt;a href="http://blog.lithoglyph.com/post/921302735/ladybugz-1-6-6-fixes-the-crashing-bugs"&gt;Another Mile to Go in the GC-back-to-NonGC Transition&lt;/a&gt;: &lt;p&gt;From &lt;a href="http://blog.lithoglyph.com/"&gt;Lithoglyphics&lt;/a&gt;, the blog of my company:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;We have just released LadyBugz 1.6.6, available from &lt;a href="http://lithoglyph.com/ladybugz/"&gt;our website&lt;/a&gt; and through auto update.&lt;/p&gt;
  
  &lt;p&gt;After we released LadyBugz 1.6.1, we have received a number of bug reports that it crashed at various places. We realized that our &lt;a href="http://blog.lithoglyph.com/post/894232657/ladybugz-1-6-better-memory-usage-bug-fixes-and"&gt;garbage collection transition&lt;/a&gt; was not completely done, and there were loose ends that we overlooked. We take those issues seriously, and after a week of testing, we believe 1.6.6 should behave way better.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have written previously &lt;a href="http://blog.lukhnos.org/post/828780709/why-my-next-mac-app-will-not-use-garbage-collection"&gt;why we chose to go back from garbage collection to manual memory management&lt;/a&gt;, and while it was absolutely necessary, I really wish we could have found the crashing bugs earlier with a broader range of testing data set. Quitting the perks of garbage collection is a hard process (not having to worry about complex object relationship and ownership is especially addictive), and hard-to-reproduce crashing bugs suck. We have learned a thing or two the hard way.&lt;/p&gt;</description><link>http://blog.lukhnos.org/post/921355783</link><guid>http://blog.lukhnos.org/post/921355783</guid><pubDate>Sun, 08 Aug 2010 00:25:00 -0700</pubDate></item><item><title>Cyclic Retention Pitfall in Objective-C Blocks</title><description>&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: Please &lt;a href="http://borkwarellc.wordpress.com/2010/09/06/block-retain-cycles/"&gt;Mark Dalrymple&amp;#8217;s article&lt;/a&gt; for a more elegant solution. In short, use __block instead of the NSValue trick below.&lt;/p&gt;

&lt;p&gt;Is it tempting to write the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    NSBlockOperation *op = [[[NSBlockOperation alloc] init] autorelease];

    [op addExecutionBlock:^(void) {
        if (![op isCancelled]) {
            // run the block if the operation is not cancelled
        }
    }];

    [someOperationQueue addOperation:op];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you don&amp;#8217;t use garbage collection (gc), the snippet above will eat up memory, and the Leaks Instrument will not be able to discover it as a leak. Why?&lt;/p&gt;

&lt;p&gt;In Objective-C, blocks behave like objects. In non-gc runtime, they have retain count, can be copied&lt;sup id="fnref:p897176864-1"&gt;&lt;a href="#fn:p897176864-1" rel="footnote"&gt;1&lt;/a&gt;&lt;/sup&gt; and must be released when you want to discard them.&lt;/p&gt;

&lt;p&gt;When a block is created, it &lt;em&gt;implicitly&lt;/em&gt; retains every Objective-C object referenced in its scope. When the block is &lt;em&gt;deallocated&lt;/em&gt;, it sends &lt;code&gt;-release&lt;/code&gt; to all those retained objects. In Objective-C, those extra calls are done for you by the compiler. If you set breakpoints to &lt;code&gt;-retain&lt;/code&gt; and &lt;code&gt;-release&lt;/code&gt;, you&amp;#8217;ll be able to observe this behavior as blocks are created or deallocated.&lt;/p&gt;

&lt;p&gt;Usually you don&amp;#8217;t need to worry about those things, and that&amp;#8217;s how you can use blocks as good closures that many other modern languages have.&lt;/p&gt;

&lt;p&gt;But here&amp;#8217;s the problem: Because blocks only send &lt;code&gt;-release&lt;/code&gt; when they are deallocated, &lt;em&gt;not&lt;/em&gt; when their scope exits, it is possible for them to retain the objects which retain them. The code snippet above is a case in point. The &lt;code&gt;NSBlockOperation&lt;/code&gt; object &lt;code&gt;op&lt;/code&gt; retains an execution block, but then the block implicitly retains &lt;code&gt;op&lt;/code&gt;. The result of such cyclic retention is that the retain count of &lt;code&gt;op&lt;/code&gt; will never reach zero, and hence &lt;code&gt;op&lt;/code&gt; will eat up a chuck of unreclaimable memory. And because &lt;code&gt;op&lt;/code&gt; is retained by another object, it is not a lone wanderer that you forget to call &lt;code&gt;-release&lt;/code&gt;, therefore the Leaks Instrument will never discover it as a leak.&lt;/p&gt;

&lt;p&gt;A solution to this problem would be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    NSBlockOperation *op = [[[NSBlockOperation alloc] init] autorelease];

    NSValue *weakOpValue = [NSValue valueWithNonretainedObject:op];
    [op addExecutionBlock:^(void) {
        if (![[weakOpValue nonretainedObjectValue] isCancelled]) {
            // run the block if the operation is not cancelled
        }
    }];

    [someOperationQueue addOperation:op];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;NSValue&lt;/code&gt; object used above is effectively a weak reference to &lt;code&gt;op&lt;/code&gt;. This breaks the retention cycle, so the &lt;code&gt;-dealloc&lt;/code&gt; of &lt;code&gt;op&lt;/code&gt; will now be called, and the block will also be released.&lt;/p&gt;

&lt;p&gt;Cyclic retention is not a problem for Objective-C runtime&amp;#8217;s garbage collector, so the first code snippet will not be a problem if you use gc.&lt;/p&gt;

&lt;p&gt;Another incomplete solution is for Apple to change the timing of calling &lt;code&gt;-release&lt;/code&gt; in a block. Releasing retained objects when a block exits seems to be a reasonable expectation. But, given the nature of the use case above (an &lt;code&gt;NSBlockOperation&lt;/code&gt; might never get executed), plus the fact that referenced objects must be retained before a block is entered, it&amp;#8217;s not an encompassing solution, although it would make blocks behave more to such expectation.&lt;/p&gt;

&lt;p&gt;One final note: Because of the potential risk of cyclic retention, you must be very careful if you use blocks within a block. This is one place where gc can liberate us from such a blocking issue (pun intended), but gc is not available on the iOS and has &lt;a href="http://blog.lukhnos.org/post/828780709/why-my-next-mac-app-will-not-use-garbage-collection"&gt;its issues&lt;/a&gt; on Mac, too.&lt;/p&gt;

&lt;div class="footnotes"&gt;
&lt;hr&gt;&lt;ol&gt;&lt;li id="fn:p897176864-1"&gt;
&lt;p&gt;But not retained. The reason is that when a block is first created, its scope refers to the values residing in the nearest outer scope on the stack. If you toss it around (i.e. passing it as an object), it must first make a (const) copy of those values onto the heap. If you just retain a block then pass it around, chances are it&amp;#8217;s still referring to the stack. And you know what will happen when dangling stack references are passed around. &lt;a href="#fnref:p897176864-1" rev="footnote"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;&lt;/div&gt;</description><link>http://blog.lukhnos.org/post/897176864</link><guid>http://blog.lukhnos.org/post/897176864</guid><pubDate>Tue, 03 Aug 2010 01:20:00 -0700</pubDate></item></channel></rss>

