life = f()();

Mar 15

Scumbag UITableView

跟 UITableView很不熟,也處得很不好。

最麻煩的是,如果要動態改變 UITableViewCell的高度,得經由 TableView的 -beginUpdates讓它自己去問 -tableView:heightForRowAtIndexPath:,而且在其中無法取得現在正在用的 UITableViewCell,於是只好先把高度,或是有沒有被選起來,存在別的地方,而不是跟 UITableViewCell放在一起。

更糟的是 -tableView:heightForRowAtIndexPath:有效率問題。可是 UITableView是呈現資料最方便的 View,自然希望它更方便做各種變化。

要怎樣跟他變成好朋友?

Mar 07

.tmux.conf

set -g utf8 on
setw -g utf8 on
setw -g mode-keys vi

# status bar
set status on
set -g status-bg default
set -g status-fg colour33
set -g status-justify centre
set -g status-left '[ #H ]'
set -g status-left-length 20
set -g status-right '[ %H:%M %D ]'

# window title
setw -g window-status-fg colour24

# current window
setw -g window-status-current-fg colour51

Too bad we have to compute colors manually.

references:

Mar 04

current .screenrc

# Turn off the startup message.
startup_message off

# Hardstatus
hardstatus alwayslastline
hardstatus string '%{= .W}[ %H ][%=%?  %-W%?%{= .r}  * %t%{= .W}%+w%=]'

# New screens
screen -t run 1 bash
screen -t main 0 bash

Feb 26

imagination

Updated.

var f = function() {
    var __kind = "O";

    return function(name) {
        // private things here
        var _vars = {};
        _vars["kind"] = function() {
            return __kind;
        };
        _vars["name"] = name || "anonymous";
        
        // then return a new instance
        // this should be hide by rewriting
        return {
            get: function(key) {
                return _vars[key];
            },
            set: function(key, value) {
                _vars[key] = value;
            }
        };
    };
};

// first () create a closure with internal vars
// second () create an instance
// well, it also looks like "OO"
var o = f()();

// or in this way
var cls = f();
var p = cls("sam");

o["set"]("x", 10);
p["set"]("x", 20);
console.log("o.name: " + o["get"]("name"));
console.log("o.kind(): " + o["get"]("kind")());
console.log("o.x: " + o["get"]("x"));
console.log("p.name: " + p["get"]("name"));
console.log("p.kind(): " + p["get"]("kind")());
console.log("p.x: " + p["get"]("x"));
​
// attention: no . notation so far

What will it looks like after rewrite.

f = () {
    return (name) {
        @@kind = "O";
        @kind = () {
            return __kind;
        };
        @name = name || "anonymous";
        return $;
    };
};

o = f()();

cls = f();
p = cls();

o.x = 10;
p.x = 20;
log("o.kind(): " + o.kind());
log("o.x: " + o.x);
log("p.kind(): " + p.kind());
log("p.x: " + p.x);​

Fun with JavaScript Singleton

var Normal = function() {
    //initialize here
    this.x = 1;
    this.y = 2;
};

var Singleton = function() {
    var shared = function() {
        //initialize here
        this.x = 10;
        this.y = 20;
    };
    
    shared.meow = function() {
        console.log("meow!");
    };

    return function() {
        return shared;
    };
}();

/* show time! */

var a0 = new Normal();
var a1 = new Normal();

a0.x = 0;

console.log(a0 == a1); // false
console.log(a0.x); // 0
console.log(a1.x); // 1

var b0 = new Singleton();
var b1 = new Singleton();

b0.x = 15;
b0.meow(); // meow!

console.log(b0 == b1); // true
console.log(b0.x); // 15
console.log(b1.x); // 15

I think this is much clear than this one, but THE reputation system on stackoverflow prevents me from answering it :D

The key of this code is that JavaScript’s construct will return an object instead of a new instance if it return any kind of object! Such a strange language!


With delegate/proxy, we can reuse without class, with closure, we can accomplish encapsulation without keywords, what’s more?

Feb 22

viewDidLoad

…This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method….

” — Scumbag UIViewController Class Reference

Feb 20

Cocoa Design Patterns Errata > creating the singleton does not work -

跟 Objective-C還有 Cocoa很不熟, Cocoa Design Patterns還真是開了我的眼界。

可是 Singleton的 code不能用了。

原來是因為 [super alloc]會叫到 [self allocWithZone:]。文中提到的辦法是改叫 [super allocWithZone:nil]來閃過,總覺得不是那麼直觀,不過也只好將就用用先。

P.s. 還好沒看中文版,那句 One pattern to rule them all的味道沒了XD。

Feb 16

prototype的聯想

去年中開始看 Douglas Crockford的 “JavaScript: The Good Parts”,才知道 JavaScript這幾年不是無緣無故翻身。而 prototype-based OOP與 class-based OOP的比較,讓我對 OOP有了不同的認識。

先看看 prototype繼承的用法, Crockford的作法是:

function beget(o) {
  function F() {};
  F.prototype = o;
  return new F();
}

之所以不能簡單的先隨便弄個新的 instance n再讓 n.prototype = o,是因為 ECMAScript不云許隨便抽換 instance的 prototype,真正的 prototype是存在一個內部變數, Dmitry Soshnikov稱它為 [[prototype]], FireFox的 SpiderMonkey有個非標準的變數 __proto__就是讓這個 [[prototype]]可以被存取。

牛人 Dmitry Soshnikov在介紹到繼承時的範例是:

var inherit = (function(){
  function F() {}
  return function (child, parent) {
    F.prototype = parent.prototype;
    child.prototype = new F;
    child.prototype.constructor = child;
    child.superproto = parent.prototype;
    return child;
  };
})();

之所以都要有個放在 closure裡面的 F,是為了避免直接用到 parent的 constructor。

悲劇的是以前寫 AS2時, AS2包裝 class inheritance的方法沒那麼好,結果無法控制 parent class第一次初始化的時間,我的 class constructor就在意料外的時候執行。

JavaScript的 prototype-based跟 class-based不衝突很讚,而我不想,也沒能力跳到動態與靜態語言之爭。另我好奇的是,如果 OO的重點不在於怎麼繼承而在於 reuse的話,那種方法並不重要不是嗎?這樣要是有個專注在 reuse上面的語言,會長什麼樣子呢?

大牛 jserv曾寫過以 C 語言實做 Javascript 的 prototype 特性這樣酷的文章,可見 C要用 prototype或 class來 reuse都可行,要是能把細節藏起來提供 syntax sugar或是有好的 IDE就更棒了。

另外一方面,從這個角度重看 c2.com整理的 OO定義,可以簡化為:

  1. 一切皆為物件
  2. 物件有自己的記憶體,可以包含別的物件
  3. 物件藉由傳遞訊息來溝通,溝通的目的是為了要求別的物件作些事
  4. 物件可以把訊息委派給其他物件

所以我們有機會擁有更簡潔但是一樣強大的語言對吧?對吧?(興奮)不知道會是什麼樣的語言?搞不好已經有了但是我不知道XDD

Jan 30

From NeXTSTEP to Cocoa: Erik Buck on the Development of Cocoa and Objective-C -

What are asynchronous graphics?

Jan 14

The Word of Notch: Coding skill and the decline of stagnation -

notch:

The more I learn, the more I realize how little I know.

I hope some day I can call myself a game programmer.

The interesting part is that the more platform I work with, the more things I learned. Different languages and different paradigms open up my mind.