LINQ

The more you use LINQ the more you will appreciate its power. The following short code snippet demostrates some thing we might have to be able to do easily when dealing with relational data but not collections in C#.

Write a program that findsany duplicate characters in a string.

Old school of thoughts – Loop through every character in the string while keeping a count on each. This type of algorithm could easily be O(n).

New way – Enter the new world of C#:

List<char> lstTest = "anapple".ToList<char>();
var result = from ss in lstTest
                group ss by ss into gr
                where gr.Count<char>() > 1
                select new { duplicateChar = gr };

foreach (var item in result)
    Console.WriteLine(item.duplicateChar.Key);

Now that’s cool!

Fun with Anonymous Method and Lambda

There are always multiple ways to write code to perform the same functionaltiy. With Lambda, we have even more options. Let’s say I want to use a delegate to perform different math functions I could do something like this:

    class Program
    {
        public delegate int MyHandler(int param);

        static void Main(string[] args)
        {
            Compute(SquareIt, 5);
            Compute(AddTen, 5);
 

            Console.ReadKey();
        }

        static void Compute(MyHandler handler, int max)
        {
            for (int i = 0; i < max; i++)
                Console.WriteLine(handler(i));
        }

        private static int SquareIt(int input)
        {
            return input * input;
        }

        private static int AddTen(int input)
        {
            return input + 10;
        }
    }

Now I could use an anonymous method to simplified the code a little:

    class Program
    {
        public delegate int MyHandler(int param);

        static void Main(string[] args)
        {
            Compute(SquareIt, 5);
           
            //The next line does exactly as "Compute(AddTen, 5);"
            Compute(new MyHandler(delegate(int x) { return x + 10; }), 5);

            Console.ReadKey();
        }

        static void Compute(MyHandler handler, int max)
        {
            for (int i = 0; i < max; i++)
                Console.WriteLine(handler(i));
        }

        private static int SquareIt(int input)
        {
            return input * input;
        }

    }

Well, I’m still not too happy about having to instantiate a new MyHandler so let’s change line 10 up there to this:


Compute(delegate(int x) { return x + 10; }, 5);

Now that looks much better. Now let’s have some real fun by using Lambda. Line 10 now can look like this:


Compute(x => x + 10, 5);

I guess now it’s pretty obvious how lambda cound help your code more readible and concise.

Reverse a String (now why on earth you wanna do that?)

The easiest way to reverse a string is to use the Reverse method on Array.

For example:

char[] charArr = new char[100];
charArr = "1234567890".ToCharArray();
Array.Reverse(charArr);
string reversedString = new string(charArr);

A different requirement could be to sort the string in ascending or descending order. The following code should give you a good idea on one of them (you’ll have to figure out which one it is):

...
Array.Sort(charArr, new ReversedComparer());

class ReversedComparer : IComparer
{
  public int Compare(object obj1, object obj2)
  {
    return -((IComparable) obj1).CompareTo(obj2);
  }
}

Converting a String to Byte Array

It’s easy enough to use System.Text.UnicodeEncoding.Unicode.GetBytes() to do this kind of conversion but problems could occur when you have invisible/control characters in the string. A better approach would be to first convert the string to a character array then to byte array:


private static byte[] StringToBytes(string str)
{
  Converter conv = new Converter((c) => (byte)c);
  Array.ConvertAll(str.ToCharArray(), conv);
}

2008 台灣總統大選預言 (2)

本人測字大膽預測, 馬蕭將贏約六十萬票左右, 特公佈於此為憑.

2008 台灣總統大選預言

今天心血來潮, 問老爸老媽要不要測個字, 看看到底明天誰會當選. 老媽測 “成”, 我想此字簡單, “馬到成功” 顯而易見, 不再多述. 老爸測個 “屁”, 此字有 “馬” 字之始, “九” 字之末. 自始至終都是馬英九, 看來天意已明.

資深股市分析師

台灣終於通過法律, 未來在媒體上擔任股市分析師的人, 都要有執照, 才能擔任此職務. 此法早就該立, 起碼能建立最基本的標準, 不至於讓一些人魚目混珠, 導致股市的混亂.

有位受訪的資深股市分析師有點酸味地表示, 未來即使有執照的分析師, 也不能保證投資人聽他的就能賺錢. 廢話! 不管有照沒照, 有誰能保證你能賺錢? 以我看來, 這些股市分析師, 大部分都不知道自己在說什麼, 或是一知半解, 看圖說故事. 要是他們真那麼厲害, 自己炒股都賺翻了, 還會在電視上拋頭露面, 教你如何賺錢?

我的結論是, 越資深的股市分析家越白癡, 一天到晚分析這, 分析那, 乍聽之下, 頭頭是道. 可是混了這麼久, 都還沒為自己炒到足夠的錢退休, 不是白癡是什麼?

放手

老婆轉寄龍應台在中國時報人間副刊的一篇文章 – 目送. 看完之後, 頗有感觸. 同樣的文章, 在十年前如果看到, 或許共鳴不大, 如今在女兒一天一天長大的同時, 想到很快我也將目送她的背影, 唏噓時光不留人的情景, 不禁猛然想起, 當初老媽嘴邊常掛著的一句話, 等你有兒女的時候, 你就會知道」.

