顯示具有 programming 標籤的文章。 顯示所有文章
顯示具有 programming 標籤的文章。 顯示所有文章

2009年5月10日 星期日

snipMate, better than snippetsEmu!

Mac 上的 TextMate 最有名的 feature 就是會幫你補完程式碼,vim 也有個叫 snippetsEmu 的 plugin。好久以前曾經試用過,覺得很難用,而且 coding style 很醜 XD

今天我發現 vim 現在有個叫 snipMate 的 plugin,做的比 snippetsEmu 好很多! 另外,snippetsEmu 也已經一年多沒更新了,snipMate 則比較活躍,也比較令人期待。不多說,直接看作者提供的 screencast 吧:

snipMate.vim Introductory Screencast from Michael Sanders on Vimeo.



除此之外,它的 snippet 還支援 backtick 跟 vim script:

snippet date
`system("date +%Y-%m-%d")`

snippet filename_foo
`filename()`




如果你用 Emacs,可以試試 gugod 提到的 yasnippet。

2008年11月16日 星期日

LISP Practice

Quick-sort


(defmacro for-all (x op xs)
`(remove-if-not #'(lambda (y) (,op y x)) xs))

(defun qsort (nums)
(if (not nums)
()

(let ((x (first nums))
(xs (rest nums)))
(if (eq x nil)
()

(append
(qsort (for-all x < xs))
(list x)
(qsort (for-all x >= xs)))))))


Permutation

(defun remove-first (n nums)
(remove n nums :count 1))

(defun join-sublists (superlist)
(reduce #'(lambda (l1 l2) (append l1 l2)) superlist))

(defun permute-sorted-list (nums perm)
(if (eq nums nil)
(list perm)

(let ((unique-nums (remove-duplicates nums))) (join-sublists
(mapcar (lambda (n)
(permute-sorted-list (remove-first n nums) (append perm (list n))))
unique-nums)))))

(defun permutation (nums)
(permute-sorted-list (sort nums #'<) '()))

LISP 初體驗

原本以為 LISP 只是一個普通的 functional language,所以之前都沒去接觸。但是在看完<<駭客與畫家>>之後,開始對 LISP 產生興趣。

書裡面提到 LISP 擁有許多最近新語言開始導入的 feature,而 LISP 也擁有一些其他語言沒有的功能。像我主要是被 LISP 的 macro 吸引(比 C 的 macro 強大多了。可以先把他想像成是 return 程式碼的 function,而且是在 compile 前先展開。),所以開始看 <<Practical Common Lisp>> 一書。目前為止,大家比較熟悉的語言中,我只有在 Perl 6 裡面看到 (http://en.wikipedia.org/wiki/Perl_6#Macros)。

大部分的人對 LISP 的印象就是一堆噁心的括號,寫個加法都要用 (+ 1 2) 這種前置式來敘述,實在是不符合人類的直覺。而 LISP 整個程式碼都是這樣 -- 由一堆 "list" 所組成。(* (+ 1 2) (+ 3 4)) 有一個 list,第一個元素是 * 這個 "function",第二、三個是另外兩個 list。類似這樣的語法,在定義 function、if、while 等等都一樣。

但這之前隱含了一件事實:LISP 本身的程式碼,就是一個資料結構! 所以 LISP 的 macro 做的事情就是生出程式碼的資料結構,我覺得這就比 Perl 6 的 macro 那種 template 的方式優雅多了。不過,這個優點是建立在令人卻步的括號海上的...

接下來,po 幾個我的 LISP 練習 XD

2007年9月1日 星期六

iGoogle _IG_FetchContent supports Big5

前幾天發現 iGoogle gadget API 的 _IG_FetchContent, _IG_FetchXmlContent 和 _IG_FetchFeedAsJSON 開始支援 Big5 了! 以前寫 gadget 時,只要遇到要抓取 Big5 的網頁,就沒辦法用。因為之前這三個 function 假設網頁的內容是 UTF-8。不過前幾天我抓了一個 Big5 的網頁,發現內容竟然正常了。看來他現在會幫你猜網頁的 encoding 了! 這樣開發 gadget 就方便多啦。

Mapplet 也是一樣,因為 mapplet 也可以用 gadget 的 API。

也許 Google 正在辦的比賽會有更多的 idea 跑出來。

2007年6月9日 星期六

Parallel::ForkManager

在 Perl 裡面如何把這種程式平行化? 假設每次 interation 是獨立的。

foreach $data (@all_data) {
eat_cpu($data);
}



超簡單



use Parallel::ForkManager; # !!
$pm = new Parallel::ForkManager($MAX_PROCESSES);


foreach $data (@all_data) {
$pid = $pm->start and next;
eat_cpu($data);
$pm->finish;
}

2007年1月17日 星期三

C++ 發明人談 C++

C++ 的發明人 Bjarne Stroustrup 談 The Problem with C++。

有很多想法不知道怎麼表達。反正,我是懂越多 C++ 越不想用 C++,能不用 C++ 就不用 C++ 啊 Orz

2006年12月5日 星期二

InvSqrt()

看到開根號倒數 (InvSqrt(), 1 / sqrt(x)) 速算法,這真是太棒了。寫了一段 code 測試,跑 1..1000000000 出來的時間,1 / sqrtf(i) 出來平均大約是 9 秒多一點,而 InvSqrt() 是接近 3 秒。至於數值的精確度,我對 1 .. MAX 每個值丟給兩個方法看他們的差,發現數字越大越準,而 InvSqrt() 出來的值「略小於」1 / sqrtf(x)。

Let f1 = 1 / sqrtf(x), f2 = InvSqrt(x)
這個表第一欄是 MAX,第二欄是 \sigma_{1..MAX}(f1 - f2),第三欄是把第二欄 sigma 裡面的值取絕對值,第四欄是平均誤差。

1000000000: 8.0000000000 8.0000000000 0.0000000800
10000000: 5.7150559425 5.7150559425 0.0000005715
1000000: 1.8854961395 1.8854961395 0.0000018855
100000: 0.5647696257 0.5647696257 0.0000056478
10000: 0.1864424646 0.1864424646 0.0000186461


另外,這是多次 (f1 - f2) / f1 跟平均值:

1000000: 944.7488403320 0.0009447498
100000: 85.7057800293 0.0008570664
10000: 9.4323215485 0.0009433264
1000: 0.9615601301 0.0009625227


1/1000 以下的相對誤差,看起還不賴呀。以後應該會用得著 XD

2005年6月9日 星期四

[lang_zh]感覺很差[/lang_zh][lang_en]Hates...[/lang_en]

[lang_zh]昨天發現有個簡單的 case 能讓 g++ 3.3 link error,但 3.4 之後會正常。[/lang_zh]
[lang_en]I found that an easy/small program can make g++ 3.3 unhappy, but 3.4 (and after) feels good.[/lang_en]


#include

int main(void)
{
std::basic_string ustr;
ustr.append(ustr);
return 0;
}


[lang_zh]
不過 report 上去之後,才知道原來 GCC 這麼快就把 3.3 close 掉了。

不過讓我感覺很差的是,當我試圖把 gcc-3.3, g++-3.3 remove 時,發現還不少東西 depends on 他們。系統上得存在這種 bug 感覺很差 orz
[/lang_zh]

[lang_en]
But after the bug report, they tell me 3.3 branch has been closed.

But why I feel really bad is when I tried to remove gcc-3.3 and g++3.3 from my box, I found the many things depend on them. It's really sad that I have to let this bug living in my box :(
[/lang_en]

2005年5月23日 星期一

Self-reproduce

在 Jserv's blog 上看到一篇文章:
自我複製程式理論依據
提到 zao 的文章: http://s88.dyndns.org/index.php?p=144#more-144 ,他用
Turing Machine 來 prove 並 implement 一個 self-reproduce 的程式。

寫得還不錯 :)

2005年4月9日 星期六

makecontext(3)

竟然有這種東西 orz

#include

void makecontext(ucontext_t *ucp, void *func(), int argc, ...);
int swapcontext (ucontext_t *oucp, ucontext_t *ucp);

2005年4月3日 星期日

TIME_WAIT state

[lang_zh]
在寫計網作業時,發現只要有 client 連進來,在 httpd 砍掉之後,沒辦法馬上再 bind 同一個 port。netstat 看到了 TIME_WAIT。

Active close 一個 connection 時,必須在 TIME_WAIT state 等待 2 x MSL (/maximum segment lifetime/) 的時間。RFC 1122 的建議值是 120 秒。但是 Linux 的實作上 follow BSD,把 MSL 設為 30 秒,net/ipv4/tcp_minisocks.c 裡面有這段註解:
[/lang_zh]

[lang_en]
When I wrote my Computer Network's homework, I found that binding to the same port will be failed after a client comes in and stop the httpd. I see TIME_WAIT state when execute netstat.

One should wait in TIME_WAIT state for 2 x MSL (/maximum segment lifetime/) after close one connection. The suggestion time interval in RFC 1122 is 120 seconds. But the implementation of Linux follows BSD, which set MSL to 30 seconds. There's a comment in net/ipv4/tcp_minisocks.c:
[/lang_en]


* [ BTW Linux. following BSD, violates this requirement waiting
* only for 60sec, we should wait at least for 240 secs.
* Well, 240 consumes too much of resources 8)
* ]



[lang_zh]
試了一下時間,真的是一分鐘 Q_Q 本來是在做測試,懶得把 port 寫在參數裡面。看來得辛苦一點點啦 XD

BTW, Unix Network Programming 真是好書呀!!

Update:
用 setsockopt 打開 SO_REUSEADDR 可以解決這個問題
[/lang_zh]

[lang_en]
I observed the time, and it really about 1 minute Q_Q So I should add a port to the argument :/

BTW, Unix Network Programming is really a great book!

Update:
Use setsockopt to enable SO_REUSEADDR will solve this problem.
[/lang_en]

2005年4月2日 星期六

accept(2) and SIGINT

在寫計網作業時,發現如果用 signal(2) 去 override default signal handler,當 accept(2) block 住時,接到了那個 signal,並不會 return 回來。這跟平常沒 override 時,會傳回 -1 並將 errno 設為 EINTR 的情形不同。翻了 > 才發現原因。

在 accept, read 這類的 /slow system call/ 裡面接到 signal 後,當 signal handler return 時,傳回 EINTR。``有些''系統會重新回到該 system call 執行 (現在的 Linux & FreeBSD 都是),我在想也是因為這個原因,我的 accept 不會 return。

後來,我用 sigaction(2) 取代 signal(2),sa_flags 不要設 SA_RESTART,就可以如我所願讓 blocked 的 accept 回來了。這個方法在 Linux 跟 FreeBSD 下都 work。