Archive for the 'General' Category

Y-combinator in Python

Sunday, August 3rd, 2008

In my current work, the main stream platform is .NET. I have not invested a lot of time to start to program in C# for .NET, but I am kind of attracted by F#, which is a functional language for .NET that I would like to know more about.

Recently, I purchased a book to know more about F# and I learn some thing that is called
Y-combinator” in functional programming. The F# code for the “Y-combinator” looks like this,

let rec y f x =
  f (y f) x;;

You can apply the “Y-combinator” to some non-recursive functional function to make it recursive. For example,

let fac f = function
  | 0 -> 1
  | n -> n * f (n-1);;

> y fac 5;;
val it : int = 120

I was curious about how to do similar thing in Python. So, I try the following code snippet:

def Y(f):
  def g(x):
    return f(Y(f))(x)
  return g

def fac(f):
  def g(x):
    return 1 if x == 0 else x * f(x-1)
  return g

With these function definitions, “Y(fac)(10)” will give you the correct result 3628800. On the other hand, it is not really easy for me to understand this code as most of my daily programming tasks are in imperative programming style with some more straight forward functional feature. Beside the mathematical way (described in the wiki page) to understand how this work, one way one can see how this work is to see how the code unroll under python interpreter:

Y(fac)(5) -> fac(Y(fac))(5) -> g(5) inside fac with f = Y(fac)

Then the “g(5)” inside fac returns 5 * Y(fac)(4) . The Y(fac)(4) will return 4 * Y(fac)(3) and so on.

It seems there is a lot interesting thing in pure functional world. Hope I will learn more about them soon.

phdfs.py, a ctypes wrapper of hadoop libhdfs for python

Saturday, May 10th, 2008

I use python and hadoop distributed file system (HDFS) to process large amount of data at work. Instead of using the regular map-reduce mechanism provided by hadoop, I have my home-made map-reduce python engine written using Pyro. It turns out it is quite efficient and sometimes it is much faster than the corresponding streaming code for some simple map-reduce work. For this kind of work, I access the file in HDFS using “hadoop fs -cat” by the unix pipe (popen) in python. It seems to me it might be useful to be able to bypass the somehow ugly unix pipe and “hadoop fs -cat” combination. There already is a SWIG wrapper of python for hdfs. However, I think it will be nice to have ctypes wrapper such that no extra compiling is necessary for installation. I spend a few nights working on such wrapper and hope it will be useful. The results is a single python module that I call “phdfs“. It provides most of the API in the libhdfs. It will be useful if one want to read, write and manipulate the hadoop filesystem with the flexible and powerful python syntax.

You can download the phdfs.py, and try it out yourself. I have not tested all the methods, so YMMV.

Postdocs, “Not Exactly Students, Not Exactly Employees, What are you?”

Saturday, May 3rd, 2008

My neighbor shows me this article from East Bay Express. Those stories sound very familiar. My personal feeling is that such academic system should be fixed soon. The academic society should give more recognition to postdocs.

As a postdoc, you don’t get those benefit to students. You are not considered as a formal employee. You don’t get any benefit and you are paid low in the name of science. I still remember that I felt so absurd when I was told I could not pay my monthly parking fee by automatic deduction from my paycheck, because I was a “temporary worker” in the school I had being working for a few years.

Well, I can not say that my career is not benefit from my postdoc research. But, I can not say I totally enjoy being treated by the school as “temporary worker” for an indefinitely amount of time. One should treat the real “working horses” in the academic research industry a little better. Without these working horses, there will be no “super-star” in research communities. Anyway, there is not much point for me to complain anymore. Industrial R&D can be fun too.

奇文共賞

Friday, April 18th, 2008

在二十一世紀的今天,台灣的某大報系下的海外版的社論出現下列的句子:

『在「百年老店」裡,58歲的馬英九是春秋鼎盛、如日方中的新星。』

『他領導國民黨仆而復起,號召台灣人民、尤其是青年一代,終結了台獨政權,正是「青年創造時代」的典型。』

『愛因斯坦的「相對論」改造了百年間的科學奧秘,而孔孟之道卻歷經千餘年影響世道人心,連馬克斯信徒也不得不信。』

『鼓勵青年學習馬英九,絕不是搞甚麼「偶像祟拜」,更無意要造一座「新神」,而是就近取譬,用大家都看得見的事實,期勉繼往開來的青年世代,好好鑄造自己、鍛鍊自己,無負「青年創造時代」的期望。』

久居國外,我對馬英九了解不算多,也沒有意見。但看了這文章後,不得不想起那連小學裡作文都要以『解救大陸水深火熱同胞』『以三民主義統一中國』的年代。也許,只是也許,某聖君可以不和獨裁磕頭,完成反共復國的大業。這樣就不用每年去拜拜了。

Disclaimer: 我年幼無知的時候為了考試或是混公假,應該也寫了不少奇聞,不過那可是上世紀的歷史共業呀!

奇文原出處之一

我最先發現奇文的地方

