Definite's Extractor

My findings on Life, Linux, Open Source, and so on.

Monthly Archives: March 2012

Feature request for Google Code Issue tracker: search issue status change within a specified period

Just submitted a feature request for Google code to provide a search field that enable us to limit the search within a specified period.

 

http://code.google.com/p/support/issues/detail?id=27959

Perl glob gotcha: do not rely on glob alone to determine whether files exists

If you, say, writing a ‘ls’ subroutine and write down something like this:

sub ls{
    my ($pattern)=@_;
    for my $f (glob $pattern){
        print "$f\n";
    }
}

If you do

ls "*.txt"

This works, but if you

ls "index.txt"

It always shows “index.txt”, no matter whether “index.txt” exists or not.

This is actually an expected behavior. Consider typing following commands in shell.

gedit *.txt
gedit index.txt

For first one, shell passes all .txt file in current directory to gedit. For second one, shell should pass ‘index.txt’ to gedit, it’s up to gedit to determine what to do with if index.txt does not exist.

So for ‘ls’ like function, following are better:

sub ls{
    my ($pattern)=@_;
    for my $f (glob $pattern){
       if ( -e $f){
           print "$f\n";
       }
    }
}

Why currying?

拜讀  Jserv 的 以 C 語言實做 Functional Language 的 Currying 後,又去 Wikipedia 找了找 Currying 介紹, 一開始的印象其實是:「要是只要綁參數,那用 Macro 就好了。這東西該不會是學術界的蛋頭們發明出來裝高深吧。」

後來一想,一堆人都在用,不會大家都裝高深吧。於是繼續探索,發現也有一些人和我有同樣的疑問: What is ‘Currying’?What are the practical advantages of currying? 

其中講的最清楚的,就是 What are the practical advantages of currying? 的第一個答案。

簡單的說呢,不用 currying, 要寫把多個參數值相加的函數,可能就得

add2(a,b)
add3(a,b,c)
add4(a,b.c)

有了  curry, 你只需要寫一個

(define add_c
  ((curry action) +))

用的時候可以v像這樣

add_c 2 3 4 5

當然啦,要玩 currying, 還是得程式語言本身支援比較方便。