2014年11月1日 星期六

用 scala akka 寫一個簡單的 grep 程式

akka 是一個 actor model 。因此他的思維和一般寫程式的思維很不一樣。akka 可以讓我們很容易寫出 concurrent 的程式。我用 akka  寫了兩個 grep 程式,用不一樣的方式。

我會介紹的兩個程式碼在此


  是推薦寫法。 akka 鼓勵你大量建立 actor 。在這隻程式裡,每個資料夾或檔案都會對應到一個 actor ,一個 actor 只會負責一個檔案或目錄。



  是一個特別的寫法。這隻程式會重複利用 FolderExplorer (這個例子中,最多會有15 個 actor )和 FileReader (這個例子中,最多會有25 個 actor )。這個例子是為了示範 RoundRobinRouter 的使用方法。

scala 中,將 list 根據條件分成兩個,一個是滿足條件的 list,一個是不滿足條件的 list。

Scala 中, list 有許多內建好用的函數。partition 是其中一個。

來看個簡單的例子

List(1, 2, 3, 1, 2).partition(_ == 2)

換產生

res0: (List[Int], List[Int]) = (List(2, 2),List(1, 3, 1))

第一個 tuple 的元素是滿足條件的 list,第二個 tuple 的元素是不滿足條件的 list。

有了 partition ,就可以很容易作到一些事情,如建立字元出現頻率的對照表


  def makeMap(data: List[Char], output: Map[Char, Int]): Map[Char, Int] = data match {
    case x :: xs => {
      val (expected, rest) = data.partition(_ == x)
      makeMap(rest, output + (x -> expected.length))
    }   
    case Nil => output
  }


2014年8月2日 星期六

configure ssd for linux


add noatime,nodiratime,discard for your ssd device in /etc/fstab.

Like below
noatime,nodiratime,discard,errors=remount-ro


add below settings in /etc/fstab

tmpfs   /tmp       tmpfs   defaults,noatime,mode=1777   0  0

tmpfs   /var/spool tmpfs   defaults,noatime,mode=1777   0  0

tmpfs   /var/tmp   tmpfs   defaults,noatime,mode=1777   0  0

tmpfs   /var/log   tmpfs   defaults,noatime,mode=0755   0  0


In /etc/default/grub,
add "elevator=noop" for GRUB_CMDLINE_LINUX_DEFAULT

2014年7月26日 星期六

linux 下 chrome 界面亂碼解決方法

修改 /etc/fonts/conf.d/49-sansserif.conf倒數第四行,改成下面這樣或是換成其他你喜歡的字型。


ubuntu


參考資料:
http://blog.csdn.net/loveaborn/article/details/29579787


如果上面這個方法無效,試試看下面指令
$ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

2014年7月14日 星期一

一些黑底白字的瀏覽器外掛或 pdf reader

chrome:

High Contrast
Night Reading Mode  (推薦)

pdf reader:
evince  (到 view -> inverted color 將白底換成黑底)

2014年7月12日 星期六

How to build x86_64 image for qemu using buildroot

Download buildroot and change into buldroot folder. And then type below command.
make qemu_x86_64_defconfig
make -j8
qemu-system-x86_64 -M pc -kernel output/images/bzImage -drive file=output/images/rootfs.ext2 -append root=/dev/sda -vga std

Enjoy it.

scala 中 for ...yield 與 map, flatMap, withFilter 之互換


-------------------------------
for(x <- expr1) yield expr2
expr1.map(x => expr2)

-------------------------------

for(x <- expr1 if expr2) expr3
expr1.withFilter(x => expr2).map(x => expr3)

-------------------------------

for(x <- expr1 if expr2; seq) yield expr3
for(x <- expr1 withFilter expr2; seq) yield expr3

-------------------------------

for(x <- expr1; y <- expr2; seq) yield expr3
expr1.flatMap(x => for (y <- expr2; seq) yield expr3)

-------------------------------


for ((x1, x2) <- expr1) yield expr2
expr1.map{case(x1, x2) => expr2}

-------------------------------

for(pat <- expr1) yield2

expr1 withFilter {
  case pat = > true
  case _ => false
} map {
  case pat => expr2
}

2014年6月26日 星期四

How to attach to running container?



If you want to attach to running container, it's possible. First, use `sudo docker ps--no-trunc` to find your container full id. And use `sudo lxc-attach --name your_id` to attach. The way seems to be dangerous. Look before you leap.

2014年6月22日 星期日

introduce to call_seq.

    A tool to help you trace code efficiently. call_seq record the program how to run and save the result to file with json format.


Quick start to try it.
                $ pip install pyside pyqode.core pyqode.python
                $ git clone https://github.com/ya790206/call_seq
                $ python demo.py
                $ python -m call_seq.browser output.json        

How to record the program how to run.
    if __name__ == '__main__':
        with CallSeq(name='output.json'):
            test() #  the code you want to trace

After the file `output.json` created, then you run the below command.
python -m call_seq.browser output.json
Now you can trace code using call_seq.browser. Enjoy it.