My one day trip to Lugradio, San Francsico, 2008

Saturday, April 12th, 2008

從 PingYeh 那聽到有 Lugradio 這週末在 San Francisco 舉行, 一時興起,決定和老婆女兒告假一天去看看熱鬧。

雖然我在 1993 還是 1994 安裝過 Linux with kernel version 0.97 後,有幾年是非 Linux 不用的人,參加 Linux / open source 社群的活動倒是第一次。台灣的 open source 活動開始熱絡的時候,我人已不再台灣,而人在米國的時候,因為學業和懶的關係也沒有看看過有沒有甚嘛好玩的活動可以參加。所以對我來說,這一次湊熱鬧的感覺是很新鮮的。

Img 4957

我約十一點多到達會場,當場交了米金大洋十塊錢,註了冊,拿了名牌和有贊助商的小禮品的小袋子就進到會場裡逛逛。會場是在 San Francisco Metreon 戲院的頂樓的 CITY VIEW,從前到 Metron 時從來沒聽過有這麼個地方可以辦活動又還有不錯的 city view 的地方。同一時間內,會場會有三場演講進行,你可以選擇比較有興趣的來聽。不想聽的話,就可以逛逛廠商地展示。我隨意聽了幾個演講:其中有 Second Life 的人來說他們 open source 的策略,有 Bungee Connect 的人示範他們的發展平台,有 VMWare 的人展示 Virtual Machine 的 Streaming,也有 Humanized 的 Aza Raskin 討論使用者介面等等。 大部分都還滿有趣,但並沒有在很多技術上比較有深度的討論,大多的討論都在比較形而上的層次。但這樣也好。而從其他聽眾的提問看來,很多參與的人很重視 open source 的發展。

在其他廠商展示方面,我跑去收集了不少 linux 廠商提供的 live CD。而在眾多的廠商展示裡,對我來說最有趣的卻是兩個硬體的廠商。其一是 TI 可以跑 Linux 的單晶片電腦 beagleboard 。看來等 TI 六月出了這東西,我可能會受不了灑點錢買來玩玩。

Img 4954

另外一個有趣的是我終於看到傳說中的 OLPC,的確是很可愛讓人會不住把玩的東西。可惜這有趣的 laptop 只能看看而已。

Img 4955

今天最後一個演講到五點,本來要 skip 晚上的 party,已打算要回家了。在離開會場前,一個對 amateur biotech 有很大興趣的軟體工程師在得知我在一家 biotech 公司工作後,興高采烈的和我討論有沒有甚麼可以在家裡做的 biotech 的計畫,聊了一個小時後才放我回家。

明天 Lugradio 還有一整天的活動,不過我有其他事要做,不能去了。但今天的一日行倒是收穫不少。意外地得了不少在工作上或是家中得不到得 inspiration 和平常不容易看到的 San Francisco City View!!
Img 4960

ad$ense or ad$pam?

Wednesday, April 2nd, 2008

200804022149

I wish I have a little bit more virtual memory so I can convert virtual money to real one.

Tokyo Tower at Night

Sunday, April 22nd, 2007

Tokyo Tower At Night

The night view of Tokyo on the fifty-fifth floor in the Mori Tower.
六本木某大樓五十五層的東京夜景。

Viacom vs. google

Tuesday, March 27th, 2007

Well, actually, I just want to test video embedding. This clip is pretty
funny anyway.

Demonstrate “quickhull” implementation in javascript

Tuesday, March 13th, 2007

Quickhull is an algorithm that is similar to the quicksort using a divide and conquer strategy to find the convex hull for scattered points. The green line in the plot is the initial base line and gray lines are the intermediate base line. The final convex hull is shown in red.



source code: qh.js


< 

Technorati Tags:

The Chariman of Google

Monday, February 26th, 2007

They must forget to google search “Chariman Schmidt”.
See also “Look up Chariman”.

This video about “googleTube” is fun too.

Diggs in a box

Monday, February 12th, 2007

I have been a fan of the “treemap” algorithm to visualize data with tree-like structure . Finally, I decide to implement one last Friday noon. After a few hours, I had a basic Treemap class in python. When I was testing the class, I found the algorithm can generate some interesting artistic painting effect.

The following is a composition using the Gene Ontology. I guess this is one of the many ways to visualize the so-called Systems Biology. You can also click on the image to see some other randomly generated compositions.

Picture 2

Inspired by Newsmap, I tried to see if I can use the python Treemap class and some javascript to map Diggs to a treemap. So, I spent a few hours in the last weekend making this little toy — Diggs in A Box.

Picture 3


It is rather primitive now and I can think a long TODO list to improve it. Hopefully, I will get some time to improve its functionality and appearance eventually. If you have any suggestions, please leave a message and I hope you find such presentation of data interesting and useful.

Technorati Tags: , ,

What is this little animal?

Tuesday, February 6th, 2007

On my way back home yesterday, I saw this small animal. I have never seen this kind of animal before. Do you know what it is? I am just curious to find it out.

200702061752