當初在 Denver 找到了灣區的工作, 在百廢待舉, 無法安頓家小的情況之下, 隻身借住在友人家中. 每兩個禮拜方能飛回 Denver 一次, 看看兩歲多的女兒. 一次妻小送我到機場, 在登機之前, 貼心的女兒突然決定演出一場十八相送, 小手死命抓著我的衣服, 嚎啕大哭地就是不放手. 我硬生生地從她手裡掙脫, 倉皇地登機坐下, 眼中的淚水搖搖欲墜.

幾年後女兒上幼稚園, 第一天老婆和我一起送她去學校. 車子停下, 老婆稍稍整理女兒的頭髮, 不忘叮嚀著她要乖. 我蹲下身, 用力地抱了女兒一下, 正想著要說些什麼, 手一放, 只見她頭也不回地衝進教室, 留下依然半蹲的我, 錯愕地看著她的背影.

又隔幾年, 一天傍晚, 女兒決心要學會騎腳踏車. 拆掉了兩邊的輔助輪, 我小心翼翼地扶著她, 一次又一次的在家門前練習. 看著天色漸暗, 我想再試最後一次. 從後推著她的小腳踏車, 口中不忘提醒她別停下, 我的手輕輕放掉, 只見她搖搖晃晃地繼續向前, 終於漸行漸遠. 只記得自己歇斯底里地歡呼著 “Yes! Yes! You’ve got it!”

作父母的永遠沉浸在握著兒女小手的快樂中, 寧願不切實際地奢望時光會暫停, 也不願面對自己終有放手的一天. 直到兒女成天不發一語, 將自己鎖在房間, 做自己的事. 這才突然覺醒, 原來他們的手早已放開, 是我們還在巴著不放, 是我們已變成那在機場嚎啕大哭, 不願放手的小女孩.

兒女終有一天會讓我們目送他們離去. 問題是, 我們該選擇望著那頭也不回的背影愁悵, 還是看著他們漸行漸遠的身影, 在心中吶喊, “Yes! Yes! You’ve got it!”

Who Works for Whom?

A lot of times we can tell how a person thinks from the way he talks. For example, I hear this kind of casual conversation all the time:

“You know Joe? He’s now working at XYZ.”

“Oh yeah, Joe Smith, he used to work for me when I was at ABC. How is he doing now?”

What’s wrong with this dialogue? You might ask. Well, technically speaking, nobody really works for you unless you’re the president of a private company. In any other scenarios, everybody works for the company as a whole.

Some people love to use others to boast or to show how distinguished they are. It’s a pity that they never feel secured enough and are always in need of something tangible to prove their worthiness. Worse yet, since some of them try so hard to gain some form of recognition or status by every means, they may even be considered as hardworking or dedicated if they’re cunning enough.

Nobody works for you, pinhead! We all work for ourselves or our family.

台灣正常國家決議文 – 解碼版

台灣是主權獨立的國家,與中國互不隸屬,互不治理。但中國國民黨持續以經濟政績為名,打擊台灣民進黨,加上中國近來經濟快速成長的內外挑戰,形成當前民進黨永續執政的五大威脅。第一,民進黨無法繼續以凱子外交與名不見經傳的小國結交,藉以轉移其治國無方的焦點,造成「國際關係不正常」。第二,台灣民進黨越來越難用不當的中華民國憲法為台獨做藉口,導致被中間選民看破手腳, 在選舉中無法勝選,造成「勝選體制不正常」。第三,無法用外來威權做幌子,或用移花接木的紀錄影片來捏造中國國民黨對本土文化的欺壓,甚至殺害台灣人民的事實,造成「愚民認同不正常」。第四,無法將過去的黑金漂白化,以金融改革為藉口,圖利特定民營機構,破壞以公民身份為基礎的社會團結,造成「社會公義不正常」。第五,無法持續以中國國民黨不當黨產為由, 滋長民粹,分化社會,造成「政黨競爭不正常」。面對當前五大威脅,民主進步黨以勝選為唯一考量,應在「台獨黨綱」與「台灣前途決議文」的基礎上,進一步提出「正常國家決議文」,積極推動正名、制憲、加入聯合國、以轉移所有焦點。因此,我們提出下列主張:

一、持續推動以「台灣」的名義申請加入聯合國、世界衛生組織等國際組織,以轉移國內對經濟及領導人貪腐的不滿與抗爭.

二、為順應世界潮流,我國應訂定「三隻小豬」為成語.

三、政府應繼續鼓吹民粹,分化社會,製造矛盾. 如此才能掩蓋治國無方之事實,並鞏固票源。

四、積極尋找有利執政的假經濟數字,以證明政府政績。

五、政府應全面推動轉型正義,強調威權統治遺留的政治符號與資源分配不公,以製造紛爭. 介入並控制司法與檢調體制,以為民進黨領導人及菁英份子之司法案件解套, 隱藏其事實的真相。

民主進步黨認為,唯有持續以台獨為藉口,拉攏深綠民眾,再以積極利用民粹,為我不正常的選舉手段正名、合理化,如此方能混淆視聽,以劣幣逐良幣. 進而贏得各級選舉,獲取龐大利益.

Follow

Get every new post delivered to your Inbox.