defalias と defadvice を使って似た関数を作る

昨日のエントリid:higepon さんにやたらと感謝されたはいいが、「ひげぽん OSとか作っちゃうかMona- - defadvice楽しい」のコメント欄で

  • 一時的に挙動を変更するのに defadvice を使うのは行儀がよくない
  • compile で例外が送出された場合に advice が無効にならないので unwind-protect を使わないといけない

とダメだしを食らった。確かに。
で、それを踏まえてこんなんしてみた。

(defalias 'silent-compile 'compile)
(defadvice silent-compile
  (around force-silent-compile activate)
  (let ((orig-yes-or-no-p (symbol-function 'yes-or-no-p)))
    (defun yes-or-no-p (prompt) t)
    (unwind-protect
        ad-do-it
      (fset 'yes-or-no-p orig-yes-or-no-p))))

本当は silent-compile から compile を呼び出せばいいかと思ったのだけど、interactive な引数の扱いが面倒だったので defalias で定義して defadvice で advice を引っ掛けるようにした。silent-compile のくせに M-x silent-compile だとコンパイルコマンド聞かれるけど。
M-x silent-compile って呼ぶ必要がないなら

(defun silent-compile (command &optional comint)
  (let ((orig-yes-or-no-p (symbol-function 'yes-or-no-p)))
    (defun yes-or-no-p (prompt) t)
    (unwind-protect
        (compile command comint)
      (fset 'yes-or-no-p orig-yes-or-no-p))))

でいいんだけどね。

New semester

新学期なのか3人ほど人が増えて、1人で使っていた自分のオフィスにスペイン人が来た。
3ヶ月の滞在らしいが、NYU に来るのは2回目らしい。
何でも昨日の飛行機でNYに来たらしいが、何もそんな日のNY行きの飛行機に乗らんでもいいのに。実は特別に安かったりするのだろうか。

Mapの簡易初期化

ふっと思いついた Map の初期化。

public static <K, V> Map<K, V> buildMap(Object... arguments) {
    Map<K, V> map = new HashMap<K, V>();
    if (arguments.length % 2 != 0) {
        throw new IllegalArgumentException("number of arguments must be even.");
    }

    for (int i = 0; i < arguments.length; i += 2) {
        K key = (K) arguments[i];
        V value = (V) arguments[i + 1];
        map.put(key, value);
    }
    return map;
}

使うときはこんな感じ。

Map<String, Integer> map = buildMap("a", 32, "b", 33, "c", 34);

TreeMap がよければ

Map<String, Integer> map = new TreeMap<String, Integer>(buildMap("a", 32, "b", 33, "c", 34));

う〜ん。ちょっと微妙かな。