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.





2014年5月4日 星期日

scala 檔案目錄操作

如果你要對目錄下作一下操作,如列出所有的檔案之類的。可以考慮用 reflect.io 這個 package。簡單的用法如下。

import reflect.io._

Directory(".").list


iscala

iscala ,一個類似 ipython 的東西,不過他是給 scala 用的。用這個 shell 最大的好處是,你可以按 tab 健來觀察該物件有哪些屬性或方法。

iscala 的官方網站在這。https://github.com/mattpap/IScala。比較簡單的安裝方法就到這個頁面下載 iscala ,然後用下面 bash 執行。(注意:你可能要修改你的 JAR_PATH 路徑)

#!/bin/bash
JAR_PATH="$(dirname $(dirname $(readlink -f $0)))/lib/IScala.jar"
KERNEL_CMD="[\"java\", \"-jar\", \"$JAR_PATH\", \"--profile\", \"{connection_file}\", \"--   parent\", \"$@\"]"
ipython console --profile scala --KernelManager.kernel_cmd="$KERNEL_CMD" --no-banner

2014年5月3日 星期六

在 sbt 下使用 scala 內建的 actor



在 build.sbt 裡新增
libraryDependencies +=
      "org.scala-lang" % "scala-actors" % "2.10.0"

scala-actors 是 library name
2.10.0 是版本號

他有提供什麼 library 以及版本可從下面列表去找

scala 的 library 列表
https://oss.sonatype.org/content/groups/public/org/scala-lang/


2014年4月6日 星期日

scala 中對於不接受參數的函數定義方式與使用方法

    scala 對於不接受參數的函數定義方式有兩種。如下面程式碼所示。

def test1 = { // 1. without ()
  println("SS")
}

def test2() = { //2. with ()
  println("GG")
}


test1()

    第一種寫法,沒有 () 。使用時,不可以加上 () 否則會出錯。因此呼叫時只有一種寫法,test1。

    第二種寫法,有 () 。使用時,可以加上 (),也可以不要加上 () 。因此呼叫時有兩種寫法, test2 或 test2()。

scala 語言特性優缺點分析

這篇文章闡述哪些 scala 特性"我認為"是好的,哪些是不好的。因為是"我認為",所以這是一篇充滿主觀意見的文章。


優點:

  1. type inference: 這個特性可以減少程式設計師輸入重複的資訊。
  2. lazy keyword: 可以做到延遲計算的功能 
  3. currying: 讓人更容易產生 partial function。
  4. 對於接受參數型別為 => Unit(Int等),要傳參數時,可以簡單的用 { } 代替。詳見範例 1
  5. actor: 多執行緒的函式庫,用 message queue share memory 的問題(當然,還是要看你的寫法。沒寫好還是有 share memory 的問題)



範例1
def repeat(times: Int) (func: => Unit) {
  for (n <- 1 to times) func
}

repeat (5) {
  println("SSS")
}


缺點:

  1. implicit conversion: 這會影響可讀性。因為你在閱讀時,無法立刻知道他在執行時,是以何種物件在執行。不過這個特性應該是為了能夠和 jvm 整合所產生的特性。
  2. 探討 scala 的函數呼叫與自動推論型別所產生的模糊情形


結論: scala 是一個很好的語言。不過他是 function programming language 的關係,可能不會紅起來。但是下一個如果能夠吸收 scala 的優點,改進 scala 的缺點,我猜能夠成為下一個主流語言。

2014年4月5日 星期六

scala play framework 使用方式





要建立專案,輸入

$ play new myFirstApp



進入 play 的 shell (用來輸入指令的)
$ cd myFirstApp
$ play shell


如果要啟用 dev server 在 play shell 輸入

run

或是

$ play run


進入 play 的 console (用來輸入程式碼的)

$ play debug console

如果要使用 stand-alone server

$ play start

你也可以把你的程式打包起來,複製到其他地方執行

$ play dist

他會產生一個 zip 檔,包含所有執行時必要的 jar。



其他可能有用指令

因為 neo4j 只支援 openjdk-7 ,所以用下面指令檢查 java version。不然 play start 未必能正常運作。

$javac -version
$java -version